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
This commit is contained in:
Oliver Drotbohm
2022-01-25 09:15:52 +01:00
parent 3312519784
commit 59fd61aad8
3 changed files with 12 additions and 1 deletions

View File

@@ -176,7 +176,7 @@ public class JpaQueryCreator extends AbstractQueryCreator<CriteriaQuery<? extend
selections.add(toExpressionRecursively(root, path, true).alias(property));
}
Class<?> typeToRead = returnedType.getTypeToRead();
Class<?> typeToRead = returnedType.getReturnedType();
query = typeToRead.isInterface()
? query.multiselect(selections)

View File

@@ -2621,6 +2621,14 @@ public class UserRepositoryTests {
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();

View File

@@ -614,6 +614,9 @@ public interface UserRepository
// #2363
List<NameOnlyDto> findAllDtoProjectedBy();
// GH-2408
List<NameOnly> findAllInterfaceProjectedBy();
interface RolesAndFirstname {
String getFirstname();