Skip to content

Commit 300e570

Browse files
author
Dave Syer
committed
Reverse priority of property sources when extracting sub properties
Fixes gh-1259
1 parent a9b8563 commit 300e570

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
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: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,24 @@
1616

1717
package org.springframework.boot.bind;
1818

19+
import static org.hamcrest.Matchers.equalTo;
20+
import static org.hamcrest.Matchers.nullValue;
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertThat;
23+
1924
import java.util.LinkedHashMap;
2025
import java.util.Map;
26+
import java.util.Properties;
2127

2228
import org.junit.Before;
2329
import org.junit.Rule;
2430
import org.junit.Test;
2531
import org.junit.rules.ExpectedException;
2632
import org.springframework.core.env.MapPropertySource;
33+
import org.springframework.core.env.MutablePropertySources;
34+
import org.springframework.core.env.PropertiesPropertySource;
2735
import org.springframework.core.env.StandardEnvironment;
2836

29-
import static org.hamcrest.Matchers.equalTo;
30-
import static org.hamcrest.Matchers.nullValue;
31-
import static org.junit.Assert.assertThat;
32-
3337
/**
3438
* Tests for {@link RelaxedPropertyResolver}.
3539
*
@@ -173,4 +177,30 @@ public void subProperties() throws Exception {
173177
assertThat(subProperties.get("a.c"), equalTo((Object) "2"));
174178
assertThat(subProperties.get("a.d"), equalTo((Object) "3"));
175179
}
180+
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+
176206
}

0 commit comments

Comments
 (0)