Skip to content

Commit f0add92

Browse files
committed
Adjust container types to which methodValidation
After the updates to MethodValidationAdapter in commit d7ce13 related to method validation on element containers, we also need to adjust the checks in HandlerMethod when method validation applies. See gh-31746
1 parent 33c1490 commit f0add92

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationAdapter.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,12 @@ else if (node.getKind().equals(ElementKind.RETURN_VALUE)) {
329329
.addViolation(violation);
330330
}
331331
else {
332-
// If the argument is a container of elements, we need the specific element,
333-
// but the only option is to check for a parent container index/key in the
334-
// next part of the property path.
332+
333+
// https://github.com/jakartaee/validation/issues/194
334+
// If the argument is a container of elements, we need the element, but
335+
// the only option is to see if the next part of the property path has
336+
// a container index/key for its parent and use it.
337+
335338
Path.Node paramNode = node;
336339
node = itr.next();
337340

spring-web/src/main/java/org/springframework/web/method/HandlerMethod.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.lang.reflect.AnnotatedType;
2222
import java.lang.reflect.Method;
2323
import java.util.ArrayList;
24-
import java.util.Collection;
2524
import java.util.Collections;
2625
import java.util.List;
2726
import java.util.Map;
@@ -407,8 +406,7 @@ public static boolean checkArguments(Class<?> beanType, MethodParameter[] parame
407406
return true;
408407
}
409408
Class<?> type = param.getParameterType();
410-
if (merged.stream().anyMatch(VALID_PREDICATE) &&
411-
(Collection.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type))) {
409+
if (merged.stream().anyMatch(VALID_PREDICATE) && isIndexOrKeyBasedContainer(type)) {
412410
return true;
413411
}
414412
merged = MergedAnnotations.from(getContainerElementAnnotations(param));
@@ -428,6 +426,15 @@ public static boolean checkReturnValue(Class<?> beanType, Method method) {
428426
return false;
429427
}
430428

429+
private static boolean isIndexOrKeyBasedContainer(Class<?> type) {
430+
431+
// Index or key-based containers only, or MethodValidationAdapter cannot access
432+
// the element given what is exposed in ConstraintViolation.
433+
434+
return (List.class.isAssignableFrom(type) || Object[].class.isAssignableFrom(type) ||
435+
Map.class.isAssignableFrom(type));
436+
}
437+
431438
/**
432439
* There may be constraints on elements of a container (list, map).
433440
*/

0 commit comments

Comments
 (0)