Skip to content

Commit 3762d39

Browse files
committed
Polish "Unwrap AOP proxies in configprops endpoint serialization"
See gh-50273
1 parent 592e0b9 commit 3762d39

2 files changed

Lines changed: 29 additions & 51 deletions

File tree

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/context/properties/ConfigurationPropertiesReportEndpoint.java

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.LinkedHashMap;
2626
import java.util.List;
2727
import java.util.Map;
28-
import java.util.function.Function;
2928
import java.util.function.Predicate;
3029

3130
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@@ -213,63 +212,28 @@ private void applySerializationModifier(JsonMapper.Builder builder) {
213212

214213
private ContextConfigurationPropertiesDescriptor describeBeans(ObjectMapper mapper, ApplicationContext context,
215214
Predicate<ConfigurationPropertiesBean> beanFilterPredicate, boolean showUnsanitized) {
215+
ApplicationContext parent = context.getParent();
216216
Map<String, ConfigurationPropertiesBean> beans = ConfigurationPropertiesBean.getAll(context);
217217
Map<String, ConfigurationPropertiesBeanDescriptor> descriptors = new LinkedHashMap<>();
218-
beans.values().stream().filter(beanFilterPredicate).forEach((bean) -> {
219-
ConfigurationPropertiesBeanDescriptor descriptor = describeBean(mapper, bean, showUnsanitized);
220-
if (descriptor != null) {
221-
descriptors.put(bean.getName(), descriptor);
222-
}
223-
});
224-
return new ContextConfigurationPropertiesDescriptor(descriptors,
225-
(context.getParent() != null) ? context.getParent().getId() : null);
218+
beans.values()
219+
.stream()
220+
.filter(beanFilterPredicate)
221+
.forEach((bean) -> descriptors.put(bean.getName(), describeBean(mapper, bean, showUnsanitized)));
222+
return new ContextConfigurationPropertiesDescriptor(descriptors, (parent != null) ? parent.getId() : null);
226223
}
227224

228225
private ConfigurationPropertiesBeanDescriptor describeBean(ObjectMapper mapper, ConfigurationPropertiesBean bean,
229226
boolean showUnsanitized) {
230-
return describeTargetBean(bean.getInstance(), (instance) -> {
231-
String prefix = bean.getAnnotation().prefix();
232-
Map<String, Object> serialized = safeSerialize(mapper, instance, prefix);
233-
Map<String, Object> properties = sanitize(prefix, serialized, showUnsanitized);
234-
Map<String, Object> inputs = getInputs(prefix, serialized, showUnsanitized);
235-
return new ConfigurationPropertiesBeanDescriptor(prefix, properties, inputs);
236-
});
237-
}
238-
239-
private ConfigurationPropertiesBeanDescriptor describeTargetBean(Object bean,
240-
Function<Object, ConfigurationPropertiesBeanDescriptor> actionWithTarget) {
241-
TargetSource targetSource = null;
242-
Object target = bean;
243-
while (target instanceof Advised advised) {
244-
try {
245-
targetSource = advised.getTargetSource();
246-
target = targetSource.getTarget();
247-
}
248-
catch (Exception ex) {
249-
return null;
250-
}
251-
}
252-
if (target != null) {
253-
try {
254-
return actionWithTarget.apply(target);
255-
}
256-
finally {
257-
if (targetSource != null) {
258-
try {
259-
targetSource.releaseTarget(target);
260-
}
261-
catch (Exception ex) {
262-
// ignore
263-
}
264-
}
265-
}
266-
}
267-
return null;
227+
String prefix = bean.getAnnotation().prefix();
228+
Map<String, Object> serialized = safeSerialize(mapper, bean.getInstance(), prefix);
229+
Map<String, Object> properties = sanitize(prefix, serialized, showUnsanitized);
230+
Map<String, Object> inputs = getInputs(prefix, serialized, showUnsanitized);
231+
return new ConfigurationPropertiesBeanDescriptor(prefix, properties, inputs);
268232
}
269233

270234
/**
271-
* Cautiously serialize the bean to a map (returning a map with an error message
272-
* instead of throwing an exception if there is a problem).
235+
* Cautiously serialize the ultimate bean target to a map (returning a map with an
236+
* error message instead of throwing an exception if there is a problem).
273237
* @param mapper the object mapper
274238
* @param bean the source bean
275239
* @param prefix the prefix
@@ -278,6 +242,18 @@ private ConfigurationPropertiesBeanDescriptor describeTargetBean(Object bean,
278242
@SuppressWarnings({ "unchecked" })
279243
private Map<String, Object> safeSerialize(ObjectMapper mapper, Object bean, String prefix) {
280244
try {
245+
if (bean instanceof Advised advised) {
246+
TargetSource targetSource = advised.getTargetSource();
247+
Object target = targetSource.getTarget();
248+
try {
249+
return safeSerialize(mapper, target, prefix);
250+
}
251+
finally {
252+
if (target != null) {
253+
targetSource.releaseTarget(target);
254+
}
255+
}
256+
}
281257
return new HashMap<>(mapper.convertValue(bean, Map.class));
282258
}
283259
catch (Exception ex) {

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/context/properties/ConfigurationPropertiesReportEndpointSerializationTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,16 @@ void aopProxyTargetingAnotherProxyIsUnwrapped() {
293293
}
294294

295295
@Test
296-
void aopProxyWithUnresolvableTargetIsExcluded() {
296+
void aopProxyWithUnresolvableTarget() {
297297
ApplicationContextRunner contextRunner = new ApplicationContextRunner()
298298
.withUserConfiguration(UnresolvableTargetFooConfig.class);
299299
contextRunner.run((context) -> {
300300
ConfigurationPropertiesReportEndpoint endpoint = context
301301
.getBean(ConfigurationPropertiesReportEndpoint.class);
302302
ConfigurationPropertiesDescriptor applicationProperties = endpoint.configurationProperties();
303-
assertThat(getContextDescriptor(context, applicationProperties).getBeans()).doesNotContainKey("foo");
303+
assertThat(getContextDescriptor(context, applicationProperties).getBeans()).containsKey("foo")
304+
.satisfies((beans) -> assertThat(beans.get("foo").getProperties()).containsEntry("error",
305+
"Cannot serialize 'foo'"));
304306
});
305307
}
306308

0 commit comments

Comments
 (0)