Skip to content

Commit 67068fc

Browse files
committed
Ignore unresolvable nested placeholders
The NamePatternEnvironmentFilter used to throw an exception if placeholders in property values could not be resolved. Fixes gh-8510
1 parent f5aeac3 commit 67068fc

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
import org.springframework.core.env.ConfigurableEnvironment;
2323
import org.springframework.core.env.EnumerablePropertySource;
2424
import org.springframework.core.env.Environment;
25+
import org.springframework.core.env.PropertyResolver;
2526
import org.springframework.core.env.PropertySource;
2627
import org.springframework.core.env.PropertySources;
28+
import org.springframework.core.env.PropertySourcesPropertyResolver;
2729
import org.springframework.http.HttpStatus;
2830
import org.springframework.http.MediaType;
2931
import org.springframework.web.bind.annotation.GetMapping;
@@ -95,7 +97,14 @@ private void getNames(PropertySources propertySources, NameCallback callback) {
9597

9698
@Override
9799
protected Object getOptionalValue(Environment source, String name) {
98-
Object result = source.getProperty(name);
100+
PropertyResolver resolver = source;
101+
if (source instanceof ConfigurableEnvironment) {
102+
resolver = new PropertySourcesPropertyResolver(
103+
((ConfigurableEnvironment) source).getPropertySources());
104+
((PropertySourcesPropertyResolver) resolver)
105+
.setIgnoreUnresolvableNestedPlaceholders(true);
106+
}
107+
Object result = resolver.getProperty(name);
99108
if (result != null) {
100109
result = ((EnvironmentEndpoint) getDelegate()).sanitize(name, result);
101110
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ public void regex() throws Exception {
102102
.andExpect(content().string(containsString("\"fool\":\"baz\"")));
103103
}
104104

105+
@Test
106+
public void nestedPathWhenPlaceholderCannotBeResolvedShouldReturnUnresolvedProperty() throws Exception {
107+
Map<String, Object> map = new HashMap<String, Object>();
108+
map.put("my.foo", "${my.bar}");
109+
((ConfigurableEnvironment) this.context.getEnvironment()).getPropertySources()
110+
.addFirst(new MapPropertySource("unresolved-placeholder", map));
111+
this.mvc.perform(get("/env/my.*")).andExpect(status().isOk())
112+
.andExpect(content().string(containsString("\"my.foo\":\"${my.bar}\"")));
113+
}
114+
105115
@Configuration
106116
@Import({ JacksonAutoConfiguration.class,
107117
HttpMessageConvertersAutoConfiguration.class, WebMvcAutoConfiguration.class,

0 commit comments

Comments
 (0)