Fixes multi-document file name clashes.

Multi-document yaml or properties files need unique names, otherwise lower precedence property sources will overwrite higher precedence ones. This adds a check to see if the original property source name contains 'document #'. If it does, that is used as the propety source name.

Fixes gh-1778
This commit is contained in:
spencergibb
2020-12-23 12:52:00 -05:00
parent ccb32d23fc
commit 3d38f7af0d
4 changed files with 54 additions and 4 deletions

View File

@@ -221,7 +221,8 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc
Environment result = new Environment(value.getName(), value.getProfiles(), value.getLabel(), this.version,
value.getState());
for (PropertySource source : value.getPropertySources()) {
String name = source.getName();
String originalName = source.getName();
String name = originalName;
if (this.environment.getPropertySources().contains(name)) {
continue;
}
@@ -271,13 +272,20 @@ public class NativeEnvironmentRepository implements EnvironmentRepository, Searc
if (!matches) {
// Don't include this one: it wasn't matched by our search locations
if (logger.isDebugEnabled()) {
logger.debug("Not adding property source: " + name);
logger.debug("Not adding property source: " + originalName);
}
continue;
}
}
logger.info("Adding property source: " + name);
result.add(new PropertySource(name, source.getSource()));
logger.info("Adding property source: " + originalName);
if (originalName.contains("document #")) {
// this is a multi-document file, use originalName for uniqueness.
result.add(new PropertySource(originalName, source.getSource()));
}
else {
// many other file tests rely on the mangled name
result.add(new PropertySource(name, source.getSource()));
}
}
return result;
}

View File

@@ -82,6 +82,31 @@ public class NativeEnvironmentRepositoryTests {
Environment environment = this.repository.findOne("foo", "development", "master");
assertThat(environment.getPropertySources().size()).isEqualTo(2);
assertThat(environment.getVersion()).as("version was wrong").isEqualTo("myversion");
// gh-1778 property sources has the same name.
assertThat(environment.getPropertySources().get(0).getName())
.isNotEqualTo(environment.getPropertySources().get(1).getName());
}
@Test
public void prefixedYaml() {
this.repository.setSearchLocations("classpath:/test");
Environment environment = this.repository.findOne("bar", "development", "master");
assertThat(environment.getPropertySources().size()).isEqualTo(2);
assertThat(environment.getVersion()).as("version was wrong").isEqualTo("myversion");
// gh-1778 property sources has the same name.
assertThat(environment.getPropertySources().get(0).getName())
.isNotEqualTo(environment.getPropertySources().get(1).getName());
}
@Test
public void prefixedMultiDocProperties() {
this.repository.setSearchLocations("classpath:/test");
Environment environment = this.repository.findOne("baz", "development", "master");
assertThat(environment.getPropertySources().size()).isEqualTo(2);
assertThat(environment.getVersion()).as("version was wrong").isEqualTo("myversion");
// gh-1778 property sources has the same name.
assertThat(environment.getPropertySources().get(0).getName())
.isNotEqualTo(environment.getPropertySources().get(1).getName());
}
@Test

View File

@@ -0,0 +1,12 @@
client:
test: Default
---
spring:
config:
activate:
on-profile: development
client:
test: Development

View File

@@ -0,0 +1,5 @@
client.test=Default
#---
spring.config.activate.on-profile=development
client.test=Development