#321 - Bind only bindable query method parameters.
We now only bind parameters to the query that are actually bindable instead of consuming the entire parameter list.
This commit is contained in:
@@ -110,13 +110,12 @@ class ExpressionEvaluatingParameterBinder {
|
||||
Parameters<?, ?> bindableParameters) {
|
||||
|
||||
T bindSpecToUse = bindSpec;
|
||||
int index = 0;
|
||||
int bindingIndex = 0;
|
||||
|
||||
for (Object value : values) {
|
||||
|
||||
Parameter bindableParameter = bindableParameters.getBindableParameter(index++);
|
||||
for (Parameter bindableParameter : bindableParameters) {
|
||||
|
||||
Object value = values[bindableParameter.getIndex()];
|
||||
Optional<String> name = bindableParameter.getName();
|
||||
|
||||
if ((name.isPresent() && isNamedParameterUsed(name)) || !expressionQuery.getBindings().isEmpty()) {
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.projection.ProjectionFactory;
|
||||
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
|
||||
import org.springframework.data.r2dbc.convert.MappingR2dbcConverter;
|
||||
@@ -221,6 +222,21 @@ public class StringBasedR2dbcQueryUnitTests {
|
||||
verifyNoMoreInteractions(bindSpec);
|
||||
}
|
||||
|
||||
@Test // gh-321
|
||||
public void skipsNonBindableParameters() {
|
||||
|
||||
StringBasedR2dbcQuery query = getQueryMethod("queryWithUnusedParameter", String.class, Sort.class);
|
||||
R2dbcParameterAccessor accessor = new R2dbcParameterAccessor(query.getQueryMethod(), "Walter", null);
|
||||
|
||||
BindableQuery stringQuery = query.createQuery(accessor);
|
||||
|
||||
assertThat(stringQuery.get()).isEqualTo("SELECT * FROM person WHERE lastname = :name");
|
||||
assertThat(stringQuery.bind(bindSpec)).isNotNull();
|
||||
|
||||
verify(bindSpec).bind(0, "Walter");
|
||||
verifyNoMoreInteractions(bindSpec);
|
||||
}
|
||||
|
||||
private StringBasedR2dbcQuery getQueryMethod(String name, Class<?>... args) {
|
||||
|
||||
Method method = ReflectionUtils.findMethod(SampleRepository.class, name, args);
|
||||
@@ -263,6 +279,9 @@ public class StringBasedR2dbcQueryUnitTests {
|
||||
|
||||
@Query("SELECT * FROM person WHERE lastname = :#{#person.name}")
|
||||
Person queryWithSpelObject(@Param("person") Person person);
|
||||
|
||||
@Query("SELECT * FROM person WHERE lastname = :name")
|
||||
Person queryWithUnusedParameter(String name, Sort unused);
|
||||
}
|
||||
|
||||
static class Person {
|
||||
|
||||
Reference in New Issue
Block a user