Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support non ascii characters of resource path on SpringBootVFS #675

Merged
merged 3 commits into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2015-2021 the original author or authors.
* Copyright 2015-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,8 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -34,8 +36,13 @@
*/
public class SpringBootVFS extends VFS {

private static Charset urlDecodingCharset;
private final ResourcePatternResolver resourceResolver;

static {
setUrlDecodingCharset(Charset.defaultCharset());
}

public SpringBootVFS() {
this.resourceResolver = new PathMatchingResourcePatternResolver(getClass().getClassLoader());
}
Expand All @@ -47,13 +54,27 @@ public boolean isValid() {

@Override
protected List<String> list(URL url, String path) throws IOException {
String urlString = url.toString();
String urlString = URLDecoder.decode(url.toString(), urlDecodingCharset.name());
String baseUrlString = urlString.endsWith("/") ? urlString : urlString.concat("/");
Resource[] resources = resourceResolver.getResources(baseUrlString + "**/*.class");
return Stream.of(resources).map(resource -> preserveSubpackageName(baseUrlString, resource, path))
.collect(Collectors.toList());
}

/**
* Set the charset for decoding an encoded URL string.
* <p>
* Default is system default charset.
* </p>
*
* @param charset
* the charset for decoding an encoded URL string
* @since 2.3.0
*/
public static void setUrlDecodingCharset(Charset charset) {
urlDecodingCharset = charset;
}

private static String preserveSubpackageName(final String baseUrlString, final Resource resource,
final String rootPath) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,17 @@ void whenCustomSpringLiquibaseIsDefinedThenMybatisSqlSessionTemplateDependsOnSpr
});
}

@Test
void testTypeAliasesWithMultiByteCharacterInPackageName() {
this.contextRunner
.withUserConfiguration(EmbeddedDataSourceConfiguration.class, MybatisBootMapperScanAutoConfiguration.class)
.withPropertyValues("mybatis.config-location:mybatis-config2.xml").run(context -> {
org.apache.ibatis.session.Configuration configuration = context.getBean(SqlSessionFactory.class)
.getConfiguration();
assertThat(configuration.getTypeAliasRegistry().getTypeAliases()).containsKey("シティー");
});
}

@Configuration
static class MultipleDataSourceConfiguration {
@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright 2015-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.spring.boot.autoconfigure.バリューオブジェクト;

@SuppressWarnings("unused")
public class シティー {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--

Copyright 2015-2022 the original author or authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="org.mybatis.spring.boot.autoconfigure.バリューオブジェクト"/>
</typeAliases>
</configuration>
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@
<siteDirectory>${project.build.directory}/site-src</siteDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- Add setting 'file.encoding' for Windows environment -->
<argLine>${argLine} -Djdk.attach.allowAttachSelf -Dfile.encoding=UTF-8</argLine>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Expand Down