1818import static org .assertj .core .api .Assertions .*;
1919
2020import java .lang .reflect .Method ;
21+ import java .util .ArrayList ;
2122import java .util .Arrays ;
2223import java .util .Collection ;
2324import java .util .HashMap ;
5152/**
5253 * @author Christoph Strobl
5354 * @author Peter-Josef Meisch
55+ * @author Niklas Herder
5456 */
5557@ ExtendWith (MockitoExtension .class )
5658public class ElasticsearchStringQueryUnitTests {
@@ -95,14 +97,50 @@ void shouldEscapeStringsInQueryParameters() throws Exception {
9597 .isEqualTo ("{\" bool\" :{\" must\" : [{\" match\" : {\" prefix\" : {\" name\" : \" hello \\ \" Stranger\\ \" \" }}]}}" );
9698 }
9799
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 )
99136 throws NoSuchMethodException {
100137
101138 Class <?>[] argTypes = Arrays .stream (args ).map (Object ::getClass ).toArray (Class []::new );
102139 ElasticsearchQueryMethod queryMethod = getQueryMethod (methodName , argTypes );
103140 ElasticsearchStringQuery elasticsearchStringQuery = queryForMethod (queryMethod );
104141 return elasticsearchStringQuery .createQuery (new ElasticsearchParametersParameterAccessor (queryMethod , args ));
105142 }
143+
106144 private ElasticsearchStringQuery queryForMethod (ElasticsearchQueryMethod queryMethod ) {
107145 return new ElasticsearchStringQuery (queryMethod , operations , queryMethod .getAnnotatedQuery ());
108146 }
@@ -116,9 +154,18 @@ private ElasticsearchQueryMethod getQueryMethod(String name, Class<?>... paramet
116154
117155 private interface SampleRepository extends Repository <Person , String > {
118156
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+
119163 @ Query ("{ 'bool' : { 'must' : { 'term' : { 'name' : '?0' } } } }" )
120164 Person findByName (String name );
121165
166+ @ Query ("{ 'bool' : { 'must' : { 'terms' : { 'name' : ?0 } } } }" )
167+ Person findByNameIn (ArrayList <String > names );
168+
122169 @ Query (value = "name:(?0, ?11, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?0, ?1)" )
123170 Person findWithRepeatedPlaceholder (String arg0 , String arg1 , String arg2 , String arg3 , String arg4 , String arg5 ,
124171 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
131178 * @author Rizwan Idrees
132179 * @author Mohsin Husen
133180 * @author Artur Konczak
181+ * @author Niklas Herder
134182 */
135183
136184 @ Document (indexName = "test-index-person-query-unittest" )
137185 static class Person {
138186
187+ @ Nullable public int age ;
139188 @ Nullable @ Id private String id ;
140189 @ Nullable private String name ;
141190 @ Nullable @ Field (type = FieldType .Nested ) private List <Car > car ;
142191 @ Nullable @ Field (type = FieldType .Nested , includeInParent = true ) private List <Book > books ;
143192
193+ @ Nullable
194+ public int getAge () {
195+ return age ;
196+ }
197+
198+ public void setAge (int age ) {
199+ this .age = age ;
200+ }
201+
144202 @ Nullable
145203 public String getId () {
146204 return id ;
0 commit comments