From 5dad677bf166ad743b54aa5926f33428ff7f8342 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Wed, 1 Feb 2023 17:31:28 +0100 Subject: [PATCH] Query method parameters annotated with `@Param` are not considered projection parameters. This is a 2.7.x adapted fix for #1452 (the actual fix for 3.x contained in #2770). We temporarily support the use of @Param on Class parameters to allow them to be be used as actual query parameters. On 3.0.x the general parameter handling gets smarter so that this mitigation can be phased out pretty quickly, but this here seems to be a simple enough fix for those who cannot upgrade to 3.0 any time soon. Closes #2770. See #1452. Original pull request: #2772 --- .../data/repository/query/Parameter.java | 7 +++++- .../repository/query/ParameterUnitTests.java | 23 ++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/springframework/data/repository/query/Parameter.java b/src/main/java/org/springframework/data/repository/query/Parameter.java index d1603616f..2116f5729 100644 --- a/src/main/java/org/springframework/data/repository/query/Parameter.java +++ b/src/main/java/org/springframework/data/repository/query/Parameter.java @@ -215,6 +215,10 @@ public class Parameter { */ private static boolean isDynamicProjectionParameter(MethodParameter parameter) { + if (parameter.hasParameterAnnotation(Param.class)) { + return false; + } + Method method = parameter.getMethod(); if (method == null) { @@ -232,7 +236,8 @@ public class Parameter { TypeInformation returnType = ClassTypeInformation.fromReturnTypeOf(method); return bound - .equals(QueryExecutionConverters.unwrapWrapperTypes(ReactiveWrapperConverters.unwrapWrapperTypes(returnType))); + .equals(QueryExecutionConverters + .unwrapWrapperTypes(ReactiveWrapperConverters.unwrapWrapperTypes(returnType))); } /** diff --git a/src/test/java/org/springframework/data/repository/query/ParameterUnitTests.java b/src/test/java/org/springframework/data/repository/query/ParameterUnitTests.java index 72e12ed36..4e8ed171a 100644 --- a/src/test/java/org/springframework/data/repository/query/ParameterUnitTests.java +++ b/src/test/java/org/springframework/data/repository/query/ParameterUnitTests.java @@ -17,6 +17,7 @@ package org.springframework.data.repository.query; import static org.assertj.core.api.Assertions.*; +import java.lang.reflect.Method; import java.util.Collections; import java.util.List; import java.util.stream.Stream; @@ -48,14 +49,20 @@ class ParameterUnitTests { assertThat(parameter.isDynamicProjectionParameter()).isTrue(); } + @Test // #1452 + void doesNotConsiderAtParamAnnotatedClassParameterDynamicProjectionOne() throws Exception { + + Parameter parameter = new Parameter(getMethodParameter("atParamOnClass")); + + assertThat(parameter.isDynamicProjectionParameter()).isFalse(); + } + @NotNull private MethodParameter getMethodParameter(String methodName) throws NoSuchMethodException { - return new MethodParameter( // - this.getClass().getDeclaredMethod( // - methodName, // - Class.class // - ), // - 0); + + Method method = getClass().getDeclaredMethod(methodName, Class.class); + + return new MethodParameter(method, 0); } List dynamicProjectionWithList(Class type) { @@ -65,4 +72,8 @@ class ParameterUnitTests { Stream dynamicProjectionWithStream(Class type) { return Stream.empty(); } + + T atParamOnClass(@Param("type") Class type) { + return null; + } }