Skip to content

Commit 01478a2

Browse files
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
1 parent bd7e89b commit 01478a2

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.core.env.PropertySource;
4545
import org.springframework.core.env.StandardEnvironment;
4646
import org.springframework.lang.Nullable;
47+
import org.springframework.util.ClassUtils;
4748
import org.springframework.util.PropertyPlaceholderHelper;
4849
import org.springframework.util.StringUtils;
4950
import org.springframework.util.SystemPropertyUtils;
@@ -184,7 +185,8 @@ public Object sanitize(String name, Object object) {
184185
}
185186

186187
protected Object stringifyIfNecessary(Object value) {
187-
if (value == null || value.getClass().isPrimitive()) {
188+
if (value == null || ClassUtils.isPrimitiveOrWrapper(value.getClass())
189+
|| Number.class.isAssignableFrom(value.getClass())) {
188190
return value;
189191
}
190192
if (CharSequence.class.isAssignableFrom(value.getClass())) {

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.ByteArrayInputStream;
2020
import java.io.IOException;
2121
import java.io.InputStream;
22+
import java.math.BigInteger;
2223
import java.util.Collections;
2324
import java.util.LinkedHashMap;
2425
import java.util.Map;
@@ -199,7 +200,7 @@ void propertyWithSensitivePlaceholderNotResolved() {
199200
}
200201

201202
@Test
202-
void propertyWithTypeOtherThanStringShouldNotFail() {
203+
void propertyWithComplexTypeShouldNotFail() {
203204
ConfigurableEnvironment environment = emptyEnvironment();
204205
environment.getPropertySources()
205206
.addFirst(singleKeyPropertySource("test", "foo", Collections.singletonMap("bar", "baz")));
@@ -208,6 +209,23 @@ void propertyWithTypeOtherThanStringShouldNotFail() {
208209
assertThat(value).isEqualTo("Complex property type java.util.Collections$SingletonMap");
209210
}
210211

212+
@Test
213+
void propertyWithPrimitiveOrWrapperTypeIsHandledCorrectly() {
214+
ConfigurableEnvironment environment = emptyEnvironment();
215+
Map<String, Object> map = new LinkedHashMap<>();
216+
map.put("char", 'a');
217+
map.put("integer", 100);
218+
map.put("boolean", true);
219+
map.put("biginteger", BigInteger.valueOf(200));
220+
environment.getPropertySources().addFirst(new MapPropertySource("test", map));
221+
EnvironmentDescriptor descriptor = new EnvironmentEndpoint(environment).environment(null);
222+
Map<String, PropertyValueDescriptor> properties = propertySources(descriptor).get("test").getProperties();
223+
assertThat(properties.get("char").getValue()).isEqualTo('a');
224+
assertThat(properties.get("integer").getValue()).isEqualTo(100);
225+
assertThat(properties.get("boolean").getValue()).isEqualTo(true);
226+
assertThat(properties.get("biginteger").getValue()).isEqualTo(BigInteger.valueOf(200));
227+
}
228+
211229
@Test
212230
void propertyWithCharSequenceTypeIsConvertedToString() throws Exception {
213231
ConfigurableEnvironment environment = emptyEnvironment();

0 commit comments

Comments
 (0)