Show endpoint.isEnabled in /configprops
Update `ConfigurationPropertiesReportEndpoint` so that properties that are set with a Boolean class but read with a boolean primitive still appear in the report. The allows the Endpoint.isEnabled() property to be displayed. Fixes gh-2929
This commit is contained in:
@@ -324,9 +324,7 @@ public class ConfigurationPropertiesReportEndpoint extends
|
||||
private boolean isReadable(BeanDescription beanDesc, BeanPropertyWriter writer) {
|
||||
String parentType = beanDesc.getType().getRawClass().getName();
|
||||
String type = writer.getPropertyType().getName();
|
||||
AnnotatedMethod setter = beanDesc.findMethod(
|
||||
"set" + StringUtils.capitalize(writer.getName()),
|
||||
new Class<?>[] { writer.getPropertyType() });
|
||||
AnnotatedMethod setter = findSetter(beanDesc, writer);
|
||||
// If there's a setter, we assume it's OK to report on the value,
|
||||
// similarly, if there's no setter but the package names match, we assume
|
||||
// that its a nested class used solely for binding to config props, so it
|
||||
@@ -336,6 +334,19 @@ public class ConfigurationPropertiesReportEndpoint extends
|
||||
|| ClassUtils.getPackageName(parentType).equals(
|
||||
ClassUtils.getPackageName(type));
|
||||
}
|
||||
|
||||
private AnnotatedMethod findSetter(BeanDescription beanDesc,
|
||||
BeanPropertyWriter writer) {
|
||||
String name = "set" + StringUtils.capitalize(writer.getName());
|
||||
Class<?> type = writer.getPropertyType();
|
||||
AnnotatedMethod setter = beanDesc.findMethod(name, new Class<?>[] { type });
|
||||
// The enabled property of endpoints returns a boolean primitive but is set
|
||||
// using a Boolean class
|
||||
if (setter == null && type.equals(Boolean.TYPE)) {
|
||||
setter = beanDesc.findMethod(name, new Class<?>[] { Boolean.class });
|
||||
}
|
||||
return setter;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,6 +26,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.greaterThan;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
@@ -151,6 +152,17 @@ public class ConfigurationPropertiesReportEndpointTests extends
|
||||
assertEquals("******", nestedProperties.get("myTestProperty"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("unchecked")
|
||||
public void mixedBoolean() throws Exception {
|
||||
ConfigurationPropertiesReportEndpoint report = getEndpointBean();
|
||||
Map<String, Object> properties = report.invoke();
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) ((Map<String, Object>) properties
|
||||
.get("testProperties")).get("properties");
|
||||
System.out.println(nestedProperties);
|
||||
assertThat(nestedProperties.get("mixedBoolean"), equalTo((Object) true));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
public static class Parent {
|
||||
@@ -183,6 +195,8 @@ public class ConfigurationPropertiesReportEndpointTests extends
|
||||
|
||||
private String myTestProperty = "654321";
|
||||
|
||||
private Boolean mixedBoolean = true;
|
||||
|
||||
public String getDbPassword() {
|
||||
return this.dbPassword;
|
||||
}
|
||||
@@ -199,5 +213,13 @@ public class ConfigurationPropertiesReportEndpointTests extends
|
||||
this.myTestProperty = myTestProperty;
|
||||
}
|
||||
|
||||
public boolean isMixedBoolean() {
|
||||
return (this.mixedBoolean == null ? false : this.mixedBoolean);
|
||||
}
|
||||
|
||||
public void setMixedBoolean(Boolean mixedBoolean) {
|
||||
this.mixedBoolean = mixedBoolean;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user