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 ;
50
51
import org .springframework .data .projection .SpelAwareProxyProjectionFactory ;
51
52
import org .springframework .data .repository .Repository ;
52
53
import org .springframework .data .repository .core .support .DefaultRepositoryMetadata ;
54
+ import org .springframework .lang .Nullable ;
53
55
54
56
/**
55
57
* @author Christoph Strobl
56
58
* @author Peter-Josef Meisch
59
+ * @author Niklas Herder
57
60
*/
58
61
@ RunWith (MockitoJUnitRunner .class )
59
62
public class ElasticsearchStringQueryUnitTests {
@@ -97,7 +100,39 @@ public void shouldEscapeStringsInQueryParameters() throws Exception {
97
100
.isEqualTo ("{\" bool\" :{\" must\" : [{\" match\" : {\" prefix\" : {\" name\" : \" hello \\ \" Stranger\\ \" \" }}]}}" );
98
101
}
99
102
100
- private org .springframework .data .elasticsearch .core .query .Query createQuery (String methodName , String ... args )
103
+ @ Test // #1858
104
+ public void shouldOnlyEscapeStringQueryParameters () throws Exception {
105
+ org .springframework .data .elasticsearch .core .query .Query query = createQuery ("findByAge" , Integer .valueOf (30 ));
106
+
107
+ assertThat (query ).isInstanceOf (StringQuery .class );
108
+ assertThat (((StringQuery ) query ).getSource ()).isEqualTo ("{ 'bool' : { 'must' : { 'term' : { 'age' : 30 } } } }" );
109
+
110
+ }
111
+
112
+ @ Test // #1858
113
+ public void shouldOnlyEscapeStringCollectionQueryParameters () throws Exception {
114
+ org .springframework .data .elasticsearch .core .query .Query query = createQuery ("findByAgeIn" ,
115
+ new ArrayList <>(Arrays .asList (30 , 35 , 40 )));
116
+
117
+ assertThat (query ).isInstanceOf (StringQuery .class );
118
+ assertThat (((StringQuery ) query ).getSource ())
119
+ .isEqualTo ("{ 'bool' : { 'must' : { 'term' : { 'age' : [30,35,40] } } } }" );
120
+
121
+ }
122
+
123
+ @ Test // #1858
124
+ public 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 )
101
136
throws NoSuchMethodException {
102
137
103
138
Class <?>[] argTypes = Arrays .stream (args ).map (Object ::getClass ).toArray (size -> new Class [size ]);
@@ -117,24 +152,55 @@ private ElasticsearchQueryMethod getQueryMethod(String name, Class<?>... paramet
117
152
new SpelAwareProxyProjectionFactory (), converter .getMappingContext ());
118
153
}
119
154
155
+ private interface SampleRepository extends Repository <Person , String > {
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
+
163
+ @ Query ("{ 'bool' : { 'must' : { 'term' : { 'name' : '?0' } } } }" )
164
+ Person findByName (String name );
165
+
166
+ @ Query ("{ 'bool' : { 'must' : { 'terms' : { 'name' : ?0 } } } }" )
167
+ Person findByNameIn (ArrayList <String > names );
168
+
169
+ @ Query (value = "name:(?0, ?11, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?0, ?1)" )
170
+ Person findWithRepeatedPlaceholder (String arg0 , String arg1 , String arg2 , String arg3 , String arg4 , String arg5 ,
171
+ String arg6 , String arg7 , String arg8 , String arg9 , String arg10 , String arg11 );
172
+
173
+ @ Query ("{\" bool\" :{\" must\" : [{\" match\" : {\" prefix\" : {\" name\" : \" ?0\" }}]}}" )
174
+ List <Book > findByPrefix (String prefix );
175
+ }
176
+
120
177
/**
121
178
* @author Rizwan Idrees
122
179
* @author Mohsin Husen
123
180
* @author Artur Konczak
181
+ * @author Niklas Herder
124
182
*/
125
183
126
184
@ Document (indexName = "test-index-person-query-unittest" , type = "user" , shards = 1 , replicas = 0 ,
127
185
refreshInterval = "-1" )
128
186
static class Person {
129
187
130
- @ Id private String id ;
131
-
132
- private String name ;
188
+ @ Nullable public int age ;
189
+ @ Nullable @ Id private String id ;
190
+ @ Nullable private String name ;
191
+ @ Nullable @ Field (type = FieldType .Nested ) private List <Car > car ;
192
+ @ Nullable @ Field (type = FieldType .Nested , includeInParent = true ) private List <Book > books ;
133
193
134
- @ Field (type = FieldType .Nested ) private List <Car > car ;
194
+ @ Nullable
195
+ public int getAge () {
196
+ return age ;
197
+ }
135
198
136
- @ Field (type = FieldType .Nested , includeInParent = true ) private List <Book > books ;
199
+ public void setAge (int age ) {
200
+ this .age = age ;
201
+ }
137
202
203
+ @ Nullable
138
204
public String getId () {
139
205
return id ;
140
206
}
@@ -168,19 +234,6 @@ public void setBooks(List<Book> books) {
168
234
}
169
235
}
170
236
171
- private interface SampleRepository extends Repository <Person , String > {
172
-
173
- @ Query ("{ 'bool' : { 'must' : { 'term' : { 'name' : '?0' } } } }" )
174
- Person findByName (String name );
175
-
176
- @ Query (value = "name:(?0, ?11, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?0, ?1)" )
177
- Person findWithRepeatedPlaceholder (String arg0 , String arg1 , String arg2 , String arg3 , String arg4 , String arg5 ,
178
- String arg6 , String arg7 , String arg8 , String arg9 , String arg10 , String arg11 );
179
-
180
- @ Query ("{\" bool\" :{\" must\" : [{\" match\" : {\" prefix\" : {\" name\" : \" ?0\" }}]}}" )
181
- List <Book > findByPrefix (String prefix );
182
- }
183
-
184
237
/**
185
238
* @author Rizwan Idrees
186
239
* @author Mohsin Husen
0 commit comments