From 79b5dd3ae701649f4a926827db84a6b6c7d9ffc0 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 14 Nov 2018 15:50:53 -0800 Subject: [PATCH] Protect against NPE when collecting annotations Update `AnnotationsPropertySource` to ensure that `null` results from `findMergedAnnotation` are not added to the annotation list. Prior to this commit, if `findMergedAnnotation` returned `null` then `AnnotationsPropertySource.collectProperties` would throw an NPE. Although `findMergedAnnotation` should never return `null`, we're best off being defensive so that bugs such as SPR-17495 won't cause problems. Closes gh-15175 --- .../properties/AnnotationsPropertySource.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySource.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySource.java index 0cef66a3430..697f6082fc0 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySource.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/properties/AnnotationsPropertySource.java @@ -90,8 +90,11 @@ public class AnnotationsPropertySource extends EnumerablePropertySource if (annotations != null) { for (Annotation annotation : annotations) { if (!AnnotationUtils.isInJavaLangAnnotationPackage(annotation)) { - mergedAnnotations - .add(findMergedAnnotation(root, annotation.annotationType())); + Annotation mergedAnnotation = findMergedAnnotation(root, + annotation.annotationType()); + if (mergedAnnotation != null) { + mergedAnnotations.add(mergedAnnotation); + } } } }