Skip to content

Commit

Permalink
Allow configprops endpoint stringify primitive wrappers
Browse files Browse the repository at this point in the history
Update `ConfigurationPropertiesReportEndpoint` so that primitive
wrapper input values in the Environment are stringified for display.

Fixes gh-36076
  • Loading branch information
philwebb committed Jul 2, 2023
1 parent 4d1defd commit 51ee702
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ private Map<String, Object> getInput(ConfigurationProperty candidate, Object san
}

private Object stringifyIfNecessary(Object value) {
if (value == null || value.getClass().isPrimitive()) {
if (value == null || ClassUtils.isPrimitiveOrWrapper(value.getClass()) || value instanceof String) {
return value;
}
if (CharSequence.class.isAssignableFrom(value.getClass())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.mock.env.MockPropertySource;
import org.springframework.util.unit.DataSize;

Expand Down Expand Up @@ -143,6 +146,21 @@ void descriptorWithDurationProperty() {
.isEqualTo(Duration.ofSeconds(10).toString())));
}

@Test // gh-36076
void descriptorWithWrapperProperty() {
this.contextRunner.withUserConfiguration(TestPropertiesConfiguration.class).withInitializer((context) -> {
ConfigurableEnvironment environment = context.getEnvironment();
Map<String, Object> map = Collections.singletonMap("test.wrapper", 10);
PropertySource<?> propertySource = new MapPropertySource("test", map);
environment.getPropertySources().addLast(propertySource);
})
.run(assertProperties("test", (properties) -> assertThat(properties.get("wrapper")).isEqualTo(10),
(inputs) -> {
Map<String, Object> wrapper = (Map<String, Object>) inputs.get("wrapper");
assertThat(wrapper.get("value")).isEqualTo(10);
}));
}

@Test
void descriptorWithNonCamelCaseProperty() {
this.contextRunner.withUserConfiguration(MixedCasePropertiesConfiguration.class)
Expand Down Expand Up @@ -476,6 +494,8 @@ public static class TestProperties {

private String ignored = "dummy";

private Integer wrapper;

public String getDbPassword() {
return this.dbPassword;
}
Expand Down Expand Up @@ -512,6 +532,14 @@ public String getIgnored() {
return this.ignored;
}

public Integer getWrapper() {
return this.wrapper;
}

public void setWrapper(Integer wrapper) {
this.wrapper = wrapper;
}

}

@Configuration(proxyBeanMethods = false)
Expand Down

0 comments on commit 51ee702

Please sign in to comment.