Skip to content

Commit 1d5a62b

Browse files
committed
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
1 parent 61bc876 commit 1d5a62b

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpoint.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,7 @@ public List<BeanPropertyWriter> changeProperties(SerializationConfig config,
324324
private boolean isReadable(BeanDescription beanDesc, BeanPropertyWriter writer) {
325325
String parentType = beanDesc.getType().getRawClass().getName();
326326
String type = writer.getPropertyType().getName();
327-
AnnotatedMethod setter = beanDesc.findMethod(
328-
"set" + StringUtils.capitalize(writer.getName()),
329-
new Class<?>[] { writer.getPropertyType() });
327+
AnnotatedMethod setter = findSetter(beanDesc, writer);
330328
// If there's a setter, we assume it's OK to report on the value,
331329
// similarly, if there's no setter but the package names match, we assume
332330
// that its a nested class used solely for binding to config props, so it
@@ -336,6 +334,19 @@ private boolean isReadable(BeanDescription beanDesc, BeanPropertyWriter writer)
336334
|| ClassUtils.getPackageName(parentType).equals(
337335
ClassUtils.getPackageName(type));
338336
}
337+
338+
private AnnotatedMethod findSetter(BeanDescription beanDesc,
339+
BeanPropertyWriter writer) {
340+
String name = "set" + StringUtils.capitalize(writer.getName());
341+
Class<?> type = writer.getPropertyType();
342+
AnnotatedMethod setter = beanDesc.findMethod(name, new Class<?>[] { type });
343+
// The enabled property of endpoints returns a boolean primitive but is set
344+
// using a Boolean class
345+
if (setter == null && type.equals(Boolean.TYPE)) {
346+
setter = beanDesc.findMethod(name, new Class<?>[] { Boolean.class });
347+
}
348+
return setter;
349+
}
339350
}
340351

341352
/**

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.context.annotation.Bean;
2727
import org.springframework.context.annotation.Configuration;
2828

29+
import static org.hamcrest.Matchers.equalTo;
2930
import static org.hamcrest.Matchers.greaterThan;
3031
import static org.junit.Assert.assertEquals;
3132
import static org.junit.Assert.assertNotNull;
@@ -151,6 +152,17 @@ public void testKeySanitizationWithCustomPatternAndKeyByEnvironment()
151152
assertEquals("******", nestedProperties.get("myTestProperty"));
152153
}
153154

155+
@Test
156+
@SuppressWarnings("unchecked")
157+
public void mixedBoolean() throws Exception {
158+
ConfigurationPropertiesReportEndpoint report = getEndpointBean();
159+
Map<String, Object> properties = report.invoke();
160+
Map<String, Object> nestedProperties = (Map<String, Object>) ((Map<String, Object>) properties
161+
.get("testProperties")).get("properties");
162+
System.out.println(nestedProperties);
163+
assertThat(nestedProperties.get("mixedBoolean"), equalTo((Object) true));
164+
}
165+
154166
@Configuration
155167
@EnableConfigurationProperties
156168
public static class Parent {
@@ -183,6 +195,8 @@ public static class TestProperties {
183195

184196
private String myTestProperty = "654321";
185197

198+
private Boolean mixedBoolean = true;
199+
186200
public String getDbPassword() {
187201
return this.dbPassword;
188202
}
@@ -199,5 +213,13 @@ public void setMyTestProperty(String myTestProperty) {
199213
this.myTestProperty = myTestProperty;
200214
}
201215

216+
public boolean isMixedBoolean() {
217+
return (this.mixedBoolean == null ? false : this.mixedBoolean);
218+
}
219+
220+
public void setMixedBoolean(Boolean mixedBoolean) {
221+
this.mixedBoolean = mixedBoolean;
222+
}
223+
202224
}
203225
}

0 commit comments

Comments
 (0)