Skip to content

Commit 425d5a9

Browse files
committed
Adapt ConstraintViolation's from method validation
See gh-29825
1 parent 38abee0 commit 425d5a9

File tree

8 files changed

+1237
-169
lines changed

8 files changed

+1237
-169
lines changed

Diff for: spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationAdapter.java

+459
Large diffs are not rendered by default.

Diff for: spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationDelegate.java

-161
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright 2002-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.validation.beanvalidation;
18+
19+
import java.lang.reflect.Method;
20+
import java.util.List;
21+
import java.util.Set;
22+
23+
import jakarta.validation.ConstraintViolation;
24+
import jakarta.validation.ConstraintViolationException;
25+
26+
/**
27+
* Extension of {@link ConstraintViolationException} that exposes an additional
28+
* list of {@link ParameterValidationResult} with violations adapted to
29+
* {@link org.springframework.context.MessageSourceResolvable} and grouped by
30+
* method parameter.
31+
*
32+
* <p>For {@link jakarta.validation.Valid @Valid}-annotated, Object method
33+
* parameters or return types with cascaded violations, the {@link ParameterErrors}
34+
* subclass of {@link ParameterValidationResult} implements
35+
* {@link org.springframework.validation.Errors} and exposes
36+
* {@link org.springframework.validation.FieldError field errors}.
37+
*
38+
* @author Rossen Stoyanchev
39+
* @since 6.1
40+
* @see ParameterValidationResult
41+
* @see ParameterErrors
42+
* @see MethodValidationAdapter
43+
*/
44+
@SuppressWarnings("serial")
45+
public class MethodValidationException extends ConstraintViolationException {
46+
47+
private final Object target;
48+
49+
private final Method method;
50+
51+
private final List<ParameterValidationResult> allValidationResults;
52+
53+
54+
public MethodValidationException(
55+
Object target, Method method,
56+
List<ParameterValidationResult> validationResults,
57+
Set<? extends ConstraintViolation<?>> violations) {
58+
59+
super(violations);
60+
this.target = target;
61+
this.method = method;
62+
this.allValidationResults = validationResults;
63+
}
64+
65+
66+
/**
67+
* Return the target of the method invocation to which validation was applied.
68+
*/
69+
public Object getTarget() {
70+
return this.target;
71+
}
72+
73+
/**
74+
* Return the method to which validation was applied.
75+
*/
76+
public Method getMethod() {
77+
return this.method;
78+
}
79+
80+
/**
81+
* Return all validation results. This includes method parameters with
82+
* constraints declared on them, as well as
83+
* {@link jakarta.validation.Valid @Valid} method parameters with
84+
* cascaded constraints.
85+
* @see #getValueResults()
86+
* @see #getBeanResults()
87+
*/
88+
public List<ParameterValidationResult> getAllValidationResults() {
89+
return this.allValidationResults;
90+
}
91+
92+
/**
93+
* Return only validation results for method parameters with constraints
94+
* declared directly on them. This excludes
95+
* {@link jakarta.validation.Valid @Valid} method parameters with cascaded
96+
* constraints.
97+
* @see #getAllValidationResults()
98+
*/
99+
public List<ParameterValidationResult> getValueResults() {
100+
return this.allValidationResults.stream()
101+
.filter(result -> !(result instanceof ParameterErrors))
102+
.toList();
103+
}
104+
105+
/**
106+
* Return only validation results for {@link jakarta.validation.Valid @Valid}
107+
* method parameters with cascaded constraints. This excludes method
108+
* parameters with constraints declared directly on them.
109+
* @see #getAllValidationResults()
110+
*/
111+
public List<ParameterErrors> getBeanResults() {
112+
return this.allValidationResults.stream()
113+
.filter(result -> result instanceof ParameterErrors)
114+
.map(result -> (ParameterErrors) result)
115+
.toList();
116+
}
117+
118+
}

Diff for: spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationInterceptor.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -55,30 +55,30 @@
5555
*/
5656
public class MethodValidationInterceptor implements MethodInterceptor {
5757

58-
private final MethodValidationDelegate delegate;
58+
private final MethodValidationAdapter delegate;
5959

6060

6161
/**
6262
* Create a new MethodValidationInterceptor using a default JSR-303 validator underneath.
6363
*/
6464
public MethodValidationInterceptor() {
65-
this.delegate = new MethodValidationDelegate();
65+
this.delegate = new MethodValidationAdapter();
6666
}
6767

6868
/**
6969
* Create a new MethodValidationInterceptor using the given JSR-303 ValidatorFactory.
7070
* @param validatorFactory the JSR-303 ValidatorFactory to use
7171
*/
7272
public MethodValidationInterceptor(ValidatorFactory validatorFactory) {
73-
this.delegate = new MethodValidationDelegate(validatorFactory);
73+
this.delegate = new MethodValidationAdapter(validatorFactory);
7474
}
7575

7676
/**
7777
* Create a new MethodValidationInterceptor using the given JSR-303 Validator.
7878
* @param validator the JSR-303 Validator to use
7979
*/
8080
public MethodValidationInterceptor(Validator validator) {
81-
this.delegate = new MethodValidationDelegate(validator);
81+
this.delegate = new MethodValidationAdapter(validator);
8282
}
8383

8484
/**
@@ -88,7 +88,7 @@ public MethodValidationInterceptor(Validator validator) {
8888
* @since 6.0
8989
*/
9090
public MethodValidationInterceptor(Supplier<Validator> validator) {
91-
this.delegate = new MethodValidationDelegate(validator);
91+
this.delegate = new MethodValidationAdapter(validator);
9292
}
9393

9494

@@ -153,7 +153,7 @@ else if (FactoryBean.class.isAssignableFrom(clazz)) {
153153
protected Class<?>[] determineValidationGroups(MethodInvocation invocation) {
154154
Object target = getTarget(invocation);
155155
Method method = invocation.getMethod();
156-
return this.delegate.determineValidationGroups(target, method);
156+
return MethodValidationAdapter.determineValidationGroups(target, method);
157157
}
158158

159159
}

0 commit comments

Comments
 (0)