Support dots in System environment properties

Fixes gh-12728
pull/12776/head
Madhura Bhave 7 years ago
parent d14cd2cbae
commit e5f8078749

@ -16,6 +16,9 @@
package org.springframework.boot.context.properties.source; package org.springframework.boot.context.properties.source;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.function.Function; import java.util.function.Function;
@ -74,7 +77,7 @@ class SpringConfigurationPropertySource implements ConfigurationPropertySource {
Assert.notNull(propertySource, "PropertySource must not be null"); Assert.notNull(propertySource, "PropertySource must not be null");
Assert.notNull(mapper, "Mapper must not be null"); Assert.notNull(mapper, "Mapper must not be null");
this.propertySource = propertySource; this.propertySource = propertySource;
this.mapper = new ExceptionSwallowingPropertyMapper(mapper); this.mapper = mapper;
this.containsDescendantOf = (containsDescendantOf != null ? containsDescendantOf this.containsDescendantOf = (containsDescendantOf != null ? containsDescendantOf
: (n) -> ConfigurationPropertyState.UNKNOWN); : (n) -> ConfigurationPropertyState.UNKNOWN);
} }
@ -156,9 +159,10 @@ class SpringConfigurationPropertySource implements ConfigurationPropertySource {
private static PropertyMapper getPropertyMapper(PropertySource<?> source) { private static PropertyMapper getPropertyMapper(PropertySource<?> source) {
if (source instanceof SystemEnvironmentPropertySource if (source instanceof SystemEnvironmentPropertySource
&& hasSystemEnvironmentName(source)) { && hasSystemEnvironmentName(source)) {
return SystemEnvironmentPropertyMapper.INSTANCE; return new DelegatingPropertyMapper(SystemEnvironmentPropertyMapper.INSTANCE,
DefaultPropertyMapper.INSTANCE);
} }
return DefaultPropertyMapper.INSTANCE; return new DelegatingPropertyMapper(DefaultPropertyMapper.INSTANCE);
} }
private static boolean hasSystemEnvironmentName(PropertySource<?> source) { private static boolean hasSystemEnvironmentName(PropertySource<?> source) {
@ -207,35 +211,44 @@ class SpringConfigurationPropertySource implements ConfigurationPropertySource {
} }
/** /**
* {@link PropertyMapper} that swallows exceptions when the mapping fails. * {@link PropertyMapper} that delegates to other {@link PropertyMapper}s.
* It also swallows exceptions when the mapping fails.
*/ */
private static class ExceptionSwallowingPropertyMapper implements PropertyMapper { private static class DelegatingPropertyMapper implements PropertyMapper {
private final PropertyMapper mapper; private final PropertyMapper[] mappers;
ExceptionSwallowingPropertyMapper(PropertyMapper mapper) { DelegatingPropertyMapper(PropertyMapper... mappers) {
this.mapper = mapper; this.mappers = mappers;
} }
@Override @Override
public PropertyMapping[] map( public PropertyMapping[] map(
ConfigurationPropertyName configurationPropertyName) { ConfigurationPropertyName configurationPropertyName) {
try { List<PropertyMapping> mappings = new ArrayList<>();
return this.mapper.map(configurationPropertyName); for (PropertyMapper mapper : this.mappers) {
} try {
catch (Exception ex) { mappings.addAll(Arrays.asList(mapper.map(configurationPropertyName)));
return NO_MAPPINGS; }
catch (Exception ex) {
}
} }
return mappings.toArray(new PropertyMapping[] {});
} }
@Override @Override
public PropertyMapping[] map(String propertySourceName) { public PropertyMapping[] map(String propertySourceName) {
try { List<PropertyMapping> mappings = new ArrayList<>();
return this.mapper.map(propertySourceName); for (PropertyMapper mapper : this.mappers) {
} try {
catch (Exception ex) { mappings.addAll(Arrays.asList(mapper.map(propertySourceName)));
return NO_MAPPINGS; }
catch (Exception ex) {
}
} }
return mappings.toArray(new PropertyMapping[] {});
} }
} }

@ -461,6 +461,18 @@ public class ConfigurationPropertiesTests {
assertThat(bean.getMap().get("foo")).containsOnly(entry("bar", "baz")); assertThat(bean.getMap().get("foo")).containsOnly(entry("bar", "baz"));
} }
@Test
public void loadWhenDotsInSystemEnvironmentPropertiesShouldBind() {
this.context.getEnvironment().getPropertySources()
.addLast(new SystemEnvironmentPropertySource(
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
Collections.singletonMap("com.example.bar", "baz")));
load(SimplePrefixedProperties.class);
SimplePrefixedProperties bean = this.context
.getBean(SimplePrefixedProperties.class);
assertThat(bean.getBar()).isEqualTo("baz");
}
@Test @Test
public void loadWhenOverridingPropertiesShouldBind() { public void loadWhenOverridingPropertiesShouldBind() {
MutablePropertySources sources = this.context.getEnvironment() MutablePropertySources sources = this.context.getEnvironment()

Loading…
Cancel
Save