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
pull/3241/head
Phillip Webb 10 years ago
parent 61bc876ae8
commit 1d5a62b3df

@ -324,9 +324,7 @@ public class ConfigurationPropertiesReportEndpoint extends
private boolean isReadable(BeanDescription beanDesc, BeanPropertyWriter writer) { private boolean isReadable(BeanDescription beanDesc, BeanPropertyWriter writer) {
String parentType = beanDesc.getType().getRawClass().getName(); String parentType = beanDesc.getType().getRawClass().getName();
String type = writer.getPropertyType().getName(); String type = writer.getPropertyType().getName();
AnnotatedMethod setter = beanDesc.findMethod( AnnotatedMethod setter = findSetter(beanDesc, writer);
"set" + StringUtils.capitalize(writer.getName()),
new Class<?>[] { writer.getPropertyType() });
// If there's a setter, we assume it's OK to report on the value, // 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 // 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 // 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(parentType).equals(
ClassUtils.getPackageName(type)); 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.greaterThan;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
@ -151,6 +152,17 @@ public class ConfigurationPropertiesReportEndpointTests extends
assertEquals("******", nestedProperties.get("myTestProperty")); 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 @Configuration
@EnableConfigurationProperties @EnableConfigurationProperties
public static class Parent { public static class Parent {
@ -183,6 +195,8 @@ public class ConfigurationPropertiesReportEndpointTests extends
private String myTestProperty = "654321"; private String myTestProperty = "654321";
private Boolean mixedBoolean = true;
public String getDbPassword() { public String getDbPassword() {
return this.dbPassword; return this.dbPassword;
} }
@ -199,5 +213,13 @@ public class ConfigurationPropertiesReportEndpointTests extends
this.myTestProperty = myTestProperty; this.myTestProperty = myTestProperty;
} }
public boolean isMixedBoolean() {
return (this.mixedBoolean == null ? false : this.mixedBoolean);
}
public void setMixedBoolean(Boolean mixedBoolean) {
this.mixedBoolean = mixedBoolean;
}
} }
} }

Loading…
Cancel
Save