Refactor to prepare for method validation handling

To handle method validation errors in ResponseEntityExceptionHandler,
MethodValidationException and associated types should not depend on
Bean Validation. To that effect:

1. MethodValidationResult and ParameterValidationResult no longer make
the underlying ConstraintViolation set available, and instead expose
only the adapted validation errors (MessageSourceResolvable, Errors),
analogous to what SpringValidatorAdapter does. And likewise
MethodValidationException no longer extends ConstraintViolationException.

2. MethodValidationPostProcessor has a new property
adaptConstraintViolations to decide whether to simply raise
ConstraintViolationException, or otherwise to adapt the ConstraintViolations
and raise MethodValidationException instead, with the former is the default
for compatibility.

3. As a result, the MethodValidator contract can now expose methods that
return MethodValidationResult, which provided more flexibility for handling,
and it allows MethodValidationAdapter to implement MethodValidator directly.

4. Update Javadoc in method validation classes to reflect this shift, and
use terminology consistent with Spring validation in classes without an
explicit dependency on Bean Validation.

See gh-30644
This commit is contained in:
rstoyanchev
2023-06-30 14:22:41 +01:00
parent ba4d9a5230
commit a481c7649f
15 changed files with 429 additions and 434 deletions

View File

@@ -69,9 +69,8 @@ public class MethodValidationAdapterTests {
MyService target = new MyService();
Method method = getMethod(target, "addStudent");
validateArguments(target, method, new Object[] {faustino1234, cayetana6789, 3}, ex -> {
testArgs(target, method, new Object[] {faustino1234, cayetana6789, 3}, ex -> {
assertThat(ex.getConstraintViolations()).hasSize(3);
assertThat(ex.getAllValidationResults()).hasSize(3);
assertBeanResult(ex.getBeanResults().get(0), 0, "student", faustino1234, List.of("""
@@ -104,9 +103,8 @@ public class MethodValidationAdapterTests {
this.validationAdapter.setBindingResultNameResolver((parameter, value) -> "studentToAdd");
validateArguments(target, method, new Object[] {faustino1234, new Person("Joe"), 1}, ex -> {
testArgs(target, method, new Object[] {faustino1234, new Person("Joe"), 1}, ex -> {
assertThat(ex.getConstraintViolations()).hasSize(1);
assertThat(ex.getAllValidationResults()).hasSize(1);
assertBeanResult(ex.getBeanResults().get(0), 0, "studentToAdd", faustino1234, List.of("""
@@ -122,9 +120,8 @@ public class MethodValidationAdapterTests {
void validateReturnValue() {
MyService target = new MyService();
validateReturnValue(target, getMethod(target, "getIntValue"), 4, ex -> {
testReturnValue(target, getMethod(target, "getIntValue"), 4, ex -> {
assertThat(ex.getConstraintViolations()).hasSize(1);
assertThat(ex.getAllValidationResults()).hasSize(1);
assertValueResult(ex.getValueResults().get(0), -1, 4, List.of("""
@@ -140,9 +137,8 @@ public class MethodValidationAdapterTests {
void validateReturnValueBean() {
MyService target = new MyService();
validateReturnValue(target, getMethod(target, "getPerson"), faustino1234, ex -> {
testReturnValue(target, getMethod(target, "getPerson"), faustino1234, ex -> {
assertThat(ex.getConstraintViolations()).hasSize(1);
assertThat(ex.getAllValidationResults()).hasSize(1);
assertBeanResult(ex.getBeanResults().get(0), -1, "person", faustino1234, List.of("""
@@ -159,9 +155,8 @@ public class MethodValidationAdapterTests {
MyService target = new MyService();
Method method = getMethod(target, "addPeople");
validateArguments(target, method, new Object[] {List.of(faustino1234, cayetana6789)}, ex -> {
testArgs(target, method, new Object[] {List.of(faustino1234, cayetana6789)}, ex -> {
assertThat(ex.getConstraintViolations()).hasSize(2);
assertThat(ex.getAllValidationResults()).hasSize(2);
int paramIndex = 0;
@@ -184,18 +179,12 @@ public class MethodValidationAdapterTests {
});
}
private void validateArguments(
Object target, Method method, Object[] arguments, Consumer<MethodValidationResult> assertions) {
assertions.accept(
this.validationAdapter.validateMethodArguments(target, method, null, arguments, new Class<?>[0]));
private void testArgs(Object target, Method method, Object[] args, Consumer<MethodValidationResult> consumer) {
consumer.accept(this.validationAdapter.validateArguments(target, method, null, args, new Class<?>[0]));
}
private void validateReturnValue(
Object target, Method method, @Nullable Object returnValue, Consumer<MethodValidationResult> assertions) {
assertions.accept(
this.validationAdapter.validateMethodReturnValue(target, method, null, returnValue, new Class<?>[0]));
private void testReturnValue(Object target, Method method, @Nullable Object value, Consumer<MethodValidationResult> consumer) {
consumer.accept(this.validationAdapter.validateReturnValue(target, method, null, value, new Class<?>[0]));
}
private static void assertBeanResult(