Skip to content

Commit

Permalink
Fix potential NullPointerException in JpaQueryCreator.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
odrotbohm committed Jan 25, 2022
1 parent 408237b commit a1faffe
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ protected CriteriaQuery<? extends Object> complete(@Nullable Predicate predicate
selections.add(toExpressionRecursively(root, path, true).alias(property));
}

Class<?> typeToRead = returnedType.getTypeToRead();
Class<?> typeToRead = returnedType.getReturnedType();

query = typeToRead.isInterface()
? query.multiselect(selections)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2630,6 +2630,14 @@ void readsDtoProjections() {
assertThat(repository.findAllDtoProjectedBy()).hasSize(4);
}

@Test // GH-2408, GH-2363
void readsDerivedInterfaceProjections() {

flushTestUsers();

assertThat(repository.findAllInterfaceProjectedBy()).hasSize(4);
}

private Page<User> executeSpecWithSort(Sort sort) {

flushTestUsers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,9 @@ Page<User> findAllOrderedBySpecialNameMultipleParams(@Param("name") String name,
// #2363
List<NameOnlyDto> findAllDtoProjectedBy();

// GH-2408
List<NameOnly> findAllInterfaceProjectedBy();

interface RolesAndFirstname {

String getFirstname();
Expand Down

0 comments on commit a1faffe

Please sign in to comment.