Skip to content

Commit

Permalink
Adjust container types to which methodValidation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rstoyanchev committed Dec 21, 2023
1 parent 33c1490 commit f0add92
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,12 @@ else if (node.getKind().equals(ElementKind.RETURN_VALUE)) {
.addViolation(violation);
}
else {
// If the argument is a container of elements, we need the specific element,
// but the only option is to check for a parent container index/key in the
// next part of the property path.

// https://github.com/jakartaee/validation/issues/194
// If the argument is a container of elements, we need the element, but
// the only option is to see if the next part of the property path has
// a container index/key for its parent and use it.

Path.Node paramNode = node;
node = itr.next();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -407,8 +406,7 @@ public static boolean checkArguments(Class<?> beanType, MethodParameter[] parame
return true;
}
Class<?> type = param.getParameterType();
if (merged.stream().anyMatch(VALID_PREDICATE) &&
(Collection.class.isAssignableFrom(type) || Map.class.isAssignableFrom(type))) {
if (merged.stream().anyMatch(VALID_PREDICATE) && isIndexOrKeyBasedContainer(type)) {
return true;
}
merged = MergedAnnotations.from(getContainerElementAnnotations(param));
Expand All @@ -428,6 +426,15 @@ public static boolean checkReturnValue(Class<?> beanType, Method method) {
return false;
}

private static boolean isIndexOrKeyBasedContainer(Class<?> type) {

// Index or key-based containers only, or MethodValidationAdapter cannot access
// the element given what is exposed in ConstraintViolation.

return (List.class.isAssignableFrom(type) || Object[].class.isAssignableFrom(type) ||
Map.class.isAssignableFrom(type));
}

/**
* There may be constraints on elements of a container (list, map).
*/
Expand Down

0 comments on commit f0add92

Please sign in to comment.