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
This commit is contained in:
		
							parent
							
								
									bd7e89be1a
								
							
						
					
					
						commit
						01478a25b4
					
				|  | @ -44,6 +44,7 @@ import org.springframework.core.env.MutablePropertySources; | |||
| 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 class EnvironmentEndpoint { | |||
| 	} | ||||
| 
 | ||||
| 	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())) { | ||||
|  |  | |||
|  | @ -19,6 +19,7 @@ package org.springframework.boot.actuate.env; | |||
| 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 @@ class EnvironmentEndpointTests { | |||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	void propertyWithTypeOtherThanStringShouldNotFail() { | ||||
| 	void propertyWithComplexTypeShouldNotFail() { | ||||
| 		ConfigurableEnvironment environment = emptyEnvironment(); | ||||
| 		environment.getPropertySources() | ||||
| 				.addFirst(singleKeyPropertySource("test", "foo", Collections.singletonMap("bar", "baz"))); | ||||
|  | @ -208,6 +209,23 @@ class EnvironmentEndpointTests { | |||
| 		assertThat(value).isEqualTo("Complex property type java.util.Collections$SingletonMap"); | ||||
| 	} | ||||
| 
 | ||||
| 	@Test | ||||
| 	void propertyWithPrimitiveOrWrapperTypeIsHandledCorrectly() { | ||||
| 		ConfigurableEnvironment environment = emptyEnvironment(); | ||||
| 		Map<String, Object> 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<String, PropertyValueDescriptor> 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(); | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue