DATAES-552 - @Query annotation fail when passing over 10 parameters

Updated the replacePlaceholders methods to use replaceFirst to prevent duplication substitutions.

Original Pull Request: #267
This commit is contained in:
Taylor 2019-03-24 13:59:50 -07:00 committed by Christoph Strobl
parent 16bf8450f0
commit b93bd07a3a
3 changed files with 24 additions and 2 deletions

View File

@ -32,6 +32,7 @@ import org.springframework.util.Assert;
* @author Rizwan Idrees
* @author Mohsin Husen
* @author Mark Paluch
* @author Taylor Ono
*/
public class ElasticsearchStringQuery extends AbstractElasticsearchRepositoryQuery {
@ -86,8 +87,9 @@ public class ElasticsearchStringQuery extends AbstractElasticsearchRepositoryQue
String result = input;
while (matcher.find()) {
String group = matcher.group();
group = "\\" + group;
int index = Integer.parseInt(matcher.group(1));
result = result.replace(group, getParameterWithIndex(accessor, index));
result = result.replaceFirst(group, getParameterWithIndex(accessor, index));
}
return result;
}

View File

@ -26,6 +26,7 @@ import org.springframework.util.ObjectUtils;
/**
* @author Christoph Strobl
* @author Taylor Ono
* @since 3.2
*/
public class ReactiveElasticsearchStringQuery extends AbstractReactiveElasticsearchRepositoryQuery {
@ -60,8 +61,9 @@ public class ReactiveElasticsearchStringQuery extends AbstractReactiveElasticsea
String result = input;
while (matcher.find()) {
String group = matcher.group();
group = "\\" + group;
int index = Integer.parseInt(matcher.group(1));
result = result.replace(group, getParameterWithIndex(accessor, index));
result = result.replaceFirst(group, getParameterWithIndex(accessor, index));
}
return result;
}

View File

@ -87,6 +87,20 @@ public class ReactiveElasticsearchStringQueryUnitTests {
assertThat(((StringQuery) query).getSource()).isEqualTo(reference.getSource());
}
@Test // DATAES-552
public void shouldReplaceLotsOfParametersCorrectly() throws Exception {
ReactiveElasticsearchStringQuery elasticsearchStringQuery = createQueryForMethod("findWithQuiteSomeParameters",
String.class, String.class, String.class, String.class, String.class, String.class, String.class, String.class,
String.class, String.class, String.class, String.class);
StubParameterAccessor accesor = new StubParameterAccessor("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l");
org.springframework.data.elasticsearch.core.query.Query query = elasticsearchStringQuery.createQuery(accesor);
StringQuery reference = new StringQuery("name:(a, b, c, d, e, f, g, h, i, j, k, l)");
assertThat(query).isInstanceOf(StringQuery.class);
assertThat(((StringQuery) query).getSource()).isEqualTo(reference.getSource());
}
private ReactiveElasticsearchStringQuery createQueryForMethod(String name, Class<?>... parameters) throws Exception {
Method method = SampleRepository.class.getMethod(name, parameters);
@ -104,5 +118,9 @@ public class ReactiveElasticsearchStringQueryUnitTests {
@Query("{ 'bool' : { 'must' : { 'term' : { 'name' : '?#{[0]}' } } } }")
Flux<Person> findByNameWithExpression(String param0);
@Query(value = "name:(?0, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)")
Person findWithQuiteSomeParameters(String arg0, String arg1, String arg2, String arg3, String arg4, String arg5,
String arg6, String arg7, String arg8, String arg9, String arg10, String arg11);
}
}