Skip to content

Commit

Permalink
Handle wrapper types in Env actuator
Browse files Browse the repository at this point in the history
This commit modifies the actuator `EnvironmentEndpoint` to allow
primitive wrapper types to be serialized in the response data
structure.

Fixes gh-24307
  • Loading branch information
scottfrederick committed Dec 1, 2020
1 parent bd7e89b commit 01478a2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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())) {
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -199,7 +200,7 @@ void propertyWithSensitivePlaceholderNotResolved() {
}

@Test
void propertyWithTypeOtherThanStringShouldNotFail() {
void propertyWithComplexTypeShouldNotFail() {
ConfigurableEnvironment environment = emptyEnvironment();
environment.getPropertySources()
.addFirst(singleKeyPropertySource("test", "foo", Collections.singletonMap("bar", "baz")));
Expand All @@ -208,6 +209,23 @@ void propertyWithTypeOtherThanStringShouldNotFail() {
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();
Expand Down

0 comments on commit 01478a2

Please sign in to comment.