Skip to content

Commit c543631

Browse files
authored
Throw 4xx instead of 5xx for ESQL malformed async query params (elastic#134879)
1 parent 8e4f8f3 commit c543631

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

docs/changelog/134879.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 134879
2+
summary: Throw 4xx instead of 5xx for ESQL malformed query params
3+
area: ES|QL
4+
type: bug
5+
issues:
6+
- 134618

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/RequestXContent.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,18 @@ private static QueryParams parseParams(XContentParser p) throws IOException {
222222
}
223223
}
224224
}
225+
} else {
226+
errors.add(
227+
new XContentParseException(
228+
"Unexpected token ["
229+
+ token
230+
+ "] at "
231+
+ p.getTokenLocation()
232+
+ ", expected "
233+
+ XContentParser.Token.START_ARRAY
234+
+ ". Please check documentation for the correct format of the 'params' field."
235+
)
236+
);
225237
}
226238
// don't allow mixed named and unnamed parameters
227239
if (namedParams.isEmpty() == false && unNamedParams.isEmpty() == false) {

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/action/EsqlQueryRequestTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,26 @@ public void testInvalidMultivaluedUnnamedParams() throws IOException {
408408
);
409409
}
410410

411+
public void testInvalidParamsString() {
412+
String query = randomAlphaOfLengthBetween(1, 100);
413+
String json1 = String.format(Locale.ROOT, """
414+
{
415+
"query": "%s",
416+
"params": {*}
417+
}""", query);
418+
Exception e1 = expectThrows(XContentParseException.class, () -> parseEsqlQueryRequestSync(json1));
419+
String message1 = e1.getCause().getMessage();
420+
assertThat("Unexpected failure when parsing " + json1 + ". " + message1, containsString("Unexpected token [START_OBJECT]"));
421+
String json2 = String.format(Locale.ROOT, """
422+
{
423+
"query": "%s",
424+
"params": "foo"
425+
}""", query);
426+
Exception e2 = expectThrows(XContentParseException.class, () -> parseEsqlQueryRequestSync(json2));
427+
String message2 = e2.getCause().getMessage();
428+
assertThat("Unexpected failure when parsing " + json2 + ". " + message2, containsString("Unexpected token [VALUE_STRING]"));
429+
}
430+
411431
public void testInvalidParamsForIdentifiersPatterns() throws IOException {
412432
String query = randomAlphaOfLengthBetween(1, 100);
413433
boolean columnar = randomBoolean();

0 commit comments

Comments
 (0)