Skip to content

Commit 85e569b

Browse files
committed
DATACMNS-804 - ParametersParameterAccessor now correctly iterates over only the bindable values.
Corrected the way the BindableParametersIterator works by limiting the upper bound of the iteration to the number of actually bindable parameters.
1 parent b17a797 commit 85e569b

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,33 +145,49 @@ public boolean hasBindableNullValue() {
145145
* @see org.springframework.data.repository.query.ParameterAccessor#iterator()
146146
*/
147147
public BindableParameterIterator iterator() {
148-
return new BindableParameterIterator();
148+
return new BindableParameterIterator(this);
149149
}
150150

151151
/**
152152
* Iterator class to allow traversing all bindable parameters inside the accessor.
153153
*
154154
* @author Oliver Gierke
155155
*/
156-
private class BindableParameterIterator implements Iterator<Object> {
156+
private static class BindableParameterIterator implements Iterator<Object> {
157+
158+
private final int bindableParameterCount;
159+
private final ParameterAccessor accessor;
157160

158161
private int currentIndex = 0;
159162

163+
/**
164+
* Creates a new {@link BindableParameterIterator}.
165+
*
166+
* @param accessor must not be {@literal null}.
167+
*/
168+
public BindableParameterIterator(ParametersParameterAccessor accessor) {
169+
170+
Assert.notNull(accessor, "ParametersParameterAccessor must not be null!");
171+
172+
this.accessor = accessor;
173+
this.bindableParameterCount = accessor.getParameters().getBindableParameters().getNumberOfParameters();
174+
}
175+
160176
/**
161177
* Returns the next bindable parameter.
162178
*
163179
* @return
164180
*/
165181
public Object next() {
166-
return getBindableValue(currentIndex++);
182+
return accessor.getBindableValue(currentIndex++);
167183
}
168184

169185
/*
170186
* (non-Javadoc)
171187
* @see java.util.Iterator#hasNext()
172188
*/
173189
public boolean hasNext() {
174-
return values.size() > currentIndex;
190+
return bindableParameterCount > currentIndex;
175191
}
176192

177193
/*

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2013 the original author or authors.
2+
* Copyright 2011-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,14 +15,15 @@
1515
*/
1616
package org.springframework.data.repository.query;
1717

18-
import static org.hamcrest.CoreMatchers.*;
18+
import static org.hamcrest.Matchers.*;
1919
import static org.junit.Assert.*;
2020

2121
import java.lang.reflect.Method;
2222
import java.util.Iterator;
2323

2424
import org.junit.Before;
2525
import org.junit.Test;
26+
import org.springframework.data.domain.PageRequest;
2627
import org.springframework.data.domain.Pageable;
2728

2829
/**
@@ -65,6 +66,21 @@ public void detectsNullValue() throws Exception {
6566
assertThat(accessor.hasBindableNullValue(), is(false));
6667
}
6768

69+
/**
70+
* @see DATACMNS-804
71+
*/
72+
@Test
73+
public void iteratesonlyOverBindableValues() throws Exception {
74+
75+
Method method = Sample.class.getMethod("method", Pageable.class, String.class);
76+
DefaultParameters parameters = new DefaultParameters(method);
77+
78+
ParametersParameterAccessor accessor = new ParametersParameterAccessor(parameters,
79+
new Object[] { new PageRequest(0, 10), "Foo" });
80+
81+
assertThat(accessor, is(iterableWithSize(1)));
82+
}
83+
6884
interface Sample {
6985

7086
void method(String string, int integer);

0 commit comments

Comments
 (0)