From 67068fc83df29ffa0346b3f10dd142f6217ec694 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Mon, 6 Mar 2017 16:06:42 -0800 Subject: [PATCH] Ignore unresolvable nested placeholders The NamePatternEnvironmentFilter used to throw an exception if placeholders in property values could not be resolved. Fixes gh-8510 --- .../actuate/endpoint/mvc/EnvironmentMvcEndpoint.java | 11 ++++++++++- .../endpoint/mvc/EnvironmentMvcEndpointTests.java | 10 ++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EnvironmentMvcEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EnvironmentMvcEndpoint.java index 840232405af..465af6dd356 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EnvironmentMvcEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EnvironmentMvcEndpoint.java @@ -22,8 +22,10 @@ import org.springframework.context.EnvironmentAware; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.EnumerablePropertySource; import org.springframework.core.env.Environment; +import org.springframework.core.env.PropertyResolver; import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySources; +import org.springframework.core.env.PropertySourcesPropertyResolver; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; @@ -95,7 +97,14 @@ public class EnvironmentMvcEndpoint extends EndpointMvcAdapter @Override protected Object getOptionalValue(Environment source, String name) { - Object result = source.getProperty(name); + PropertyResolver resolver = source; + if (source instanceof ConfigurableEnvironment) { + resolver = new PropertySourcesPropertyResolver( + ((ConfigurableEnvironment) source).getPropertySources()); + ((PropertySourcesPropertyResolver) resolver) + .setIgnoreUnresolvableNestedPlaceholders(true); + } + Object result = resolver.getProperty(name); if (result != null) { result = ((EnvironmentEndpoint) getDelegate()).sanitize(name, result); } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/EnvironmentMvcEndpointTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/EnvironmentMvcEndpointTests.java index 5b91e61886d..82701d0a36f 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/EnvironmentMvcEndpointTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/mvc/EnvironmentMvcEndpointTests.java @@ -102,6 +102,16 @@ public class EnvironmentMvcEndpointTests { .andExpect(content().string(containsString("\"fool\":\"baz\""))); } + @Test + public void nestedPathWhenPlaceholderCannotBeResolvedShouldReturnUnresolvedProperty() throws Exception { + Map map = new HashMap(); + map.put("my.foo", "${my.bar}"); + ((ConfigurableEnvironment) this.context.getEnvironment()).getPropertySources() + .addFirst(new MapPropertySource("unresolved-placeholder", map)); + this.mvc.perform(get("/env/my.*")).andExpect(status().isOk()) + .andExpect(content().string(containsString("\"my.foo\":\"${my.bar}\""))); + } + @Configuration @Import({ JacksonAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, WebMvcAutoConfiguration.class,