Skip to content

Commit ac9b127

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 0d4fb60 commit ac9b127

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
@@ -120,13 +120,15 @@ private void populateAnnotationCache(Property property) {
120120

121121
Class<? extends Annotation> annotationType = annotation.annotationType();
122122

123-
validateAnnotation(annotation,
123+
Annotation mergedAnnotation = AnnotatedElementUtils.getMergedAnnotation(it, annotationType);
124+
125+
validateAnnotation(mergedAnnotation,
124126
"Ambiguous mapping! Annotation %s configured "
125127
+ "multiple times on accessor methods of property %s in class %s!",
126128
annotationType.getSimpleName(), getName(), getOwner().getType().getSimpleName());
127129

128130
annotationCache.put(annotationType,
129-
Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(it, annotationType)));
131+
Optional.of(mergedAnnotation));
130132
}
131133
});
132134

@@ -135,13 +137,14 @@ private void populateAnnotationCache(Property property) {
135137
for (Annotation annotation : it.getAnnotations()) {
136138

137139
Class<? extends Annotation> annotationType = annotation.annotationType();
140+
Annotation mergedAnnotation = AnnotatedElementUtils.getMergedAnnotation(it, annotationType);
138141

139-
validateAnnotation(annotation,
142+
validateAnnotation(mergedAnnotation,
140143
"Ambiguous mapping! Annotation %s configured " + "on field %s and one of its accessor methods in class %s!",
141144
annotationType.getSimpleName(), it.getName(), getOwner().getType().getSimpleName());
142145

143146
annotationCache.put(annotationType,
144-
Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(it, annotationType)));
147+
Optional.of(mergedAnnotation));
145148
}
146149
});
147150
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ public String getGetter() {
356356
}
357357

358358
@MyAnnotation
359+
@RevisedAnnnotationWithAliasFor(value = "my-value")
359360
public void setSetter(String setter) {
360361
this.setter = setter;
361362
}

0 commit comments

Comments
 (0)