Skip to content

Commit d6cfc20

Browse files
herdersothawo
authored andcommitted
Support collection parameters in @query methods.
Original Pull Request #1856 Closes #1858 (cherry picked from commit 6f84a1c) (cherry picked from commit 254948d) (cherry picked from commit 979c164)
1 parent 5578ad1 commit d6cfc20

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

src/main/java/org/springframework/data/elasticsearch/repository/support/StringQueryUtil.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
*/
1616
package org.springframework.data.elasticsearch.repository.support;
1717

18+
import java.util.Collection;
1819
import java.util.regex.Matcher;
1920
import java.util.regex.Pattern;
21+
import java.util.stream.Collectors;
2022

2123
import org.springframework.core.convert.support.GenericConversionService;
2224
import org.springframework.data.elasticsearch.core.convert.DateTimeConverters;
@@ -27,6 +29,7 @@
2729

2830
/**
2931
* @author Peter-Josef Meisch
32+
* @author Niklas Herder
3033
*/
3134
final public class StringQueryUtil {
3235

@@ -70,6 +73,28 @@ private static String getParameterWithIndex(ParameterAccessor accessor, int inde
7073
// noinspection ConstantConditions
7174
if (parameter != null) {
7275

76+
parameterValue = convert(parameter);
77+
}
78+
79+
return parameterValue;
80+
81+
}
82+
83+
private static String convert(Object parameter) {
84+
if (Collection.class.isAssignableFrom(parameter.getClass())) {
85+
Collection<?> collectionParam = (Collection<?>) parameter;
86+
StringBuilder sb = new StringBuilder("[");
87+
sb.append(collectionParam.stream().map(o -> {
88+
if (o instanceof String) {
89+
return "\"" + convert(o) + "\"";
90+
} else {
91+
return convert(o);
92+
}
93+
}).collect(Collectors.joining(",")));
94+
sb.append("]");
95+
return sb.toString();
96+
} else {
97+
String parameterValue = "null";
7398
if (conversionService.canConvert(parameter.getClass(), String.class)) {
7499
String converted = conversionService.convert(parameter, String.class);
75100

@@ -79,11 +104,10 @@ private static String getParameterWithIndex(ParameterAccessor accessor, int inde
79104
} else {
80105
parameterValue = parameter.toString();
81106
}
82-
}
83-
84-
parameterValue = parameterValue.replaceAll("\"", Matcher.quoteReplacement("\\\""));
85-
return parameterValue;
86107

108+
parameterValue = parameterValue.replaceAll("\"", Matcher.quoteReplacement("\\\""));
109+
return parameterValue;
110+
}
87111
}
88112

89113
}

src/test/java/org/springframework/data/elasticsearch/repository/query/ElasticsearchStringQueryUnitTests.java

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import lombok.Setter;
2525

