Skip to content

Commit 9b5c1e8

Browse files
gregturnodrotbohm
authored andcommitted
Extend query method special parameter types to any subclasses.
Spring Data Commons has a hard-coded list of special types than can be included in query methods including Pageable and Sort. A custom finder with PageRequest, even though it extends Pageable, will fail when it would work fine with a narrowed input. This extends the list using an assignability check. Related: spring-projects/spring-data-jpa#2013 See spring-projects#2626.
1 parent 463cb47 commit 9b5c1e8

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

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

+21-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* @author Oliver Gierke
4343
* @author Mark Paluch
4444
* @author Jens Schauder
45+
* @author Greg Turnquist
4546
*/
4647
public class Parameter {
4748

@@ -79,7 +80,7 @@ protected Parameter(MethodParameter parameter) {
7980
this.parameter = parameter;
8081
this.parameterType = potentiallyUnwrapParameterType(parameter);
8182
this.isDynamicProjectionParameter = isDynamicProjectionParameter(parameter);
82-
this.name = TYPES.contains(parameter.getParameterType()) ? Lazy.of(Optional.empty()) : Lazy.of(() -> {
83+
this.name = isSpecialParameterType(parameter.getParameterType()) ? Lazy.of(Optional.empty()) : Lazy.of(() -> {
8384
Param annotation = parameter.getParameterAnnotation(Param.class);
8485
return Optional.ofNullable(annotation == null ? parameter.getParameterName() : annotation.value());
8586
});
@@ -92,7 +93,7 @@ protected Parameter(MethodParameter parameter) {
9293
* @see #TYPES
9394
*/
9495
public boolean isSpecialParameter() {
95-
return isDynamicProjectionParameter || TYPES.contains(parameter.getParameterType());
96+
return isDynamicProjectionParameter || isSpecialParameterType(parameter.getParameterType());
9697
}
9798

9899
/**
@@ -273,4 +274,22 @@ private static Class<?> potentiallyUnwrapParameterType(MethodParameter parameter
273274

274275
return originalType;
275276
}
277+
278+
/**
279+
* Identify is a given {@link Class} is either part of {@code TYPES} or an instanceof of one of its members. For
280+
* example, {@code PageRequest} is an instance of {@code Pageable} (a member of {@code TYPES}).
281+
*
282+
* @param parameterType must not be {@literal null}.
283+
* @return boolean
284+
*/
285+
private static boolean isSpecialParameterType(Class<?> parameterType) {
286+
287+
for (Class<?> specialParameterType : TYPES) {
288+
if (specialParameterType.isAssignableFrom(parameterType)) {
289+
return true;
290+
}
291+
}
292+
293+
return false;
294+
}
276295
}

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

+16
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* Unit tests for {@link ParametersParameterAccessor}.
3030
*
3131
* @author Oliver Gierke
32+
* @author Greg Turnquist
3233
*/
3334
class ParametersParameterAccessorUnitTests {
3435

@@ -75,12 +76,27 @@ void iteratesonlyOverBindableValues() throws Exception {
7576
new Object[] { PageRequest.of(0, 10), "Foo" });
7677

7778
assertThat(accessor).hasSize(1);
79+
assertThat(accessor.getBindableValue(0)).isEqualTo("Foo");
80+
}
81+
82+
@Test // #2626
83+
void handlesPageRequestAsAParameterType() throws NoSuchMethodException {
84+
85+
Method method = Sample.class.getMethod("methodWithPageRequest", PageRequest.class, String.class);
86+
DefaultParameters parameters = new DefaultParameters(method);
87+
88+
ParameterAccessor accessor = new ParametersParameterAccessor(parameters, new Object[] { PageRequest.of(0, 10), "Foo" });
89+
90+
assertThat(accessor).hasSize(1);
91+
assertThat(accessor.getBindableValue(0)).isEqualTo("Foo");
7892
}
7993

8094
interface Sample {
8195

8296
void method(String string, int integer);
8397

8498
void method(Pageable pageable, String string);
99+
100+
void methodWithPageRequest(PageRequest pageRequest, String string);
85101
}
86102
}

0 commit comments

Comments
 (0)