Skip to content

Commit bee6dd9

Browse files
Dave Syerphilwebb
authored andcommitted
Reverse priority of property sources when extracting sub properties
(cherry picked from commit 300e570) Fixes gh-2261 See gh-1259 Conflicts: spring-boot/src/test/java/org/springframework/boot/bind/RelaxedPropertyResolverTests.java
1 parent c08f912 commit bee6dd9

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

spring-boot/src/main/java/org/springframework/boot/bind/PropertySourceUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static Map<String, Object> getSubProperties(PropertySources propertySourc
6767
.getPropertyNames()) {
6868
String key = PropertySourceUtils.getSubKey(name, rootPrefix,
6969
keyPrefixes);
70-
if (key != null) {
70+
if (key != null && !subProperties.containsKey(key)) {
7171
subProperties.put(key, source.getProperty(name));
7272
}
7373
}

spring-boot/src/test/java/org/springframework/boot/bind/RelaxedPropertyResolverTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@
1818

1919
import java.util.LinkedHashMap;
2020
import java.util.Map;
21+
import java.util.Properties;
2122

2223
import org.junit.Before;
2324
import org.junit.Rule;
2425
import org.junit.Test;
2526
import org.junit.rules.ExpectedException;
2627
import org.springframework.core.env.MapPropertySource;
28+
import org.springframework.core.env.MutablePropertySources;
29+
import org.springframework.core.env.PropertiesPropertySource;
2730
import org.springframework.core.env.StandardEnvironment;
2831

2932
import static org.hamcrest.Matchers.equalTo;
3033
import static org.hamcrest.Matchers.nullValue;
34+
import static org.junit.Assert.assertEquals;
3135
import static org.junit.Assert.assertThat;
3236

3337
/**
@@ -174,4 +178,29 @@ public void subProperties() throws Exception {
174178
assertThat(subProperties.get("a.d"), equalTo((Object) "3"));
175179
}
176180

181+
@Test
182+
public void testPropertySource() throws Exception {
183+
Properties properties;
184+
PropertiesPropertySource propertySource;
185+
String propertyPrefix = "spring.datasource.";
186+
String propertyName = "password";
187+
String fullPropertyName = propertyPrefix + propertyName;
188+
StandardEnvironment environment = new StandardEnvironment();
189+
MutablePropertySources sources = environment.getPropertySources();
190+
properties = new Properties();
191+
properties.put(fullPropertyName, "systemPassword");
192+
propertySource = new PropertiesPropertySource("system", properties);
193+
sources.addLast(propertySource);
194+
properties = new Properties();
195+
properties.put(fullPropertyName, "propertiesPassword");
196+
propertySource = new PropertiesPropertySource("properties", properties);
197+
sources.addLast(propertySource);
198+
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(
199+
environment, propertyPrefix);
200+
String directProperty = propertyResolver.getProperty(propertyName);
201+
Map<String, Object> subProperties = propertyResolver.getSubProperties("");
202+
String subProperty = (String) subProperties.get(propertyName);
203+
assertEquals(directProperty, subProperty);
204+
}
205+
177206
}

0 commit comments

Comments
 (0)