diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java index a49835d6..061e7979 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepository.java @@ -266,6 +266,7 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc } name = name.replace("applicationConfig: [", ""); name = name.replace("file [", "file:"); + name = name.replace("class path resource [", "classpath:/"); if (name.indexOf('[') < 0) { // only remove if there isn't a matching left bracket name = name.replace("]", ""); @@ -313,6 +314,9 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc if (!pattern.contains(":")) { pattern = "file:" + pattern; } + if (pattern.startsWith("optional:")) { + pattern = pattern.substring("optional:".length()); + } if (pattern.startsWith("file:")) { pattern = StringUtils.cleanPath(new File(pattern.substring("file:".length())).getAbsolutePath()) + "/"; } @@ -320,14 +324,16 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc if (logger.isTraceEnabled()) { logger.trace("Testing pattern: " + finalPattern + " with property source: " + name); } - if (normal.startsWith(finalPattern) && !normal.substring(finalPattern.length()).contains("/")) { + if (normal.startsWith(finalPattern)) { matches = true; break; } if (locations != null) { - return !Arrays.stream(locations).map(this::cleanFileLocation) - .noneMatch(location -> location.startsWith(finalPattern) - && !location.substring(finalPattern.length()).contains("/")); + matches = Arrays.stream(locations).map(this::cleanFileLocation) + .anyMatch(location -> location.startsWith(finalPattern)); + if (matches) { + break; + } } } return matches; diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java index 99bd964a..09932e71 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/NativeEnvironmentRepositoryTests.java @@ -259,8 +259,45 @@ public class NativeEnvironmentRepositoryTests { @Test public void testDefaultLabel() { this.repository.setDefaultLabel("test"); - assertThat(this.repository.findOne("foo", "default", null).getPropertySources().get(0).getSource().get("foo")) - .isEqualTo("test_bar"); + Environment environment = this.repository.findOne("foo", "default", null); + assertThat(environment.getPropertySources().get(0).getSource().get("foo")).isEqualTo("test_bar"); + } + + @Test + public void testImportVanilla() { + testImport(); + } + + @Test + public void testImportEmptySearchLocations() { + this.repository.setSearchLocations(); + testImport(); + } + + @Test + public void testImportPrefixedWithClasspath() { + this.repository.setSearchLocations("classpath:/test"); + testImport(); + } + + @Test + public void testImportPrefixedWithFile() { + this.repository.setSearchLocations("file:./src/test/resources/test"); + testImport(); + } + + @Test + public void testImportWithoutPrefix() { + this.repository.setSearchLocations("src/test/resources/test"); + testImport(); + } + + private void testImport() { + Environment environment = this.repository.findOne("import", "default", "master"); + // TODO should be 4, bar.yml contains 2 yaml documents + assertThat(environment.getPropertySources().size()).isEqualTo(3); + assertThat(environment.getPropertySources().get(0).getSource().get("foo")).isEqualTo("imported"); + assertThat(environment.getPropertySources().get(2).getSource().get("foo")).isEqualTo("importing"); } @Test diff --git a/spring-cloud-config-server/src/test/resources/import.yml b/spring-cloud-config-server/src/test/resources/import.yml new file mode 100644 index 00000000..65f711dd --- /dev/null +++ b/spring-cloud-config-server/src/test/resources/import.yml @@ -0,0 +1,5 @@ +foo: importing + +spring.config.import: + - configserver-test.yml + - test/import/foo.yml diff --git a/spring-cloud-config-server/src/test/resources/test/import.yml b/spring-cloud-config-server/src/test/resources/test/import.yml new file mode 100644 index 00000000..c2cac348 --- /dev/null +++ b/spring-cloud-config-server/src/test/resources/test/import.yml @@ -0,0 +1,5 @@ +foo: importing + +spring.config.import: + - bar.yml + - import/foo.yml diff --git a/spring-cloud-config-server/src/test/resources/test/import/foo.yml b/spring-cloud-config-server/src/test/resources/test/import/foo.yml new file mode 100644 index 00000000..82ba48c6 --- /dev/null +++ b/spring-cloud-config-server/src/test/resources/test/import/foo.yml @@ -0,0 +1 @@ +foo: imported