2626
import java.lang.reflect.Method;
27+
import java.util.ArrayList;
2728
import java.util.Arrays;
2829
import java.util.Collection;
2930
import java.util.HashMap;
@@ -57,6 +58,7 @@
5758
/**
5859
* @author Christoph Strobl
5960
* @author Peter-Josef Meisch
61+
* @author Niklas Herder
6062
*/
6163
@ExtendWith(MockitoExtension.class)
6264
public class ElasticsearchStringQueryUnitTests {
@@ -101,14 +103,50 @@ void shouldEscapeStringsInQueryParameters() throws Exception {
101103
.isEqualTo("{\"bool\":{\"must\": [{\"match\": {\"prefix\": {\"name\" : \"hello \\\"Stranger\\\"\"}}]}}");
102104
}
103105

104-
private org.springframework.data.elasticsearch.core.query.Query createQuery(String methodName, String... args)
106+
@Test // #1858
107+
@DisplayName("should only quote String query parameters")
108+
void shouldOnlyEscapeStringQueryParameters() throws Exception {
109+
org.springframework.data.elasticsearch.core.query.Query query = createQuery("findByAge", Integer.valueOf(30));
110+
111+
assertThat(query).isInstanceOf(StringQuery.class);
112+
assertThat(((StringQuery) query).getSource()).isEqualTo("{ 'bool' : { 'must' : { 'term' : { 'age' : 30 } } } }");
113+
114+
}
115+
116+
@Test // #1858
117+
@DisplayName("should only quote String collection query parameters")
118+
void shouldOnlyEscapeStringCollectionQueryParameters() throws Exception {
119+
org.springframework.data.elasticsearch.core.query.Query query = createQuery("findByAgeIn",
120+
new ArrayList<>(Arrays.asList(30, 35, 40)));
121+
122+
assertThat(query).isInstanceOf(StringQuery.class);
123+
assertThat(((StringQuery) query).getSource())
124+
.isEqualTo("{ 'bool' : { 'must' : { 'term' : { 'age' : [30,35,40] } } } }");
125+
126+
}
127+
128+
@Test // #1858
129+
@DisplayName("should escape Strings in collection query parameters")
130+
void shouldEscapeStringsInCollectionsQueryParameters() throws Exception {
131+
132+
final List<String> another_string = Arrays.asList("hello \"Stranger\"", "Another string");
133+
List<String> params = new ArrayList<>(another_string);
134+
org.springframework.data.elasticsearch.core.query.Query query = createQuery("findByNameIn", params);
135+
136+
assertThat(query).isInstanceOf(StringQuery.class);
137+
assertThat(((StringQuery) query).getSource()).isEqualTo(
138+
"{ 'bool' : { 'must' : { 'terms' : { 'name' : [\"hello \\\"Stranger\\\"\",\"Another string\"] } } } }");
139+
}
140+
141+
private org.springframework.data.elasticsearch.core.query.Query createQuery(String methodName, Object... args)
105142
throws NoSuchMethodException {
106143

107144
Class<?>[] argTypes = Arrays.stream(args).map(Object::getClass).toArray(Class[]::new);
108145
ElasticsearchQueryMethod queryMethod = getQueryMethod(methodName, argTypes);
109146
ElasticsearchStringQuery elasticsearchStringQuery = queryForMethod(queryMethod);
110147
return elasticsearchStringQuery.createQuery(new ElasticsearchParametersParameterAccessor(queryMethod, args));
111148
}
149+
112150
private ElasticsearchStringQuery queryForMethod(ElasticsearchQueryMethod queryMethod) {
113151
return new ElasticsearchStringQuery(queryMethod, operations, queryMethod.getAnnotatedQuery());
114152
}
@@ -122,9 +160,18 @@ private ElasticsearchQueryMethod getQueryMethod(String name, Class<?>... paramet
122160

123161
private interface SampleRepository extends Repository<Person, String> {
124162

163+
@Query("{ 'bool' : { 'must' : { 'term' : { 'age' : ?0 } } } }")
164+
List<Person> findByAge(Integer age);
165+
166+
@Query("{ 'bool' : { 'must' : { 'term' : { 'age' : ?0 } } } }")
167+
List<Person> findByAgeIn(ArrayList<Integer> age);
168+
125169
@Query("{ 'bool' : { 'must' : { 'term' : { 'name' : '?0' } } } }")
126170
Person findByName(String name);
127171

172+
@Query("{ 'bool' : { 'must' : { 'terms' : { 'name' : ?0 } } } }")
173+
Person findByNameIn(ArrayList<String> names);
174+
128175
@Query(value = "name:(?0, ?11, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?0, ?1)")
129176
Person findWithRepeatedPlaceholder(String arg0, String arg1, String arg2, String arg3, String arg4, String arg5,
130177
String arg6, String arg7, String arg8, String arg9, String arg10, String arg11);
@@ -137,16 +184,27 @@ Person findWithRepeatedPlaceholder(String arg0, String arg1, String arg2, String
137184
* @author Rizwan Idrees
138185
* @author Mohsin Husen
139186
* @author Artur Konczak
187+
* @author Niklas Herder
140188
*/
141189

142190
@Document(indexName = "test-index-person-query-unittest", replicas = 0, refreshInterval = "-1")
143191
static class Person {
144192

193+
@Nullable public int age;
145194
@Nullable @Id private String id;
146195
@Nullable private String name;
147196
@Nullable @Field(type = FieldType.Nested) private List<Car> car;
148197
@Nullable @Field(type = FieldType.Nested, includeInParent = true) private List<Book> books;
149198

199+
@Nullable
200+
public int getAge() {
201+
return age;
202+
}
203+
204+
public void setAge(int age) {
205+
this.age = age;
206+
}
207+
150208
@Nullable
151209
public String getId() {
152210
return id;

0 commit comments

Comments
 (0)