24
24
import lombok .Setter ;
25
25
26
26
import java .lang .reflect .Method ;
27
+ import java .util .ArrayList ;
27
28
import java .util .Arrays ;
28
29
import java .util .Collection ;
29
30
import java .util .HashMap ;
57
58
/**
58
59
* @author Christoph Strobl
59
60
* @author Peter-Josef Meisch
61
+ * @author Niklas Herder
60
62
*/
61
63
@ ExtendWith (MockitoExtension .class )
62
64
public class ElasticsearchStringQueryUnitTests {
@@ -101,14 +103,50 @@ void shouldEscapeStringsInQueryParameters() throws Exception {
101
103
.isEqualTo ("{\" bool\" :{\" must\" : [{\" match\" : {\" prefix\" : {\" name\" : \" hello \\ \" Stranger\\ \" \" }}]}}" );
102
104
}
103
105
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 )
105
142
throws NoSuchMethodException {
106
143
107
144
Class <?>[] argTypes = Arrays .stream (args ).map (Object ::getClass ).toArray (Class []::new );
108
145
ElasticsearchQueryMethod queryMethod = getQueryMethod (methodName , argTypes );
109
146
ElasticsearchStringQuery elasticsearchStringQuery = queryForMethod (queryMethod );
110
147
return elasticsearchStringQuery .createQuery (new ElasticsearchParametersParameterAccessor (queryMethod , args ));
111
148
}
149
+
112
150
private ElasticsearchStringQuery queryForMethod (ElasticsearchQueryMethod queryMethod ) {
113
151
return new ElasticsearchStringQuery (queryMethod , operations , queryMethod .getAnnotatedQuery ());
114
152
}
@@ -122,9 +160,18 @@ private ElasticsearchQueryMethod getQueryMethod(String name, Class<?>... paramet
122
160
123
161
private interface SampleRepository extends Repository <Person , String > {
124
162
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
+
125
169
@ Query ("{ 'bool' : { 'must' : { 'term' : { 'name' : '?0' } } } }" )
126
170
Person findByName (String name );
127
171
172
+ @ Query ("{ 'bool' : { 'must' : { 'terms' : { 'name' : ?0 } } } }" )
173
+ Person findByNameIn (ArrayList <String > names );
174
+
128
175
@ Query (value = "name:(?0, ?11, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?0, ?1)" )
129
176
Person findWithRepeatedPlaceholder (String arg0 , String arg1 , String arg2 , String arg3 , String arg4 , String arg5 ,
130
177
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
137
184
* @author Rizwan Idrees
138
185
* @author Mohsin Husen
139
186
* @author Artur Konczak
187
+ * @author Niklas Herder
140
188
*/
141
189
142
190
@ Document (indexName = "test-index-person-query-unittest" , replicas = 0 , refreshInterval = "-1" )
143
191
static class Person {
144
192
193
+ @ Nullable public int age ;
145
194
@ Nullable @ Id private String id ;
146
195
@ Nullable private String name ;
147
196
@ Nullable @ Field (type = FieldType .Nested ) private List <Car > car ;
148
197
@ Nullable @ Field (type = FieldType .Nested , includeInParent = true ) private List <Book > books ;
149
198
199
+ @ Nullable
200
+ public int getAge () {
201
+ return age ;
202
+ }
203
+
204
+ public void setAge (int age ) {
205
+ this .age = age ;
206
+ }
207
+
150
208
@ Nullable
151
209
public String getId () {
152
210
return id ;
0 commit comments