Limit when SystemEnvironment mapping is used
Update `SpringConfigurationPropertySource` so that the `SystemEnvironmentPropertyMapper` is only used for the "actual" system environment property source. This allows SystemEnvironmentProperySource class to be used for other purposes (for example, Spring Cloud uses it to as an override source providing decryption). Only property sources named `systemEnvironment` or ending with `-systemEnvironment` now have the `SystemEnvironmentPropertyMapper` applied. The `TestPropertyValues` has been retrofitted to name the source it adds appropriately. Fixes gh-10840
This commit is contained in:
@@ -34,6 +34,7 @@ import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.core.env.StandardEnvironment;
|
||||
import org.springframework.core.env.SystemEnvironmentPropertySource;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -98,7 +99,7 @@ public final class TestPropertyValues {
|
||||
* @param type the type of {@link PropertySource} to be added. See {@link Type}
|
||||
*/
|
||||
public void applyTo(ConfigurableEnvironment environment, Type type) {
|
||||
applyTo(environment, type, "test");
|
||||
applyTo(environment, type, type.applySuffix("test"));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -212,23 +213,31 @@ public final class TestPropertyValues {
|
||||
/**
|
||||
* Used for {@link SystemEnvironmentPropertySource}.
|
||||
*/
|
||||
SYSTEM(SystemEnvironmentPropertySource.class),
|
||||
SYSTEM_ENVIRONMENT(SystemEnvironmentPropertySource.class,
|
||||
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME),
|
||||
|
||||
/**
|
||||
* Used for {@link MapPropertySource}.
|
||||
*/
|
||||
MAP(MapPropertySource.class);
|
||||
MAP(MapPropertySource.class, null);
|
||||
|
||||
private Class<? extends MapPropertySource> sourceClass;
|
||||
private final Class<? extends MapPropertySource> sourceClass;
|
||||
|
||||
Type(Class<? extends MapPropertySource> sourceClass) {
|
||||
private final String suffix;
|
||||
|
||||
Type(Class<? extends MapPropertySource> sourceClass, String suffix) {
|
||||
this.sourceClass = sourceClass;
|
||||
this.suffix = (suffix == null ? null : "-" + suffix);
|
||||
}
|
||||
|
||||
public Class<? extends MapPropertySource> getSourceClass() {
|
||||
return this.sourceClass;
|
||||
}
|
||||
|
||||
protected String applySuffix(String name) {
|
||||
return (this.suffix == null ? name : name + "-" + this.suffix);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -54,8 +54,10 @@ public class TestPropertyValuesTests {
|
||||
|
||||
@Test
|
||||
public void applyToSystemPropertySource() throws Exception {
|
||||
TestPropertyValues.of("FOO_BAR=BAZ").applyTo(this.environment, Type.SYSTEM);
|
||||
TestPropertyValues.of("FOO_BAR=BAZ").applyTo(this.environment, Type.SYSTEM_ENVIRONMENT);
|
||||
assertThat(this.environment.getProperty("foo.bar")).isEqualTo("BAZ");
|
||||
assertThat(this.environment.getPropertySources().contains(
|
||||
"test-" + StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -70,7 +72,7 @@ public class TestPropertyValuesTests {
|
||||
throws Exception {
|
||||
TestPropertyValues.of("foo.bar=baz", "hello.world=hi").applyTo(this.environment,
|
||||
Type.MAP, "other");
|
||||
TestPropertyValues.of("FOO_BAR=BAZ").applyTo(this.environment, Type.SYSTEM,
|
||||
TestPropertyValues.of("FOO_BAR=BAZ").applyTo(this.environment, Type.SYSTEM_ENVIRONMENT,
|
||||
"other");
|
||||
assertThat(this.environment.getPropertySources().get("other"))
|
||||
.isInstanceOf(SystemEnvironmentPropertySource.class);
|
||||
|
||||
Reference in New Issue
Block a user