Browse Source

Merge branch '6.2.x'

pull/33956/head
Sam Brannen 1 year ago
parent
commit
102cc72fd3
  1. 23
      spring-context/src/main/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessor.java
  2. 27
      spring-context/src/test/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessorTests.java

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -18,7 +18,6 @@ package org.springframework.validation.beanvalidation;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
@ -107,7 +106,7 @@ class BeanValidationBeanRegistrationAotProcessor implements BeanRegistrationAotP
Set<Class<?>> validatedClasses = new HashSet<>(); Set<Class<?>> validatedClasses = new HashSet<>();
Set<Class<? extends ConstraintValidator<?, ?>>> constraintValidatorClasses = new HashSet<>(); Set<Class<? extends ConstraintValidator<?, ?>>> constraintValidatorClasses = new HashSet<>();
processAheadOfTime(beanClass, validatedClasses, constraintValidatorClasses); processAheadOfTime(beanClass, new HashSet<>(), validatedClasses, constraintValidatorClasses);
if (!validatedClasses.isEmpty() || !constraintValidatorClasses.isEmpty()) { if (!validatedClasses.isEmpty() || !constraintValidatorClasses.isEmpty()) {
return new AotContribution(validatedClasses, constraintValidatorClasses); return new AotContribution(validatedClasses, constraintValidatorClasses);
@ -115,10 +114,14 @@ class BeanValidationBeanRegistrationAotProcessor implements BeanRegistrationAotP
return null; return null;
} }
private static void processAheadOfTime(Class<?> clazz, Collection<Class<?>> validatedClasses, private static void processAheadOfTime(Class<?> clazz, Set<Class<?>> visitedClasses, Set<Class<?>> validatedClasses,
Collection<Class<? extends ConstraintValidator<?, ?>>> constraintValidatorClasses) { Set<Class<? extends ConstraintValidator<?, ?>>> constraintValidatorClasses) {
Assert.notNull(validator, "Validator cannot be null");
Assert.notNull(validator, "Validator can't be null"); if (!visitedClasses.add(clazz)) {
return;
}
BeanDescriptor descriptor; BeanDescriptor descriptor;
try { try {
@ -149,12 +152,12 @@ class BeanValidationBeanRegistrationAotProcessor implements BeanRegistrationAotP
ReflectionUtils.doWithFields(clazz, field -> { ReflectionUtils.doWithFields(clazz, field -> {
Class<?> type = field.getType(); Class<?> type = field.getType();
if (Iterable.class.isAssignableFrom(type) || List.class.isAssignableFrom(type) || Optional.class.isAssignableFrom(type)) { if (Iterable.class.isAssignableFrom(type) || Optional.class.isAssignableFrom(type)) {
ResolvableType resolvableType = ResolvableType.forField(field); ResolvableType resolvableType = ResolvableType.forField(field);
Class<?> genericType = resolvableType.getGeneric(0).toClass(); Class<?> genericType = resolvableType.getGeneric(0).toClass();
if (shouldProcess(genericType)) { if (shouldProcess(genericType)) {
validatedClasses.add(clazz); validatedClasses.add(clazz);
processAheadOfTime(genericType, validatedClasses, constraintValidatorClasses); processAheadOfTime(genericType, visitedClasses, validatedClasses, constraintValidatorClasses);
} }
} }
if (Map.class.isAssignableFrom(type)) { if (Map.class.isAssignableFrom(type)) {
@ -163,11 +166,11 @@ class BeanValidationBeanRegistrationAotProcessor implements BeanRegistrationAotP
Class<?> valueGenericType = resolvableType.getGeneric(1).toClass(); Class<?> valueGenericType = resolvableType.getGeneric(1).toClass();
if (shouldProcess(keyGenericType)) { if (shouldProcess(keyGenericType)) {
validatedClasses.add(clazz); validatedClasses.add(clazz);
processAheadOfTime(keyGenericType, validatedClasses, constraintValidatorClasses); processAheadOfTime(keyGenericType, visitedClasses, validatedClasses, constraintValidatorClasses);
} }
if (shouldProcess(valueGenericType)) { if (shouldProcess(valueGenericType)) {
validatedClasses.add(clazz); validatedClasses.add(clazz);
processAheadOfTime(valueGenericType, validatedClasses, constraintValidatorClasses); processAheadOfTime(valueGenericType, visitedClasses, validatedClasses, constraintValidatorClasses);
} }
} }
}); });

27
spring-context/src/test/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessorTests.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2023 the original author or authors. * Copyright 2002-2024 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,6 +22,8 @@ import java.lang.annotation.Retention;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Optional;
import jakarta.validation.Constraint; import jakarta.validation.Constraint;
import jakarta.validation.ConstraintValidator; import jakarta.validation.ConstraintValidator;
@ -31,6 +33,8 @@ import jakarta.validation.Valid;
import jakarta.validation.constraints.Pattern; import jakarta.validation.constraints.Pattern;
import org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator; import org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.aot.generate.GenerationContext; import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.hint.MemberCategory; import org.springframework.aot.hint.MemberCategory;
@ -121,6 +125,15 @@ class BeanValidationBeanRegistrationAotProcessorTests {
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.generationContext.getRuntimeHints()); .withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.generationContext.getRuntimeHints());
} }
@ParameterizedTest // gh-33936
@ValueSource(classes = {BeanWithRecursiveIterable.class, BeanWithRecursiveMap.class, BeanWithRecursiveOptional.class})
void shouldProcessRecursiveGenericsWithoutInfiniteRecursion(Class<?> beanClass) {
process(beanClass);
assertThat(this.generationContext.getRuntimeHints().reflection().typeHints()).hasSize(1);
assertThat(RuntimeHintsPredicates.reflection().onType(beanClass)
.withMemberCategory(MemberCategory.DECLARED_FIELDS)).accepts(this.generationContext.getRuntimeHints());
}
private void process(Class<?> beanClass) { private void process(Class<?> beanClass) {
BeanRegistrationAotContribution contribution = createContribution(beanClass); BeanRegistrationAotContribution contribution = createContribution(beanClass);
if (contribution != null) { if (contribution != null) {
@ -244,4 +257,16 @@ class BeanValidationBeanRegistrationAotProcessorTests {
} }
} }
static class BeanWithRecursiveIterable {
Iterable<BeanWithRecursiveIterable> iterable;
}
static class BeanWithRecursiveMap {
Map<BeanWithRecursiveMap, BeanWithRecursiveMap> map;
}
static class BeanWithRecursiveOptional {
Optional<BeanWithRecursiveOptional> optional;
}
} }

Loading…
Cancel
Save