From a6a9fcd84c9d2a2e51a653ff2e33f4a57b84dc7c Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Thu, 29 Jun 2023 13:20:02 +0300 Subject: [PATCH] Improve generic resolution support in Spring Data JPA Fixes: #34350 --- .../deployment/generate/DerivedMethodsAdder.java | 16 ++++++++++++---- .../data/jpa/IntermediatePostRepository.java | 2 +- .../it/spring/data/jpa/PostRepository.java | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/extensions/spring-data-jpa/deployment/src/main/java/io/quarkus/spring/data/deployment/generate/DerivedMethodsAdder.java b/extensions/spring-data-jpa/deployment/src/main/java/io/quarkus/spring/data/deployment/generate/DerivedMethodsAdder.java index 0fd83bbf1abde..7be969bce4be2 100644 --- a/extensions/spring-data-jpa/deployment/src/main/java/io/quarkus/spring/data/deployment/generate/DerivedMethodsAdder.java +++ b/extensions/spring-data-jpa/deployment/src/main/java/io/quarkus/spring/data/deployment/generate/DerivedMethodsAdder.java @@ -297,12 +297,20 @@ private Type extractResultType(ClassInfo repositoryClassInfo, MethodInfo method) // this is accomplished by resolving the generic type from the interface we are actually implementing TypeVariable resultTypeVariable = resultType.asTypeVariable(); List interfaceTypeVariables = method.declaringClass().typeParameters(); - if (interfaceTypeVariables.size() == 1 && interfaceTypeVariables.get(0) - .equals(resultTypeVariable)) { + + int matchingIndex = -1; + for (int i = 0; i < interfaceTypeVariables.size(); i++) { + if (interfaceTypeVariables.get(i).equals(resultTypeVariable)) { + matchingIndex = i; + break; + } + } + + if (matchingIndex != -1) { List resolveTypeParameters = JandexUtil.resolveTypeParameters(repositoryClassInfo.name(), method.declaringClass().name(), index); - if (resolveTypeParameters.size() == 1) { - return resolveTypeParameters.get(0); + if (matchingIndex < resolveTypeParameters.size()) { + return resolveTypeParameters.get(matchingIndex); } } } diff --git a/integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/IntermediatePostRepository.java b/integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/IntermediatePostRepository.java index 733757e3191fc..f2d6a7ed8e506 100644 --- a/integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/IntermediatePostRepository.java +++ b/integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/IntermediatePostRepository.java @@ -9,7 +9,7 @@ * Used to ensure that entity relationships work correctly */ @NoRepositoryBean -public interface IntermediatePostRepository extends BypassHolderRepository { +public interface IntermediatePostRepository extends BypassHolderRepository { List findByPostedBefore(ZonedDateTime zdt); } diff --git a/integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/PostRepository.java b/integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/PostRepository.java index 643420fd825f3..0c3d7494e54ba 100644 --- a/integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/PostRepository.java +++ b/integration-tests/spring-data-jpa/src/main/java/io/quarkus/it/spring/data/jpa/PostRepository.java @@ -5,7 +5,7 @@ /** * Used to ensure that entity relationships work correctly */ -public interface PostRepository extends IntermediatePostRepository { +public interface PostRepository extends IntermediatePostRepository { List findAllByOrganization(String organization);