Skip to content

Commit 5dad677

Browse files
odrotbohmmp911de
authored andcommitted
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
1 parent 7189e07 commit 5dad677

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/main/java/org/springframework/data/repository/query/Parameter.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ boolean isSort() {
215215
*/
216216
private static boolean isDynamicProjectionParameter(MethodParameter parameter) {
217217

218+
if (parameter.hasParameterAnnotation(Param.class)) {
219+
return false;
220+
}
221+
218222
Method method = parameter.getMethod();
219223

220224
if (method == null) {
@@ -232,7 +236,8 @@ private static boolean isDynamicProjectionParameter(MethodParameter parameter) {
232236
TypeInformation<Object> returnType = ClassTypeInformation.fromReturnTypeOf(method);
233237

234238
return bound
235-
.equals(QueryExecutionConverters.unwrapWrapperTypes(ReactiveWrapperConverters.unwrapWrapperTypes(returnType)));
239+
.equals(QueryExecutionConverters
240+
.unwrapWrapperTypes(ReactiveWrapperConverters.unwrapWrapperTypes(returnType)));
236241
}
237242

238243
/**

src/test/java/org/springframework/data/repository/query/ParameterUnitTests.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20+
import java.lang.reflect.Method;
2021
import java.util.Collections;
2122
import java.util.List;
2223
import java.util.stream.Stream;
@@ -48,14 +49,20 @@ void classParameterWithSameTypeParameterAsReturnedStreamIsDynamicProjectionParam
4849
assertThat(parameter.isDynamicProjectionParameter()).isTrue();
4950
}
5051

52+
@Test // #1452
53+
void doesNotConsiderAtParamAnnotatedClassParameterDynamicProjectionOne() throws Exception {
54+
55+
Parameter parameter = new Parameter(getMethodParameter("atParamOnClass"));
56+
57+
assertThat(parameter.isDynamicProjectionParameter()).isFalse();
58+
}
59+
5160
@NotNull
5261
private MethodParameter getMethodParameter(String methodName) throws NoSuchMethodException {
53-
return new MethodParameter( //
54-
this.getClass().getDeclaredMethod( //
55-
methodName, //
56-
Class.class //
57-
), //
58-
0);
62+
63+
Method method = getClass().getDeclaredMethod(methodName, Class.class);
64+
65+
return new MethodParameter(method, 0);
5966
}
6067

6168
<T> List<T> dynamicProjectionWithList(Class<T> type) {
@@ -65,4 +72,8 @@ <T> List<T> dynamicProjectionWithList(Class<T> type) {
6572
<T> Stream<T> dynamicProjectionWithStream(Class<T> type) {
6673
return Stream.empty();
6774
}
75+
76+
<T> T atParamOnClass(@Param("type") Class<T> type) {
77+
return null;
78+
}
6879
}

0 commit comments

Comments
 (0)