Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public String replacePlaceholders(String input, ParameterAccessor accessor) {

String placeholder = Pattern.quote(matcher.group()) + "(?!\\d+)";
int index = NumberUtils.parseNumber(matcher.group(1), Integer.class);
result = result.replaceAll(placeholder, Matcher.quoteReplacement(getParameterWithIndex(accessor, index)));
String replacement = Matcher.quoteReplacement(getParameterWithIndex(accessor, index));
result = result.replaceAll(placeholder, replacement);
// need to escape backslashes that are not escapes for quotes so that they are sent as double-backslashes
// to Elasticsearch
result = result.replaceAll("\\\\([^\"'])", "\\\\\\\\$1");
}
return result;
}
Expand All @@ -56,7 +60,6 @@ private String getParameterWithIndex(ParameterAccessor accessor, int index) {
Object parameter = accessor.getBindableValue(index);
String parameterValue = "null";

// noinspection ConstantConditions
if (parameter != null) {

parameterValue = convert(parameter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ void shouldEscapeStringsInCollectionsQueryParameters() throws Exception {
"{ 'bool' : { 'must' : { 'terms' : { 'name' : [\"hello \\\"Stranger\\\"\",\"Another string\"] } } } }");
}

@Test // #2326
@DisplayName("should escape backslashes in collection query parameters")
void shouldEscapeBackslashesInCollectionQueryParameters() throws NoSuchMethodException {

final List<String> parameters = Arrays.asList("param\\1", "param\\2");
List<String> params = new ArrayList<>(parameters);
org.springframework.data.elasticsearch.core.query.Query query = createQuery("findByNameIn", params);

assertThat(query).isInstanceOf(StringQuery.class);
assertThat(((StringQuery) query).getSource()).isEqualTo(
"{ 'bool' : { 'must' : { 'terms' : { 'name' : [\"param\\\\1\",\"param\\\\2\"] } } } }");
}
private org.springframework.data.elasticsearch.core.query.Query createQuery(String methodName, Object... args)
throws NoSuchMethodException {

Expand Down