Skip to content

Commit 55b0fe1

Browse files
committed
Unwrap SpringValidatorAdapter (e.g. CustomValidatorBean) to native Validator
Issue: SPR-15629 (cherry picked from commit 8330134)
1 parent 510436b commit 55b0fe1

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

spring-context/src/main/java/org/springframework/validation/beanvalidation/LocalValidatorFactoryBean.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ public javax.validation.ClockProvider getClockProvider() {
378378
*/
379379

380380
@Override
381+
@SuppressWarnings("unchecked")
381382
public <T> T unwrap(@Nullable Class<T> type) {
382383
if (type == null || !ValidatorFactory.class.isAssignableFrom(type)) {
383384
try {
@@ -387,7 +388,16 @@ public <T> T unwrap(@Nullable Class<T> type) {
387388
// ignore - we'll try ValidatorFactory unwrapping next
388389
}
389390
}
390-
return this.validatorFactory.unwrap(type);
391+
try {
392+
return this.validatorFactory.unwrap(type);
393+
}
394+
catch (ValidationException ex) {
395+
// ignore if just being asked for ValidatorFactory
396+
if (ValidatorFactory.class == type) {
397+
return (T) this.validatorFactory;
398+
}
399+
throw ex;
400+
}
391401
}
392402

393403
public void close() {

spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationPostProcessor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,13 @@ public void setValidatedAnnotationType(Class<? extends Annotation> validatedAnno
8383
* <p>Default is the default ValidatorFactory's default Validator.
8484
*/
8585
public void setValidator(Validator validator) {
86+
// Unwrap to the native Validator with forExecutables support
8687
if (validator instanceof LocalValidatorFactoryBean) {
8788
this.validator = ((LocalValidatorFactoryBean) validator).getValidator();
8889
}
90+
else if (validator instanceof SpringValidatorAdapter) {
91+
this.validator = validator.unwrap(Validator.class);
92+
}
8993
else {
9094
this.validator = validator;
9195
}

spring-context/src/main/java/org/springframework/validation/beanvalidation/SpringValidatorAdapter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.util.Set;
2626
import java.util.TreeMap;
2727
import javax.validation.ConstraintViolation;
28+
import javax.validation.ValidationException;
2829
import javax.validation.executable.ExecutableValidator;
2930
import javax.validation.metadata.BeanDescriptor;
3031
import javax.validation.metadata.ConstraintDescriptor;
@@ -303,7 +304,16 @@ public BeanDescriptor getConstraintsForClass(Class<?> clazz) {
303304
@SuppressWarnings("unchecked")
304305
public <T> T unwrap(@Nullable Class<T> type) {
305306
Assert.state(this.targetValidator != null, "No target Validator set");
306-
return (type != null ? this.targetValidator.unwrap(type) : (T) this.targetValidator);
307+
try {
308+
return (type != null ? this.targetValidator.unwrap(type) : (T) this.targetValidator);
309+
}
310+
catch (ValidationException ex) {
311+
// ignore if just being asked for plain Validator
312+
if (javax.validation.Validator.class == type) {
313+
return (T) this.targetValidator;
314+
}
315+
throw ex;
316+
}
307317
}
308318

309319
@Override

0 commit comments

Comments
 (0)