Add MethodParameter[] input to MethodValidationAdapter

This allows re-use of existing MethodParameter instances from controller
methods with cached metadata, and also ensures additional capabilities
such as looking up parameter annotations on interfaces.

See gh-29825
This commit is contained in:
rstoyanchev
2023-06-13 17:40:32 +01:00
parent e7c3e1c516
commit 85d81024a4
8 changed files with 94 additions and 57 deletions

View File

@@ -18,6 +18,7 @@ package org.springframework.validation.beanvalidation;
import java.lang.reflect.Method;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
/**
@@ -44,31 +45,45 @@ public class DefaultMethodValidator implements MethodValidator {
}
@Override
public void validateArguments(Object target, Method method, Object[] arguments, Class<?>[] groups) {
MethodValidationResult result = this.adapter.validateMethodArguments(target, method, arguments, groups);
handleArgumentsResult(target, method, arguments, groups, result);
public void validateArguments(
Object target, Method method, @Nullable MethodParameter[] parameters, Object[] arguments,
Class<?>[] groups) {
handleArgumentsValidationResult(target, method, arguments, groups,
this.adapter.validateMethodArguments(target, method, parameters, arguments, groups));
}
public void validateReturnValue(
Object target, Method method, @Nullable MethodParameter returnType, @Nullable Object returnValue,
Class<?>[] groups) {
handleReturnValueValidationResult(target, method, returnValue, groups,
this.adapter.validateMethodReturnValue(target, method, returnType, returnValue, groups));
}
/**
* Subclasses can override this to handle the result of argument validation.
* By default, {@link MethodValidationResult#throwIfViolationsPresent()} is called.
* @param bean the target Object for method invocation
* @param method the target method
* @param arguments the candidate argument values to validate
* @param groups groups for validation determined via
*/
protected void handleArgumentsResult(
protected void handleArgumentsValidationResult(
Object bean, Method method, Object[] arguments, Class<?>[] groups, MethodValidationResult result) {
result.throwIfViolationsPresent();
}
public void validateReturnValue(Object target, Method method, @Nullable Object returnValue, Class<?>[] groups) {
MethodValidationResult result = this.adapter.validateMethodReturnValue(target, method, returnValue, groups);
handleReturnValueResult(target, method, returnValue, groups, result);
}
/**
* Subclasses can override this to handle the result of return value validation.
* By default, {@link MethodValidationResult#throwIfViolationsPresent()} is called.
* @param bean the target Object for method invocation
* @param method the target method
* @param returnValue the return value to validate
* @param groups groups for validation determined via
*/
protected void handleReturnValueResult(
protected void handleReturnValueValidationResult(
Object bean, Method method, @Nullable Object returnValue, Class<?>[] groups, MethodValidationResult result) {
result.throwIfViolationsPresent();

View File

@@ -176,8 +176,8 @@ public class MethodValidationAdapter {
/**
* Use this method determine the validation groups to pass into
* {@link #validateMethodArguments(Object, Method, Object[], Class[])} and
* {@link #validateMethodReturnValue(Object, Method, Object, Class[])}.
* {@link #validateMethodArguments(Object, Method, MethodParameter[], Object[], Class[])} and
* {@link #validateMethodReturnValue(Object, Method, MethodParameter, Object, Class[])}.
* <p>Default are the validation groups as specified in the {@link Validated}
* annotation on the method, or on the containing target class of the method,
* or for an AOP proxy without a target (with all behavior in advisors), also
@@ -208,7 +208,8 @@ public class MethodValidationAdapter {
* Validate the given method arguments and return the result of validation.
* @param target the target Object
* @param method the target method
* @param arguments candidate arguments for a method invocation
* @param parameters the parameters, if already created and available
* @param arguments the candidate argument values to validate
* @param groups groups for validation determined via
* {@link #determineValidationGroups(Object, Method)}
* @return a result with {@link ConstraintViolation violations} and
@@ -216,7 +217,8 @@ public class MethodValidationAdapter {
* in case there are no violations
*/
public MethodValidationResult validateMethodArguments(
Object target, Method method, Object[] arguments, Class<?>[] groups) {
Object target, Method method, @Nullable MethodParameter[] parameters, Object[] arguments,
Class<?>[] groups) {
ExecutableValidator execVal = this.validator.get().forExecutables();
Set<ConstraintViolation<Object>> result;
@@ -231,14 +233,18 @@ public class MethodValidationAdapter {
result = execVal.validateParameters(target, bridgedMethod, arguments, groups);
}
return (result.isEmpty() ? EMPTY_RESULT :
createException(target, method, result, i -> arguments[i], false));
createException(target, method, result,
i -> parameters != null ? parameters[i] : new MethodParameter(method, i),
i -> arguments[i],
false));
}
/**
* Validate the given return value and return the result of validation.
* @param target the target Object
* @param method the target method
* @param returnValue value returned from invoking the target method
* @param returnType the return parameter, if already created and available
* @param returnValue the return value to validate
* @param groups groups for validation determined via
* {@link #determineValidationGroups(Object, Method)}
* @return a result with {@link ConstraintViolation violations} and
@@ -246,16 +252,22 @@ public class MethodValidationAdapter {
* in case there are no violations
*/
public MethodValidationResult validateMethodReturnValue(
Object target, Method method, @Nullable Object returnValue, Class<?>[] groups) {
Object target, Method method, @Nullable MethodParameter returnType, @Nullable Object returnValue,
Class<?>[] groups) {
ExecutableValidator execVal = this.validator.get().forExecutables();
Set<ConstraintViolation<Object>> result = execVal.validateReturnValue(target, method, returnValue, groups);
return (result.isEmpty() ? EMPTY_RESULT : createException(target, method, result, i -> returnValue, true));
return (result.isEmpty() ? EMPTY_RESULT :
createException(target, method, result,
i -> returnType != null ? returnType : new MethodParameter(method, -1),
i -> returnValue,
true));
}
private MethodValidationException createException(
Object target, Method method, Set<ConstraintViolation<Object>> violations,
Function<Integer, Object> argumentFunction, boolean forReturnValue) {
Function<Integer, MethodParameter> parameterFunction, Function<Integer, Object> argumentFunction,
boolean forReturnValue) {
Map<MethodParameter, ValueResultBuilder> parameterViolations = new LinkedHashMap<>();
Map<Path.Node, BeanResultBuilder> cascadedViolations = new LinkedHashMap<>();
@@ -267,10 +279,11 @@ public class MethodValidationAdapter {
MethodParameter parameter;
if (node.getKind().equals(ElementKind.PARAMETER)) {
parameter = new MethodParameter(method, node.as(Path.ParameterNode.class).getParameterIndex());
int index = node.as(Path.ParameterNode.class).getParameterIndex();
parameter = parameterFunction.apply(index);
}
else if (node.getKind().equals(ElementKind.RETURN_VALUE)) {
parameter = new MethodParameter(method, -1);
parameter = parameterFunction.apply(-1);
}
else {
continue;

View File

@@ -104,12 +104,12 @@ public class MethodValidationInterceptor implements MethodInterceptor {
Method method = invocation.getMethod();
Class<?>[] groups = determineValidationGroups(invocation);
this.delegate.validateMethodArguments(target, method, invocation.getArguments(), groups)
this.delegate.validateMethodArguments(target, method, null, invocation.getArguments(), groups)
.throwIfViolationsPresent();
Object returnValue = invocation.proceed();
this.delegate.validateMethodReturnValue(target, method, returnValue, groups)
this.delegate.validateMethodReturnValue(target, method, null, returnValue, groups)
.throwIfViolationsPresent();
return returnValue;

View File

@@ -18,6 +18,7 @@ package org.springframework.validation.beanvalidation;
import java.lang.reflect.Method;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
/**
@@ -35,8 +36,8 @@ public interface MethodValidator {
/**
* Use this method determine the validation groups to pass into
* {@link #validateArguments(Object, Method, Object[], Class[])} and
* {@link #validateReturnValue(Object, Method, Object, Class[])}.
* {@link #validateArguments(Object, Method, MethodParameter[], Object[], Class[])} and
* {@link #validateReturnValue(Object, Method, MethodParameter, Object, Class[])}.
* @param target the target Object
* @param method the target method
* @return the applicable validation groups as a {@code Class} array
@@ -48,24 +49,30 @@ public interface MethodValidator {
* Validate the given method arguments and return the result of validation.
* @param target the target Object
* @param method the target method
* @param arguments candidate arguments for a method invocation
* @param parameters the parameters, if already created and available
* @param arguments the candidate argument values to validate
* @param groups groups for validation determined via
* {@link #determineValidationGroups(Object, Method)}
* @throws MethodValidationException should be raised in case of validation
* errors unless the implementation handles those errors otherwise (e.g.
* by injecting {@code BindingResult} into the method).
*/
void validateArguments(Object target, Method method, Object[] arguments, Class<?>[] groups);
void validateArguments(
Object target, Method method, @Nullable MethodParameter[] parameters, Object[] arguments,
Class<?>[] groups);
/**
* Validate the given return value and return the result of validation.
* @param target the target Object
* @param method the target method
* @param returnValue value returned from invoking the target method
* @param returnType the return parameter, if already created and available
* @param returnValue the return value to validate
* @param groups groups for validation determined via
* {@link #determineValidationGroups(Object, Method)}
* @throws MethodValidationException in case of validation errors
*/
void validateReturnValue(Object target, Method method, @Nullable Object returnValue, Class<?>[] groups);
void validateReturnValue(
Object target, Method method, @Nullable MethodParameter returnType, @Nullable Object returnValue,
Class<?>[] groups);
}