Fix NPE in Q type resolution for primitive and wrapper types.

Closes: #3284
See: spring-projects/spring-data-mongodb#4958

Signed-off-by: ckdgus08 <ckdgus0808@naver.com>
This commit is contained in:
ckdgus0808
2025-05-04 16:16:58 +09:00
committed by Christoph Strobl
parent c595f6bc3e
commit ee5f3191d8
2 changed files with 65 additions and 0 deletions

View File

@@ -41,6 +41,10 @@ public class QTypeContributor {
return;
}
if (type.isPrimitive() || type.isArray()) {
return;
}
String queryClassName = getQueryClassName(type);
if (ClassUtils.isPresent(queryClassName, classLoader)) {

View File

@@ -26,10 +26,16 @@ import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.data.aot.sample.ConfigWithQuerydslPredicateExecutor.Person;
import org.springframework.data.aot.sample.QConfigWithQuerydslPredicateExecutor_Person;
import org.springframework.data.classloadersupport.HidingClassLoader;
import org.springframework.data.querydsl.User;
import org.springframework.javapoet.ClassName;
import com.querydsl.core.types.EntityPath;
/**
* Unit tests for {@link QTypeContributor}.
*
* @author ckdgus08
*/
class QTypeContributorUnitTests {
@Test // GH-2721
@@ -68,4 +74,59 @@ class QTypeContributorUnitTests {
assertThat(generationContext.getRuntimeHints()).matches(
RuntimeHintsPredicates.reflection().onType(QConfigWithQuerydslPredicateExecutor_Person.class).negate());
}
@Test // DATAMONGO-4958
void doesNotAddQTypeHintForArrayType() {
GenerationContext generationContext = new DefaultGenerationContext(
new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles());
QTypeContributor.contributeEntityPath(Person[].class, generationContext, HidingClassLoader.hideTypes());
assertThat(generationContext.getRuntimeHints()).matches(
RuntimeHintsPredicates.reflection().onType(QConfigWithQuerydslPredicateExecutor_Person.class).negate());
assertThat(generationContext.getRuntimeHints()).matches(
RuntimeHintsPredicates.reflection().onType(QConfigWithQuerydslPredicateExecutor_Person[].class).negate());
}
@Test // DATAMONGO-4958
void addsQTypeHintForQUserType() {
GenerationContext generationContext = new DefaultGenerationContext(
new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles());
QTypeContributor.contributeEntityPath(User.class, generationContext, getClass().getClassLoader());
var qUserHintCount = generationContext.getRuntimeHints().reflection().typeHints()
.filter(hint -> hint.getType().getName().equals("org.springframework.data.querydsl.QUser"))
.count();
assertThat(qUserHintCount).isEqualTo(1);
}
@Test // DATAMONGO-4958
void doesNotAddQTypeHintForQUserArrayType() {
GenerationContext generationContext = new DefaultGenerationContext(
new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles());
var classLoader = getClass().getClassLoader();
QTypeContributor.contributeEntityPath(User[].class, generationContext, classLoader);
assertThat(generationContext.getRuntimeHints().reflection().typeHints()).isEmpty();
var qUserHintCount = generationContext.getRuntimeHints().reflection().typeHints()
.filter(hint -> hint.getType().getName().equals("org.springframework.data.querydsl.QUser"))
.count();
assertThat(qUserHintCount).isEqualTo(0);
}
@Test // DATAMONGO-4958
void doesNotAddQTypeHintForPrimitiveType() {
GenerationContext generationContext = new DefaultGenerationContext(
new ClassNameGenerator(ClassName.get(this.getClass())), new InMemoryGeneratedFiles());
QTypeContributor.contributeEntityPath(int.class, generationContext, getClass().getClassLoader());
assertThat(generationContext.getRuntimeHints().reflection().typeHints()).isEmpty();
}
}