Unwrap SpringValidatorAdapter (e.g. CustomValidatorBean) to native Validator
Issue: SPR-15629
(cherry picked from commit 8330134)
This commit is contained in:
@@ -378,6 +378,7 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter
|
||||
*/
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T unwrap(@Nullable Class<T> type) {
|
||||
if (type == null || !ValidatorFactory.class.isAssignableFrom(type)) {
|
||||
try {
|
||||
@@ -387,7 +388,16 @@ public class LocalValidatorFactoryBean extends SpringValidatorAdapter
|
||||
// ignore - we'll try ValidatorFactory unwrapping next
|
||||
}
|
||||
}
|
||||
return this.validatorFactory.unwrap(type);
|
||||
try {
|
||||
return this.validatorFactory.unwrap(type);
|
||||
}
|
||||
catch (ValidationException ex) {
|
||||
// ignore if just being asked for ValidatorFactory
|
||||
if (ValidatorFactory.class == type) {
|
||||
return (T) this.validatorFactory;
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
|
||||
@@ -83,9 +83,13 @@ public class MethodValidationPostProcessor extends AbstractBeanFactoryAwareAdvis
|
||||
* <p>Default is the default ValidatorFactory's default Validator.
|
||||
*/
|
||||
public void setValidator(Validator validator) {
|
||||
// Unwrap to the native Validator with forExecutables support
|
||||
if (validator instanceof LocalValidatorFactoryBean) {
|
||||
this.validator = ((LocalValidatorFactoryBean) validator).getValidator();
|
||||
}
|
||||
else if (validator instanceof SpringValidatorAdapter) {
|
||||
this.validator = validator.unwrap(Validator.class);
|
||||
}
|
||||
else {
|
||||
this.validator = validator;
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeMap;
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.ValidationException;
|
||||
import javax.validation.executable.ExecutableValidator;
|
||||
import javax.validation.metadata.BeanDescriptor;
|
||||
import javax.validation.metadata.ConstraintDescriptor;
|
||||
@@ -303,7 +304,16 @@ public class SpringValidatorAdapter implements SmartValidator, javax.validation.
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T unwrap(@Nullable Class<T> type) {
|
||||
Assert.state(this.targetValidator != null, "No target Validator set");
|
||||
return (type != null ? this.targetValidator.unwrap(type) : (T) this.targetValidator);
|
||||
try {
|
||||
return (type != null ? this.targetValidator.unwrap(type) : (T) this.targetValidator);
|
||||
}
|
||||
catch (ValidationException ex) {
|
||||
// ignore if just being asked for plain Validator
|
||||
if (javax.validation.Validator.class == type) {
|
||||
return (T) this.targetValidator;
|
||||
}
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user