From caa5b4f88e8e46252e83743c76e8a43143bda495 Mon Sep 17 00:00:00 2001 From: Andrei Stefan Date: Wed, 3 Mar 2021 19:25:42 +0200 Subject: [PATCH] Remove TextFormat as an instance variable (#69895) (cherry picked from commit 20f939726d5c53e967e1d4560b628c4daa782247) --- .../xpack/sql/qa/rest/RestSqlTestCase.java | 29 +++++++++++++++++++ .../xpack/sql/plugin/RestSqlQueryAction.java | 17 ++++++++--- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlTestCase.java b/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlTestCase.java index 8d52d969ff0c5..a579f58c4c625 100644 --- a/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlTestCase.java +++ b/x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlTestCase.java @@ -932,6 +932,35 @@ public void testNextPageCSV() throws IOException { executeQueryWithNextPage("text/csv; header=present", "text,number,sum\r\n", "%s,%d,%d\r\n"); } + public void testCSVWithDelimiterParameter() throws IOException { + String format = randomFrom("txt", "tsv", "json", "yaml", "smile", "cbor"); + String query = "SELECT * FROM test"; + index("{\"foo\":1}"); + + Request badRequest = new Request("POST", SQL_QUERY_REST_ENDPOINT); + badRequest.addParameter("format", format); + badRequest.addParameter("delimiter", ";"); + badRequest.setEntity( + new StringEntity( + query(query).mode(randomValueOtherThan(Mode.JDBC.toString(), BaseRestSqlTestCase::randomMode)).toString(), + ContentType.APPLICATION_JSON + ) + ); + expectBadRequest(() -> { + client().performRequest(badRequest); + return Collections.emptyMap(); + }, containsString("request [/_sql] contains unrecognized parameter: [delimiter]")); + + Request csvRequest = new Request("POST", SQL_QUERY_REST_ENDPOINT + "?format=csv&delimiter=%3B"); + csvRequest.setEntity( + new StringEntity( + query(query).mode(randomValueOtherThan(Mode.JDBC.toString(), BaseRestSqlTestCase::randomMode)).toString(), + ContentType.APPLICATION_JSON + ) + ); + assertOK(client().performRequest(csvRequest)); + } + public void testQueryInTSV() throws IOException { index( "{\"name\":" + toJson("first") + ", \"number\" : 1 }", diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlQueryAction.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlQueryAction.java index 1c8e8175df685..21556a2074b05 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlQueryAction.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlQueryAction.java @@ -31,6 +31,7 @@ import static java.util.Arrays.asList; import static java.util.Collections.emptyList; +import static java.util.Collections.emptySet; import static java.util.Collections.unmodifiableList; import static org.elasticsearch.rest.RestRequest.Method.GET; import static org.elasticsearch.rest.RestRequest.Method.POST; @@ -39,8 +40,6 @@ public class RestSqlQueryAction extends BaseRestHandler { - TextFormat textFormat; - @Override public List routes() { return emptyList(); @@ -106,13 +105,23 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli * which we turn into a 400 error. */ XContentType xContentType = accept == null ? XContentType.JSON : XContentType.fromMediaTypeOrFormat(accept); - textFormat = xContentType == null ? TextFormat.fromMediaTypeOrFormat(accept) : null; + TextFormat textFormat = xContentType == null ? TextFormat.fromMediaTypeOrFormat(accept) : null; if (xContentType == null && sqlRequest.columnar()) { throw new IllegalArgumentException("Invalid use of [columnar] argument: cannot be used in combination with " + "txt, csv or tsv formats"); } + /* + * Special handling for the "delimiter" parameter which should only be + * checked for being present or not in the case of CSV format. We cannot + * override {@link BaseRestHandler#responseParams()} because this + * parameter should only be checked for CSV, not always. + */ + if ((textFormat == null || textFormat != TextFormat.CSV) && request.hasParam(URL_PARAM_DELIMITER)) { + throw new IllegalArgumentException(unrecognized(request, Collections.singleton(URL_PARAM_DELIMITER), emptySet(), "parameter")); + } + long startNanos = System.nanoTime(); return channel -> client.execute(SqlQueryAction.INSTANCE, sqlRequest, new RestResponseListener(channel) { @Override @@ -145,7 +154,7 @@ public RestResponse buildResponse(SqlQueryResponse response) throws Exception { @Override protected Set responseParams() { - return textFormat == TextFormat.CSV ? Collections.singleton(URL_PARAM_DELIMITER) : Collections.emptySet(); + return Collections.singleton(URL_PARAM_DELIMITER); } @Override