Expose isReturnValue from MethodValidationException
See gh-29825
This commit is contained in:
parent
5c5d8e61ae
commit
cb04c3b335
|
|
@ -230,7 +230,8 @@ public class MethodValidationAdapter {
|
|||
Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(mostSpecificMethod);
|
||||
result = execVal.validateParameters(target, bridgedMethod, arguments, groups);
|
||||
}
|
||||
return (result.isEmpty() ? EMPTY_RESULT : createException(target, method, result, i -> arguments[i]));
|
||||
return (result.isEmpty() ? EMPTY_RESULT :
|
||||
createException(target, method, result, i -> arguments[i], false));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -249,12 +250,12 @@ public class MethodValidationAdapter {
|
|||
|
||||
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));
|
||||
return (result.isEmpty() ? EMPTY_RESULT : createException(target, method, result, i -> returnValue, true));
|
||||
}
|
||||
|
||||
private MethodValidationException createException(
|
||||
Object target, Method method, Set<ConstraintViolation<Object>> violations,
|
||||
Function<Integer, Object> argumentFunction) {
|
||||
Function<Integer, Object> argumentFunction, boolean forReturnValue) {
|
||||
|
||||
Map<MethodParameter, ValueResultBuilder> parameterViolations = new LinkedHashMap<>();
|
||||
Map<Path.Node, BeanResultBuilder> cascadedViolations = new LinkedHashMap<>();
|
||||
|
|
@ -296,7 +297,7 @@ public class MethodValidationAdapter {
|
|||
cascadedViolations.forEach((node, builder) -> validatonResultList.add(builder.build()));
|
||||
validatonResultList.sort(RESULT_COMPARATOR);
|
||||
|
||||
return new MethodValidationException(target, method, violations, validatonResultList);
|
||||
return new MethodValidationException(target, method, violations, validatonResultList, forReturnValue);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -549,6 +550,11 @@ public class MethodValidationAdapter {
|
|||
public void throwIfViolationsPresent() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MethodValidationResult (0 violations)";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -48,16 +48,19 @@ public class MethodValidationException extends ConstraintViolationException impl
|
|||
|
||||
private final List<ParameterValidationResult> allValidationResults;
|
||||
|
||||
private final boolean forReturnValue;
|
||||
|
||||
|
||||
public MethodValidationException(
|
||||
Object target, Method method, Set<? extends ConstraintViolation<?>> violations,
|
||||
List<ParameterValidationResult> validationResults) {
|
||||
List<ParameterValidationResult> validationResults, boolean forReturnValue) {
|
||||
|
||||
super(violations);
|
||||
Assert.notEmpty(violations, "'violations' must not be empty");
|
||||
this.target = target;
|
||||
this.method = method;
|
||||
this.allValidationResults = validationResults;
|
||||
this.forReturnValue = forReturnValue;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -75,6 +78,15 @@ public class MethodValidationException extends ConstraintViolationException impl
|
|||
return this.method;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the violations are for a return value.
|
||||
* If true the violations are from validating a return value.
|
||||
* If false the violations are from validating method arguments.
|
||||
*/
|
||||
public boolean isForReturnValue() {
|
||||
return this.forReturnValue;
|
||||
}
|
||||
|
||||
// re-declare parent class method for NonNull treatment of interface
|
||||
|
||||
@Override
|
||||
|
|
@ -107,4 +119,10 @@ public class MethodValidationException extends ConstraintViolationException impl
|
|||
throw this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MethodValidationResult (" + getConstraintViolations().size() + " violations) " +
|
||||
"for " + this.method.toGenericString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue