2525import java .util .LinkedHashMap ;
2626import java .util .List ;
2727import java .util .Map ;
28- import java .util .function .Function ;
2928import java .util .function .Predicate ;
3029
3130import 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 ) {
0 commit comments