From 01478a25b482f0d98ce537563572d025c452304d Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Tue, 1 Dec 2020 15:12:13 -0600 Subject: [PATCH] Handle wrapper types in Env actuator This commit modifies the actuator `EnvironmentEndpoint` to allow primitive wrapper types to be serialized in the response data structure. Fixes gh-24307 --- .../boot/actuate/env/EnvironmentEndpoint.java | 4 +++- .../actuate/env/EnvironmentEndpointTests.java | 20 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/env/EnvironmentEndpoint.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/env/EnvironmentEndpoint.java index 4c1be68dad3f..d7f0a494f107 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/env/EnvironmentEndpoint.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/env/EnvironmentEndpoint.java @@ -44,6 +44,7 @@ import org.springframework.core.env.PropertySource; import org.springframework.core.env.StandardEnvironment; import org.springframework.lang.Nullable; +import org.springframework.util.ClassUtils; import org.springframework.util.PropertyPlaceholderHelper; import org.springframework.util.StringUtils; import org.springframework.util.SystemPropertyUtils; @@ -184,7 +185,8 @@ public Object sanitize(String name, Object object) { } protected Object stringifyIfNecessary(Object value) { - if (value == null || value.getClass().isPrimitive()) { + if (value == null || ClassUtils.isPrimitiveOrWrapper(value.getClass()) + || Number.class.isAssignableFrom(value.getClass())) { return value; } if (CharSequence.class.isAssignableFrom(value.getClass())) { diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/env/EnvironmentEndpointTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/env/EnvironmentEndpointTests.java index 313a67f312af..acc99906bc9d 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/env/EnvironmentEndpointTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/env/EnvironmentEndpointTests.java @@ -19,6 +19,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; +import java.math.BigInteger; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; @@ -199,7 +200,7 @@ void propertyWithSensitivePlaceholderNotResolved() { } @Test - void propertyWithTypeOtherThanStringShouldNotFail() { + void propertyWithComplexTypeShouldNotFail() { ConfigurableEnvironment environment = emptyEnvironment(); environment.getPropertySources() .addFirst(singleKeyPropertySource("test", "foo", Collections.singletonMap("bar", "baz"))); @@ -208,6 +209,23 @@ void propertyWithTypeOtherThanStringShouldNotFail() { assertThat(value).isEqualTo("Complex property type java.util.Collections$SingletonMap"); } + @Test + void propertyWithPrimitiveOrWrapperTypeIsHandledCorrectly() { + ConfigurableEnvironment environment = emptyEnvironment(); + Map map = new LinkedHashMap<>(); + map.put("char", 'a'); + map.put("integer", 100); + map.put("boolean", true); + map.put("biginteger", BigInteger.valueOf(200)); + environment.getPropertySources().addFirst(new MapPropertySource("test", map)); + EnvironmentDescriptor descriptor = new EnvironmentEndpoint(environment).environment(null); + Map properties = propertySources(descriptor).get("test").getProperties(); + assertThat(properties.get("char").getValue()).isEqualTo('a'); + assertThat(properties.get("integer").getValue()).isEqualTo(100); + assertThat(properties.get("boolean").getValue()).isEqualTo(true); + assertThat(properties.get("biginteger").getValue()).isEqualTo(BigInteger.valueOf(200)); + } + @Test void propertyWithCharSequenceTypeIsConvertedToString() throws Exception { ConfigurableEnvironment environment = emptyEnvironment();