Skip to content

Commit a1faffe

Browse files
committed
Fix potential NullPointerException in JpaQueryCreator.
With the commit for GH-2363, we introduced an unguarded call to ReturnedType.getTypeToRead(), which could return null under certain conditions. Unfortunately Spring Data JPA dod not contain a test case that triggered that scenario. This commit switches to ReturnedType.getReturnedType() which is non-nullable and lets us inspect the calls results for interfaces, which we need to create the JPA query properly. Fixes GH-2408
1 parent 408237b commit a1faffe

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpaQueryCreator.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ protected CriteriaQuery<? extends Object> complete(@Nullable Predicate predicate
164164
selections.add(toExpressionRecursively(root, path, true).alias(property));
165165
}
166166

167-
Class<?> typeToRead = returnedType.getTypeToRead();
167+
Class<?> typeToRead = returnedType.getReturnedType();
168168

169169
query = typeToRead.isInterface()
170170
? query.multiselect(selections)

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java

+8
Original file line numberDiff line numberDiff line change
@@ -2630,6 +2630,14 @@ void readsDtoProjections() {
26302630
assertThat(repository.findAllDtoProjectedBy()).hasSize(4);
26312631
}
26322632

2633+
@Test // GH-2408, GH-2363
2634+
void readsDerivedInterfaceProjections() {
2635+
2636+
flushTestUsers();
2637+
2638+
assertThat(repository.findAllInterfaceProjectedBy()).hasSize(4);
2639+
}
2640+
26332641
private Page<User> executeSpecWithSort(Sort sort) {
26342642

26352643
flushTestUsers();

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/sample/UserRepository.java

+3
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,9 @@ Page<User> findAllOrderedBySpecialNameMultipleParams(@Param("name") String name,
614614
// #2363
615615
List<NameOnlyDto> findAllDtoProjectedBy();
616616

617+
// GH-2408
618+
List<NameOnly> findAllInterfaceProjectedBy();
619+
617620
interface RolesAndFirstname {
618621

619622
String getFirstname();

0 commit comments

Comments
 (0)