Browse Source

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>
3.4.x
ckdgus0808 8 months ago committed by Christoph Strobl
parent
commit
d496a65303
No known key found for this signature in database
GPG Key ID: E6054036D0C37A4B
  1. 4
      src/main/java/org/springframework/data/util/QTypeContributor.java
  2. 61
      src/test/java/org/springframework/data/util/QTypeContributorUnitTests.java

4
src/main/java/org/springframework/data/util/QTypeContributor.java

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

61
src/test/java/org/springframework/data/util/QTypeContributorUnitTests.java

@ -26,10 +26,16 @@ import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; @@ -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 { @@ -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();
}
}

Loading…
Cancel
Save