Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public class AnnotatedMethod {

private volatile @Nullable List<Annotation[][]> inheritedParameterAnnotations;

private volatile @Nullable List<Method> overriddenMethods;


/**
* Create an instance that wraps the given {@link Method}.
Expand All @@ -84,6 +86,7 @@ protected AnnotatedMethod(AnnotatedMethod annotatedMethod) {
this.parameters = annotatedMethod.parameters;
this.annotations = annotatedMethod.annotations;
this.inheritedParameterAnnotations = annotatedMethod.inheritedParameterAnnotations;
this.overriddenMethods = annotatedMethod.overriddenMethods;
}


Expand Down Expand Up @@ -174,16 +177,22 @@ public <A extends Annotation> boolean hasMethodAnnotation(Class<A> annotationTyp
return (getMethodAnnotation(annotationType) != null);
}

private List<Annotation[][]> getInheritedParameterAnnotations() {
List<Annotation[][]> parameterAnnotations = this.inheritedParameterAnnotations;
if (parameterAnnotations == null) {
parameterAnnotations = new ArrayList<>();
/**
* Return interface and superclass methods that this method overrides.
* <p>Methods inherited from superinterfaces are included, because
* {@link Class#getMethods()} returns them.
* @since 7.1.1
*/
protected final List<Method> getOverriddenMethods() {
List<Method> 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);
}
}
}
Expand All @@ -194,11 +203,23 @@ private List<Annotation[][]> 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<Annotation[][]> getInheritedParameterAnnotations() {
List<Annotation[][]> parameterAnnotations = this.inheritedParameterAnnotations;
if (parameterAnnotations == null) {
parameterAnnotations = new ArrayList<>();
for (Method candidate : getOverriddenMethods()) {
parameterAnnotations.add(candidate.getParameterAnnotations());
}
this.inheritedParameterAnnotations = parameterAnnotations;
}
return parameterAnnotations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?
Expand Down Expand Up @@ -457,8 +457,9 @@ private static class MethodValidationInitializer {
private static final Predicate<MergedAnnotation<? extends Annotation>> 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)) {
Expand All @@ -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;
}
Expand All @@ -495,22 +496,38 @@ 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<Annotation> 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<Annotation> addContainerElementAnnotations(
@Nullable List<Annotation> 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<>());
result.add(annot);
}
}
}
result = (result != null ? result : Collections.emptyList());
return result.toArray(new Annotation[0]);
return result;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -232,7 +243,7 @@ public void addPersons(List<Person> persons) {
}

@Override
public void addPeople(List<@Valid Person> persons) {
public void addPeople(List<Person> persons) {
}

@Override
Expand Down Expand Up @@ -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<Integer> 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<Integer> count) {
}

@Override
public void names(List<String> names) {
}

@Override
public void people(List<Person> people) {
}

@Override
public void unchecked(Optional<Integer> count) {
}

@Override
public void fromSuper(Optional<Integer> count) {
}
}

}