Skip to content

Commit 1226ca9

Browse files
schaudermp911de
authored andcommitted
Convert only contents of collections using StringBasedJdbcQuery.
Contents of Iterables that aren't collections will not be converted individually. Closes #1343 Original pull request: #1356
1 parent 9893884 commit 1226ca9

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/query/StringBasedJdbcQuery.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.lang.reflect.Constructor;
2121
import java.sql.SQLType;
2222
import java.util.ArrayList;
23+
import java.util.Collection;
2324
import java.util.List;
2425

2526
import org.springframework.beans.BeanUtils;
@@ -188,7 +189,7 @@ private void convertAndAddParameter(MapSqlParameterSource parameters, Parameter
188189
Assert.notNull(type, "@Query parameter type could not be resolved");
189190

190191
JdbcValue jdbcValue;
191-
if (value instanceof Iterable) {
192+
if (value instanceof Collection && resolvableType.hasGenerics()) {
192193

193194
List<Object> mapped = new ArrayList<>();
194195
SQLType jdbcType = null;

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/repository/query/StringBasedJdbcQueryUnitTests.java

+43-1
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@
2222
import java.sql.JDBCType;
2323
import java.sql.ResultSet;
2424
import java.util.ArrayList;
25+
import java.util.Iterator;
2526
import java.util.List;
2627
import java.util.Properties;
2728
import java.util.Set;
2829
import java.util.stream.Stream;
2930

3031
import org.assertj.core.api.Assertions;
32+
import org.jetbrains.annotations.NotNull;
3133
import org.junit.jupiter.api.BeforeEach;
3234
import org.junit.jupiter.api.Test;
3335
import org.mockito.ArgumentCaptor;
@@ -211,7 +213,6 @@ void convertsEnumCollectionParameterUsingCustomConverterWhenRegisteredForType()
211213
assertThat(sqlParameterSource.getValue("directions")).asList().containsExactlyInAnyOrder(-1, 1);
212214
}
213215

214-
215216
@Test // GH-1212
216217
void doesNotConvertNonCollectionParameter() {
217218

@@ -222,6 +223,18 @@ void doesNotConvertNonCollectionParameter() {
222223
assertThat(sqlParameterSource.getValue("value")).isEqualTo(1);
223224
}
224225

226+
@Test // GH-1343
227+
void appliesConverterToIterable() {
228+
229+
SqlParameterSource sqlParameterSource = forMethod("findByListContainer", ListContainer.class) //
230+
.withCustomConverters(ListContainerToStringConverter.INSTANCE)
231+
.withArguments(new ListContainer("one", "two", "three")) //
232+
.extractParameterSource();
233+
234+
assertThat(sqlParameterSource.getValue("value")).isEqualTo("one");
235+
236+
}
237+
225238
QueryFixture forMethod(String name, Class... paramTypes) {
226239
return new QueryFixture(createMethod(name, paramTypes));
227240
}
@@ -321,6 +334,9 @@ interface MyRepository extends Repository<Object, Long> {
321334
@Query(value = "some sql statement")
322335
List<Object> findBySimpleValue(Integer value);
323336

337+
@Query(value = "some sql statement")
338+
List<Object> findByListContainer(ListContainer value);
339+
324340
@Query("SELECT * FROM table WHERE c = :#{myext.testValue} AND c2 = :#{myext.doSomething()}")
325341
Object findBySpelExpression(Object object);
326342
}
@@ -417,6 +433,32 @@ public Direction convert(Integer source) {
417433
}
418434
}
419435

436+
static class ListContainer implements Iterable<String> {
437+
438+
private final List<String> values;
439+
440+
ListContainer(String... values) {
441+
this.values = List.of(values);
442+
}
443+
444+
@NotNull
445+
@Override
446+
public Iterator<String> iterator() {
447+
return values.iterator();
448+
}
449+
}
450+
451+
@WritingConverter
452+
enum ListContainerToStringConverter implements Converter<ListContainer, String> {
453+
454+
INSTANCE;
455+
456+
@Override
457+
public String convert(ListContainer source) {
458+
return source.values.get(0);
459+
}
460+
}
461+
420462
private static class DummyEntity {
421463
private Long id;
422464

0 commit comments

Comments
 (0)