Skip validation constraint hint inference if no provider available
Closes gh-30130
This commit is contained in:
parent
3af8efbdc7
commit
88bc504625
|
|
@ -21,6 +21,7 @@ import java.util.HashSet;
|
|||
import java.util.Set;
|
||||
|
||||
import jakarta.validation.ConstraintValidator;
|
||||
import jakarta.validation.NoProviderFoundException;
|
||||
import jakarta.validation.Validation;
|
||||
import jakarta.validation.Validator;
|
||||
import jakarta.validation.metadata.BeanDescriptor;
|
||||
|
|
@ -48,6 +49,7 @@ import org.springframework.util.ClassUtils;
|
|||
* required for {@link ConstraintValidator}s.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @author Juergen Hoeller
|
||||
* @since 6.0.5
|
||||
*/
|
||||
class BeanValidationBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
|
||||
|
|
@ -57,8 +59,9 @@ class BeanValidationBeanRegistrationAotProcessor implements BeanRegistrationAotP
|
|||
|
||||
private static final Log logger = LogFactory.getLog(BeanValidationBeanRegistrationAotProcessor.class);
|
||||
|
||||
@Nullable
|
||||
|
||||
@Override
|
||||
@Nullable
|
||||
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
|
||||
if (isBeanValidationPresent) {
|
||||
return BeanValidationDelegate.processAheadOfTime(registeredBean);
|
||||
|
|
@ -66,12 +69,29 @@ class BeanValidationBeanRegistrationAotProcessor implements BeanRegistrationAotP
|
|||
return null;
|
||||
}
|
||||
|
||||
|
||||
private static class BeanValidationDelegate {
|
||||
|
||||
private static final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
@Nullable
|
||||
private static final Validator validator = getValidatorIfAvailable();
|
||||
|
||||
@Nullable
|
||||
private static Validator getValidatorIfAvailable() {
|
||||
try {
|
||||
return Validation.buildDefaultValidatorFactory().getValidator();
|
||||
}
|
||||
catch (NoProviderFoundException ex) {
|
||||
logger.info("No Bean Validation provider available - skipping validation constraint hint inference");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
|
||||
if (validator == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
BeanDescriptor descriptor;
|
||||
try {
|
||||
descriptor = validator.getConstraintsForClass(registeredBean.getBeanClass());
|
||||
|
|
@ -88,6 +108,7 @@ class BeanValidationBeanRegistrationAotProcessor implements BeanRegistrationAotP
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Set<ConstraintDescriptor<?>> constraintDescriptors = new HashSet<>();
|
||||
for (MethodDescriptor methodDescriptor : descriptor.getConstrainedMethods(MethodType.NON_GETTER, MethodType.GETTER)) {
|
||||
for (ParameterDescriptor parameterDescriptor : methodDescriptor.getParameterDescriptors()) {
|
||||
|
|
@ -107,9 +128,9 @@ class BeanValidationBeanRegistrationAotProcessor implements BeanRegistrationAotP
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static class BeanValidationBeanRegistrationAotContribution implements BeanRegistrationAotContribution {
|
||||
|
||||
private final Collection<ConstraintDescriptor<?>> constraintDescriptors;
|
||||
|
|
|
|||
Loading…
Reference in New Issue