Browse Source

DATACMNS-520 - Fixed @Param validation in Parameters.

Parameters erroneously rejected @Param annotated query method parameters that were preceded by a non-bindable type (like Pageable or Sort) as it checked for a 0 index of the parameter. We now manually keep an index in the check itself and have removed the Parameter.isFirst() method as it's not used anywhere else.
1.8.x
Oliver Gierke 12 years ago
parent
commit
218a820c95
  1. 9
      src/main/java/org/springframework/data/repository/query/Parameter.java
  2. 5
      src/main/java/org/springframework/data/repository/query/Parameters.java
  3. 12
      src/test/java/org/springframework/data/repository/query/ParametersUnitTests.java

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

@ -50,15 +50,6 @@ public class Parameter { @@ -50,15 +50,6 @@ public class Parameter {
this.parameter = parameter;
}
/**
* Returns whether the {@link Parameter} is the first one.
*
* @return
*/
boolean isFirst() {
return getIndex() == 0;
}
/**
* Returns whether the parameter is a special parameter.
*

5
src/main/java/org/springframework/data/repository/query/Parameters.java

@ -258,15 +258,18 @@ public abstract class Parameters<S extends Parameters<S, T>, T extends Parameter @@ -258,15 +258,18 @@ public abstract class Parameters<S extends Parameters<S, T>, T extends Parameter
private void assertEitherAllParamAnnotatedOrNone() {
boolean nameFound = false;
int index = 0;
for (T parameter : this.getBindableParameters()) {
if (parameter.isNamedParameter()) {
Assert.isTrue(nameFound || parameter.isFirst(), ALL_OR_NOTHING);
Assert.isTrue(nameFound || index == 0, ALL_OR_NOTHING);
nameFound = true;
} else {
Assert.isTrue(!nameFound, ALL_OR_NOTHING);
}
index++;
}
}

12
src/test/java/org/springframework/data/repository/query/ParametersUnitTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2008-2013 the original author or authors.
* Copyright 2008-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
@ -115,6 +115,14 @@ public class ParametersUnitTests { @@ -115,6 +115,14 @@ public class ParametersUnitTests {
assertThat(parameters.getSortIndex(), is(1));
}
/**
* @see DATACMNS-520
*/
@Test
public void doesNotRejectParameterIfPageableComesFirst() throws Exception {
getParametersFor("validWithPageableFirst", Pageable.class, String.class);
}
private Parameters<?, ?> getParametersFor(String methodName, Class<?>... parameterTypes) throws SecurityException,
NoSuchMethodException {
@ -135,6 +143,8 @@ public class ParametersUnitTests { @@ -135,6 +143,8 @@ public class ParametersUnitTests {
User validWithPageable(@Param("username") String username, Pageable pageable);
User validWithPageableFirst(Pageable pageable, @Param("username") String username);
User validWithSort(@Param("username") String username, Sort sort);
User validWithSortFirst(Sort sort, String username);

Loading…
Cancel
Save