Skip to content

Commit fd16ae7

Browse files
committed
Unwrap a validator instance of specified type contained in SpringValidatorAdapter
See gh-37081.
1 parent ed9071c commit fd16ae7

File tree

1 file changed

+17
-0
lines changed
  • spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation

1 file changed

+17
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/validation/ValidatorAdapter.java

+17
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
*
4040
* @author Stephane Nicoll
4141
* @author Phillip Webb
42+
* @author Zisis Pavloudis
4243
* @since 2.0.0
4344
*/
4445
public class ValidatorAdapter implements SmartValidator, ApplicationContextAware, InitializingBean, DisposableBean {
@@ -153,4 +154,20 @@ private static Validator wrap(Validator validator, boolean existingBean) {
153154
return validator;
154155
}
155156

157+
@Override
158+
@SuppressWarnings("unchecked")
159+
public <T> T unwrap(Class<T> type) {
160+
if (this.target == null) {
161+
return null;
162+
}
163+
if (type.isAssignableFrom(this.target.getClass())) {
164+
if (this.target instanceof SpringValidatorAdapter adapter) {
165+
return adapter.unwrap(type);
166+
}
167+
return (T) this.target;
168+
}
169+
170+
throw new ValidationException("Cannot unwrap to " + type);
171+
}
172+
156173
}

0 commit comments

Comments
 (0)