From 6824a4e1bdf914e9155c38d09036e2643fc1705e Mon Sep 17 00:00:00 2001 From: iyuan7 Date: Thu, 19 Nov 2026 14:50:34 +0800 Subject: [PATCH] Detect interface container constraints A container-element constraint declared only on an overridden interface or superclass method did not mark the handler for method validation. Parameter annotations were already merged from those methods, but type-use annotations were not. Fixes gh-37723 Signed-off-by: iyuan7 --- .../core/annotation/AnnotatedMethod.java | 33 +++++++++-- .../web/method/HandlerMethod.java | 37 ++++++++---- .../web/method/HandlerMethodTests.java | 59 ++++++++++++++++++- 3 files changed, 112 insertions(+), 17 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedMethod.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedMethod.java index be8c8ba3db37..a2c535c2acd2 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedMethod.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotatedMethod.java @@ -60,6 +60,8 @@ public class AnnotatedMethod { private volatile @Nullable List inheritedParameterAnnotations; + private volatile @Nullable List overriddenMethods; + /** * Create an instance that wraps the given {@link Method}. @@ -84,6 +86,7 @@ protected AnnotatedMethod(AnnotatedMethod annotatedMethod) { this.parameters = annotatedMethod.parameters; this.annotations = annotatedMethod.annotations; this.inheritedParameterAnnotations = annotatedMethod.inheritedParameterAnnotations; + this.overriddenMethods = annotatedMethod.overriddenMethods; } @@ -174,16 +177,22 @@ public boolean hasMethodAnnotation(Class annotationTyp return (getMethodAnnotation(annotationType) != null); } - private List getInheritedParameterAnnotations() { - List parameterAnnotations = this.inheritedParameterAnnotations; - if (parameterAnnotations == null) { - parameterAnnotations = new ArrayList<>(); + /** + * Return interface and superclass methods that this method overrides. + *

Methods inherited from superinterfaces are included, because + * {@link Class#getMethods()} returns them. + * @since 7.1.1 + */ + protected final List getOverriddenMethods() { + List methods = this.overriddenMethods; + if (methods == null) { + methods = new ArrayList<>(); Class clazz = this.method.getDeclaringClass(); while (clazz != null) { for (Class ifc : clazz.getInterfaces()) { for (Method candidate : ifc.getMethods()) { if (AnnotationsScanner.isOverride(this.method, candidate)) { - parameterAnnotations.add(candidate.getParameterAnnotations()); + methods.add(candidate); } } } @@ -194,11 +203,23 @@ private List getInheritedParameterAnnotations() { if (clazz != null) { for (Method candidate : clazz.getDeclaredMethods()) { if (AnnotationsScanner.isOverride(this.method, candidate)) { - parameterAnnotations.add(candidate.getParameterAnnotations()); + methods.add(candidate); } } } } + this.overriddenMethods = methods; + } + return methods; + } + + private List getInheritedParameterAnnotations() { + List parameterAnnotations = this.inheritedParameterAnnotations; + if (parameterAnnotations == null) { + parameterAnnotations = new ArrayList<>(); + for (Method candidate : getOverriddenMethods()) { + parameterAnnotations.add(candidate.getParameterAnnotations()); + } this.inheritedParameterAnnotations = parameterAnnotations; } return parameterAnnotations; diff --git a/spring-web/src/main/java/org/springframework/web/method/HandlerMethod.java b/spring-web/src/main/java/org/springframework/web/method/HandlerMethod.java index f0f6ed38cfc5..c7e9b63a47ce 100644 --- a/spring-web/src/main/java/org/springframework/web/method/HandlerMethod.java +++ b/spring-web/src/main/java/org/springframework/web/method/HandlerMethod.java @@ -198,7 +198,7 @@ protected HandlerMethod(HandlerMethod handlerMethod, @Nullable Object handler, b this.beanType = handlerMethod.beanType; this.validateArguments = (initValidateFlags ? - MethodValidationInitializer.checkArguments(this.beanType, getMethodParameters()) : + MethodValidationInitializer.checkArguments(this, getMethodParameters()) : handlerMethod.validateArguments); this.validateReturnValue = (initValidateFlags ? @@ -457,8 +457,9 @@ private static class MethodValidationInitializer { private static final Predicate> VALID_PREDICATE = MergedAnnotationPredicates.typeIn("jakarta.validation.Valid"); - public static boolean checkArguments(Class beanType, MethodParameter[] parameters) { - if (BEAN_VALIDATION_PRESENT && AnnotationUtils.findAnnotation(beanType, Validated.class) == null) { + public static boolean checkArguments(HandlerMethod handlerMethod, MethodParameter[] parameters) { + if (BEAN_VALIDATION_PRESENT && + AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), Validated.class) == null) { for (MethodParameter param : parameters) { MergedAnnotations merged = MergedAnnotations.from(param.getParameterAnnotations()); if (merged.stream().anyMatch(CONSTRAINT_PREDICATE)) { @@ -468,7 +469,7 @@ public static boolean checkArguments(Class beanType, MethodParameter[] parame if (merged.stream().anyMatch(VALID_PREDICATE) && isIndexOrKeyBasedContainer(type)) { return true; } - merged = MergedAnnotations.from(getContainerElementAnnotations(param)); + merged = MergedAnnotations.from(getContainerElementAnnotations(handlerMethod, param)); if (merged.stream().anyMatch(CONSTRAINT_PREDICATE.or(VALID_PREDICATE))) { return true; } @@ -495,13 +496,30 @@ private static boolean isIndexOrKeyBasedContainer(Class type) { } /** - * There may be constraints on elements of a container (list, map). + * There may be constraints on elements of a container (list, map, optional). + * A constraint declared only on an overridden interface or superclass method + * still counts: type annotations are not part of {@code getParameterAnnotations()}. */ - private static Annotation[] getContainerElementAnnotations(MethodParameter param) { + private static Annotation[] getContainerElementAnnotations(HandlerMethod handlerMethod, MethodParameter param) { List result = null; - int i = param.getParameterIndex(); + int index = param.getParameterIndex(); Method method = param.getMethod(); - if (method != null && method.getAnnotatedParameterTypes()[i] instanceof AnnotatedParameterizedType apt) { + if (method != null) { + result = addContainerElementAnnotations(result, method, index); + for (Method candidate : handlerMethod.getOverriddenMethods()) { + result = addContainerElementAnnotations(result, candidate, index); + } + } + result = (result != null ? result : Collections.emptyList()); + return result.toArray(new Annotation[0]); + } + + private static @Nullable List addContainerElementAnnotations( + @Nullable List result, Method method, int index) { + + AnnotatedType[] parameterTypes = method.getAnnotatedParameterTypes(); + if (index >= 0 && index < parameterTypes.length && + parameterTypes[index] instanceof AnnotatedParameterizedType apt) { for (AnnotatedType type : apt.getAnnotatedActualTypeArguments()) { for (Annotation annot : type.getAnnotations()) { result = (result != null ? result : new ArrayList<>()); @@ -509,8 +527,7 @@ private static Annotation[] getContainerElementAnnotations(MethodParameter param } } } - result = (result != null ? result : Collections.emptyList()); - return result.toArray(new Annotation[0]); + return result; } } diff --git a/spring-web/src/test/java/org/springframework/web/method/HandlerMethodTests.java b/spring-web/src/test/java/org/springframework/web/method/HandlerMethodTests.java index 291ba4925bdd..30facfddb53e 100644 --- a/spring-web/src/test/java/org/springframework/web/method/HandlerMethodTests.java +++ b/spring-web/src/test/java/org/springframework/web/method/HandlerMethodTests.java @@ -18,6 +18,7 @@ import java.lang.reflect.Method; import java.util.List; +import java.util.Optional; import jakarta.validation.Valid; import jakarta.validation.constraints.Max; @@ -53,6 +54,16 @@ void shouldValidateArgsWithConstraintsInInterface() { testValidateArgs(target, List.of("addPerson", "addPersonNotValidated", "getPerson", "getIntValue"), false); } + @Test // gh-37723 + void shouldValidateContainerElementConstraintDeclaredOnlyOnOverriddenMethod() { + Object target = new ContainerConstraintImpl(); + shouldValidateArguments(getHandlerMethod(target, "optionalCount", Optional.class), true); + shouldValidateArguments(getHandlerMethod(target, "names", List.class), true); + shouldValidateArguments(getHandlerMethod(target, "people", List.class), true); + shouldValidateArguments(getHandlerMethod(target, "unchecked", Optional.class), false); + shouldValidateArguments(getHandlerMethod(target, "fromSuper", Optional.class), true); + } + @Test void shouldValidateArgsWithConstraintsInGenericAbstractSuperclass() { Object target = new GenericInterfaceImpl(); @@ -232,7 +243,7 @@ public void addPersons(List persons) { } @Override - public void addPeople(List<@Valid Person> persons) { + public void addPeople(List persons) { } @Override @@ -292,4 +303,50 @@ public void processTwo(String value) { } } + + @SuppressWarnings("unused") + private interface ContainerConstraintApi { + + void optionalCount(Optional<@Max(100) Integer> count); + + void names(List<@NotEmpty String> names); + + void people(List<@Valid Person> people); + + void unchecked(Optional count); + } + + + @SuppressWarnings("unused") + private abstract static class ContainerConstraintSuper { + + public void fromSuper(Optional<@Max(10) Integer> count) { + } + } + + + @SuppressWarnings("unused") + private static class ContainerConstraintImpl extends ContainerConstraintSuper implements ContainerConstraintApi { + + @Override + public void optionalCount(Optional count) { + } + + @Override + public void names(List names) { + } + + @Override + public void people(List people) { + } + + @Override + public void unchecked(Optional count) { + } + + @Override + public void fromSuper(Optional count) { + } + } + }