18
18
import static org .assertj .core .api .Assertions .*;
19
19
20
20
import java .lang .reflect .Method ;
21
+ import java .util .ArrayList ;
21
22
import java .util .Arrays ;
22
23
import java .util .Collection ;
23
24
import java .util .HashMap ;
51
52
/**
52
53
* @author Christoph Strobl
53
54
* @author Peter-Josef Meisch
55
+ * @author Niklas Herder
54
56
*/
55
57
@ ExtendWith (MockitoExtension .class )
56
58
public class ElasticsearchStringQueryUnitTests {
@@ -95,14 +97,50 @@ void shouldEscapeStringsInQueryParameters() throws Exception {
95
97
.isEqualTo ("{\" bool\" :{\" must\" : [{\" match\" : {\" prefix\" : {\" name\" : \" hello \\ \" Stranger\\ \" \" }}]}}" );
96
98
}
97
99
98
- private org .springframework .data .elasticsearch .core .query .Query createQuery (String methodName , String ... args )
100
+ @ Test // #1858
101
+ @ DisplayName ("should only quote String query parameters" )
102
+ void shouldOnlyEscapeStringQueryParameters () throws Exception {
103
+ org .springframework .data .elasticsearch .core .query .Query query = createQuery ("findByAge" , Integer .valueOf (30 ));
104
+
105
+ assertThat (query ).isInstanceOf (StringQuery .class );
106
+ assertThat (((StringQuery ) query ).getSource ()).isEqualTo ("{ 'bool' : { 'must' : { 'term' : { 'age' : 30 } } } }" );
107
+
108
+ }
109
+
110
+ @ Test // #1858
111
+ @ DisplayName ("should only quote String collection query parameters" )
112
+ void shouldOnlyEscapeStringCollectionQueryParameters () throws Exception {
113
+ org .springframework .data .elasticsearch .core .query .Query query = createQuery ("findByAgeIn" ,
114
+ new ArrayList <>(Arrays .asList (30 , 35 , 40 )));
115
+
116
+ assertThat (query ).isInstanceOf (StringQuery .class );
117
+ assertThat (((StringQuery ) query ).getSource ())
118
+ .isEqualTo ("{ 'bool' : { 'must' : { 'term' : { 'age' : [30,35,40] } } } }" );
119
+
120
+ }
121
+
122
+ @ Test // #1858
123
+ @ DisplayName ("should escape Strings in collection query parameters" )
124
+ void shouldEscapeStringsInCollectionsQueryParameters () throws Exception {
125
+
126
+ final List <String > another_string = Arrays .asList ("hello \" Stranger\" " , "Another string" );
127
+ List <String > params = new ArrayList <>(another_string );
128
+ org .springframework .data .elasticsearch .core .query .Query query = createQuery ("findByNameIn" , params );
129
+
130
+ assertThat (query ).isInstanceOf (StringQuery .class );
131
+ assertThat (((StringQuery ) query ).getSource ()).isEqualTo (
132
+ "{ 'bool' : { 'must' : { 'terms' : { 'name' : [\" hello \\ \" Stranger\\ \" \" ,\" Another string\" ] } } } }" );
133
+ }
134
+
135
+ private org .springframework .data .elasticsearch .core .query .Query createQuery (String methodName , Object ... args )
99
136
throws NoSuchMethodException {
100
137
101
138
Class <?>[] argTypes = Arrays .stream (args ).map (Object ::getClass ).toArray (Class []::new );
102
139
ElasticsearchQueryMethod queryMethod = getQueryMethod (methodName , argTypes );
103
140
ElasticsearchStringQuery elasticsearchStringQuery = queryForMethod (queryMethod );
104
141
return elasticsearchStringQuery .createQuery (new ElasticsearchParametersParameterAccessor (queryMethod , args ));
105
142
}
143
+
106
144
private ElasticsearchStringQuery queryForMethod (ElasticsearchQueryMethod queryMethod ) {
107
145
return new ElasticsearchStringQuery (queryMethod , operations , queryMethod .getAnnotatedQuery ());
108
146
}
@@ -116,9 +154,18 @@ private ElasticsearchQueryMethod getQueryMethod(String name, Class<?>... paramet
116
154
117
155
private interface SampleRepository extends Repository <Person , String > {
118
156
157
+ @ Query ("{ 'bool' : { 'must' : { 'term' : { 'age' : ?0 } } } }" )
158
+ List <Person > findByAge (Integer age );
159
+
160
+ @ Query ("{ 'bool' : { 'must' : { 'term' : { 'age' : ?0 } } } }" )
161
+ List <Person > findByAgeIn (ArrayList <Integer > age );
162
+
119
163
@ Query ("{ 'bool' : { 'must' : { 'term' : { 'name' : '?0' } } } }" )
120
164
Person findByName (String name );
121
165
166
+ @ Query ("{ 'bool' : { 'must' : { 'terms' : { 'name' : ?0 } } } }" )
167
+ Person findByNameIn (ArrayList <String > names );
168
+
122
169
@ Query (value = "name:(?0, ?11, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?0, ?1)" )
123
170
Person findWithRepeatedPlaceholder (String arg0 , String arg1 , String arg2 , String arg3 , String arg4 , String arg5 ,
124
171
String arg6 , String arg7 , String arg8 , String arg9 , String arg10 , String arg11 );
@@ -131,16 +178,27 @@ Person findWithRepeatedPlaceholder(String arg0, String arg1, String arg2, String
131
178
* @author Rizwan Idrees
132
179
* @author Mohsin Husen
133
180
* @author Artur Konczak
181
+ * @author Niklas Herder
134
182
*/
135
183
136
184
@ Document (indexName = "test-index-person-query-unittest" )
137
185
static class Person {
138
186
187
+ @ Nullable public int age ;
139
188
@ Nullable @ Id private String id ;
140
189
@ Nullable private String name ;
141
190
@ Nullable @ Field (type = FieldType .Nested ) private List <Car > car ;
142
191
@ Nullable @ Field (type = FieldType .Nested , includeInParent = true ) private List <Book > books ;
143
192
193
+ @ Nullable
194
+ public int getAge () {
195
+ return age ;
196
+ }
197
+
198
+ public void setAge (int age ) {
199
+ this .age = age ;
200
+ }
201
+
144
202
@ Nullable
145
203
public String getId () {
146
204
return id ;
0 commit comments