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
This commit is contained in:
rstoyanchev
2023-12-21 17:50:40 +00:00
parent 33c149077a
commit f0add920f5
2 changed files with 16 additions and 6 deletions

View File

@@ -21,7 +21,6 @@ import java.lang.reflect.AnnotatedParameterizedType;
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;
@@ -407,8 +406,7 @@ public class HandlerMethod extends AnnotatedMethod {
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));
@@ -428,6 +426,15 @@ public class HandlerMethod extends AnnotatedMethod {
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).
*/