Skip to content

Commit 276a9a0

Browse files
committed
Reflect each source's values in env endpoint's response
Previously, the env endpoint would use the entire environment to get the value of each property in a source. This meant that when there were multiple sources with the same property, the value from the source with the highest precedence would be used for every source that contains the property. This commit update the endpoint to retrieve the value from the property source that is being described, rather than resolving it against all the environment's property sources. Closes gh-10883
1 parent 5cf2e76 commit 276a9a0

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpoint.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ public Map<String, Object> invoke() {
6969
EnumerablePropertySource<?> enumerable = (EnumerablePropertySource<?>) source;
7070
Map<String, Object> properties = new LinkedHashMap<String, Object>();
7171
for (String name : enumerable.getPropertyNames()) {
72-
Object resolved = resolver.getProperty(name, Object.class);
72+
Object property = source.getProperty(name);
73+
Object resolved = property instanceof String
74+
? resolver.resolvePlaceholders((String) property) : property;
7375
properties.put(name, sanitize(name, resolved));
7476
}
7577
properties = postProcessSourceProperties(sourceName, properties);

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/EnvironmentEndpointTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,26 @@ public void propertyWithTypeOtherThanStringShouldNotFail() throws Exception {
279279
assertThat(foo.get("bar")).isEqualTo("baz");
280280
}
281281

282+
@SuppressWarnings("unchecked")
283+
@Test
284+
public void multipleSourcesWithSameProperty() {
285+
this.context = new AnnotationConfigApplicationContext();
286+
MutablePropertySources propertySources = this.context.getEnvironment()
287+
.getPropertySources();
288+
propertySources.addFirst(new MapPropertySource("one",
289+
Collections.<String, Object>singletonMap("a", "alpha")));
290+
propertySources.addFirst(new MapPropertySource("two",
291+
Collections.<String, Object>singletonMap("a", "apple")));
292+
this.context.register(Config.class);
293+
this.context.refresh();
294+
EnvironmentEndpoint report = getEndpointBean();
295+
Map<String, Object> env = report.invoke();
296+
Map<String, Object> sourceOne = (Map<String, Object>) env.get("one");
297+
assertThat(sourceOne).containsEntry("a", "alpha");
298+
Map<String, Object> sourceTwo = (Map<String, Object>) env.get("two");
299+
assertThat(sourceTwo).containsEntry("a", "apple");
300+
}
301+
282302
private void clearSystemProperties(String... properties) {
283303
for (String property : properties) {
284304
System.clearProperty(property);

0 commit comments

Comments
 (0)