Fix spring.config.import for environment type native (#1990)
* fix for 'spring.config.import' from subdirectory of 'spring.cloud.config.server' search-locations
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
5
spring-cloud-config-server/src/test/resources/import.yml
Normal file
5
spring-cloud-config-server/src/test/resources/import.yml
Normal file
@@ -0,0 +1,5 @@
|
||||
foo: importing
|
||||
|
||||
spring.config.import:
|
||||
- configserver-test.yml
|
||||
- test/import/foo.yml
|
||||
@@ -0,0 +1,5 @@
|
||||
foo: importing
|
||||
|
||||
spring.config.import:
|
||||
- bar.yml
|
||||
- import/foo.yml
|
||||
@@ -0,0 +1 @@
|
||||
foo: imported
|
||||
Reference in New Issue
Block a user