Catch defensively validator exceptions in AOT processing
An ArrayIndexOutOfBoundsException is thrown by Validator.getConstraintsForClass when processing Kotlin beans with extensions functions (Kotlin or Hibernate Validator bug). This commit catches those exceptions and report them as warning without the full stactrace, and report as well other ones thrown as errors with the full stracktrace. Closes gh-30037
This commit is contained in:
@@ -30,6 +30,8 @@ import jakarta.validation.metadata.MethodDescriptor;
|
||||
import jakarta.validation.metadata.MethodType;
|
||||
import jakarta.validation.metadata.ParameterDescriptor;
|
||||
import jakarta.validation.metadata.PropertyDescriptor;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.aot.generate.GenerationContext;
|
||||
import org.springframework.aot.hint.MemberCategory;
|
||||
@@ -37,6 +39,7 @@ import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
|
||||
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
|
||||
import org.springframework.beans.factory.aot.BeanRegistrationCode;
|
||||
import org.springframework.beans.factory.support.RegisteredBean;
|
||||
import org.springframework.core.KotlinDetector;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
@@ -52,6 +55,8 @@ class BeanValidationBeanRegistrationAotProcessor implements BeanRegistrationAotP
|
||||
private static final boolean isBeanValidationPresent = ClassUtils.isPresent(
|
||||
"jakarta.validation.Validation", BeanValidationBeanRegistrationAotProcessor.class.getClassLoader());
|
||||
|
||||
private static final Log logger = LogFactory.getLog(BeanValidationBeanRegistrationAotProcessor.class);
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
|
||||
@@ -67,7 +72,22 @@ class BeanValidationBeanRegistrationAotProcessor implements BeanRegistrationAotP
|
||||
|
||||
@Nullable
|
||||
public static BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
|
||||
BeanDescriptor descriptor = validator.getConstraintsForClass(registeredBean.getBeanClass());
|
||||
BeanDescriptor descriptor;
|
||||
try {
|
||||
descriptor = validator.getConstraintsForClass(registeredBean.getBeanClass());
|
||||
}
|
||||
catch (RuntimeException ex) {
|
||||
if (KotlinDetector.isKotlinType(registeredBean.getBeanClass()) && ex instanceof ArrayIndexOutOfBoundsException) {
|
||||
// See https://hibernate.atlassian.net/browse/HV-1796 and https://youtrack.jetbrains.com/issue/KT-40857
|
||||
logger.warn("Skipping validation constraint hint inference for bean " + registeredBean.getBeanName() +
|
||||
" due to an ArrayIndexOutOfBoundsException at validator level");
|
||||
}
|
||||
else {
|
||||
logger.error("Skipping validation constraint hint inference for bean " +
|
||||
registeredBean.getBeanName(), ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
Set<ConstraintDescriptor<?>> constraintDescriptors = new HashSet<>();
|
||||
for (MethodDescriptor methodDescriptor : descriptor.getConstrainedMethods(MethodType.NON_GETTER, MethodType.GETTER)) {
|
||||
for (ParameterDescriptor parameterDescriptor : methodDescriptor.getParameterDescriptors()) {
|
||||
|
||||
Reference in New Issue
Block a user