From e5f8078749b95a3f989b40c2196c30ec573617fc Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Tue, 3 Apr 2018 14:37:06 -0700 Subject: [PATCH] Support dots in System environment properties Fixes gh-12728 --- .../SpringConfigurationPropertySource.java | 49 ++++++++++++------- .../ConfigurationPropertiesTests.java | 12 +++++ 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySource.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySource.java index be02b03595..2d4a14da33 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySource.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SpringConfigurationPropertySource.java @@ -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 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 mappings = new ArrayList<>(); + for (PropertyMapper mapper : this.mappers) { + try { + mappings.addAll(Arrays.asList(mapper.map(propertySourceName))); + } + catch (Exception ex) { + + } } + return mappings.toArray(new PropertyMapping[] {}); } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java index 5cd7ec1d03..4feb8b450a 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java @@ -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()