Omit deprecation warning for Bean Validation constraint annotations

Prior to this commit, AnnotationTypeMapping logged a warning for the use
of convention-based annotation attribute overrides in composed Bean
Validation constraint annotations, even though those attribute overrides
are not related to Spring.

For example, Hibernate's @URL constraint annotation is meta-annotated
with Bean Validation's @Pattern constraint annotation, and we should not
log a warning in such scenarios.

This commit addresses that by not logging a warning if convention-based
annotation attribute overrides are detected for a composed @Constraint
annotation.

Closes gh-29206
This commit is contained in:
Sam Brannen
2022-10-17 19:39:02 +02:00
parent 0e861af050
commit 4d44aaf81b
2 changed files with 73 additions and 5 deletions

View File

@@ -29,6 +29,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -52,9 +53,15 @@ final class AnnotationTypeMapping {
private static final Log logger = LogFactory.getLog(AnnotationTypeMapping.class);
private static final Predicate<? super Annotation> isBeanValidationConstraint = annotation ->
annotation.annotationType().getName().equals("jakarta.validation.Constraint");
/**
* Set of fully qualified class names for annotations which we have already
* checked for use of convention-based annotation attribute overrides.
* Set used to track which convention-based annotation attribute overrides
* have already been checked. Each key is the combination of the fully
* qualified class names of a composed annotation and a meta-annotation
* that it is either present or meta-present on the composed annotation,
* separated by a dash.
* @since 6.0
* @see #addConventionMappings()
*/
@@ -299,9 +306,18 @@ final class AnnotationTypeMapping {
}
}
String rootAnnotationTypeName = this.root.annotationType.getName();
// We want to avoid duplicate log warnings as much as possible, without full synchronization.
if (conventionBasedOverrideCheckCache.add(rootAnnotationTypeName) &&
!conventionMappedAttributes.isEmpty() && logger.isWarnEnabled()) {
String cacheKey = rootAnnotationTypeName + '-' + this.annotationType.getName();
// We want to avoid duplicate log warnings as much as possible, without full synchronization,
// and we intentionally invoke add() before checking if any convention-based overrides were
// actually encountered in order to ensure that we add a "tracked" entry for the current cache
// key in any case.
// In addition, we do NOT want to log warnings for custom Java Bean Validation constraint
// annotations that are meta-annotated with other constraint annotations -- for example,
// @org.hibernate.validator.constraints.URL which overrides attributes in
// @jakarta.validation.constraints.Pattern.
if (conventionBasedOverrideCheckCache.add(cacheKey) && !conventionMappedAttributes.isEmpty() &&
Arrays.stream(this.annotationType.getAnnotations()).noneMatch(isBeanValidationConstraint) &&
logger.isWarnEnabled()) {
logger.warn("""
Support for convention-based annotation attribute overrides is deprecated \
and will be removed in Spring Framework 6.1. Please annotate the following \