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;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.function.Function;
@ -74,7 +77,7 @@ class SpringConfigurationPropertySource implements ConfigurationPropertySource {
Assert.notNull(propertySource, "PropertySource must not be null");
Assert.notNull(mapper, "Mapper must not be null");
this.propertySource = propertySource;
this.mapper = new ExceptionSwallowingPropertyMapper(mapper);
this.mapper = mapper;
this.containsDescendantOf = (containsDescendantOf != null ? containsDescendantOf
: (n) -> ConfigurationPropertyState.UNKNOWN);
}
@ -156,9 +159,10 @@ class SpringConfigurationPropertySource implements ConfigurationPropertySource {
private static PropertyMapper getPropertyMapper(PropertySource<?> source) {
if (source instanceof SystemEnvironmentPropertySource
&& 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) {
@ -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) {
this.mapper = mapper;
DelegatingPropertyMapper(PropertyMapper... mappers) {
this.mappers = mappers;
}
@Override
public PropertyMapping[] map(
ConfigurationPropertyName configurationPropertyName) {
try {
return this.mapper.map(configurationPropertyName);
}
catch (Exception ex) {
return NO_MAPPINGS;
List<PropertyMapping> mappings = new ArrayList<>();
for (PropertyMapper mapper : this.mappers) {
try {
mappings.addAll(Arrays.asList(mapper.map(configurationPropertyName)));
}
catch (Exception ex) {
}
}
return mappings.toArray(new PropertyMapping[] {});
}
@Override
public PropertyMapping[] map(String propertySourceName) {
try {
return this.mapper.map(propertySourceName);
}
catch (Exception ex) {
return NO_MAPPINGS;
List<PropertyMapping> mappings = new ArrayList<>();
for (PropertyMapper mapper : this.mappers) {
try {
mappings.addAll(Arrays.asList(mapper.map(propertySourceName)));
}
catch (Exception ex) {
}
}
return mappings.toArray(new PropertyMapping[] {});
}
}

@ -461,6 +461,18 @@ public class ConfigurationPropertiesTests {
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
public void loadWhenOverridingPropertiesShouldBind() {
MutablePropertySources sources = this.context.getEnvironment()

Loading…
Cancel
Save