Skip to content

Commit 59fd61a

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 3312519 commit 59fd61a

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

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

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

179-
Class<?> typeToRead = returnedType.getTypeToRead();
179+
Class<?> typeToRead = returnedType.getReturnedType();
180180

181181
query = typeToRead.isInterface()
182182
? query.multiselect(selections)

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

+8
Original file line numberDiff line numberDiff line change
@@ -2621,6 +2621,14 @@ void readsDtoProjections() {
26212621
assertThat(repository.findAllDtoProjectedBy()).hasSize(4);
26222622
}
26232623

2624+
@Test // GH-2408, GH-2363
2625+
void readsDerivedInterfaceProjections() {
2626+
2627+
flushTestUsers();
2628+
2629+
assertThat(repository.findAllInterfaceProjectedBy()).hasSize(4);
2630+
}
2631+
26242632
private Page<User> executeSpecWithSort(Sort sort) {
26252633

26262634
flushTestUsers();

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)