Skip to content

Commit e06a5fa

Browse files
committed
Obtain merged annotation for annotation validation.
We now validate declared annotations by pre-processing these through AnnotatedElementUtils to ensure a proper comparison. Previously, we compared annotation in their declared form (AnnotatedElement.getAnnotations()) with merged annotations which could fail due to aliasing effects of merged annotations. Closes #2368.
1 parent 1f52a13 commit e06a5fa

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/main/java/org/springframework/data/mapping/model/AnnotationBasedPersistentProperty.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,15 @@ private void populateAnnotationCache(Property property) {
107107

108108
Class<? extends Annotation> annotationType = annotation.annotationType();
109109

110-
validateAnnotation(annotation,
110+
Annotation mergedAnnotation = AnnotatedElementUtils.getMergedAnnotation(it, annotationType);
111+
112+
validateAnnotation(mergedAnnotation,
111113
"Ambiguous mapping! Annotation %s configured "
112114
+ "multiple times on accessor methods of property %s in class %s!",
113115
annotationType.getSimpleName(), getName(), getOwner().getType().getSimpleName());
114116

115117
annotationCache.put(annotationType,
116-
Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(it, annotationType)));
118+
Optional.of(mergedAnnotation));
117119
}
118120
});
119121

@@ -122,13 +124,14 @@ private void populateAnnotationCache(Property property) {
122124
for (Annotation annotation : it.getAnnotations()) {
123125

124126
Class<? extends Annotation> annotationType = annotation.annotationType();
127+
Annotation mergedAnnotation = AnnotatedElementUtils.getMergedAnnotation(it, annotationType);
125128

126-
validateAnnotation(annotation,
129+
validateAnnotation(mergedAnnotation,
127130
"Ambiguous mapping! Annotation %s configured " + "on field %s and one of its accessor methods in class %s!",
128131
annotationType.getSimpleName(), it.getName(), getOwner().getType().getSimpleName());
129132

130133
annotationCache.put(annotationType,
131-
Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(it, annotationType)));
134+
Optional.of(mergedAnnotation));
132135
}
133136
});
134137
}

src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java

+1
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ public String getGetter() {
331331
}
332332

333333
@MyAnnotation
334+
@RevisedAnnnotationWithAliasFor(value = "my-value")
334335
public void setSetter(String setter) {
335336
this.setter = setter;
336337
}

0 commit comments

Comments
 (0)