Skip to content

Commit 68ff7ca

Browse files
authored
Remove TextFormat as an instance variable (#69895) (#69912)
(cherry picked from commit 20f9397)
1 parent ade7268 commit 68ff7ca

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

x-pack/plugin/sql/qa/server/src/main/java/org/elasticsearch/xpack/sql/qa/rest/RestSqlTestCase.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,35 @@ public void testNextPageCSV() throws IOException {
934934
executeQueryWithNextPage("text/csv; header=present", "text,number,sum\r\n", "%s,%d,%d\r\n");
935935
}
936936

937+
public void testCSVWithDelimiterParameter() throws IOException {
938+
String format = randomFrom("txt", "tsv", "json", "yaml", "smile", "cbor");
939+
String query = "SELECT * FROM test";
940+
index("{\"foo\":1}");
941+
942+
Request badRequest = new Request("POST", SQL_QUERY_REST_ENDPOINT);
943+
badRequest.addParameter("format", format);
944+
badRequest.addParameter("delimiter", ";");
945+
badRequest.setEntity(
946+
new StringEntity(
947+
query(query).mode(randomValueOtherThan(Mode.JDBC.toString(), BaseRestSqlTestCase::randomMode)).toString(),
948+
ContentType.APPLICATION_JSON
949+
)
950+
);
951+
expectBadRequest(() -> {
952+
client().performRequest(badRequest);
953+
return Collections.emptyMap();
954+
}, containsString("request [/_sql] contains unrecognized parameter: [delimiter]"));
955+
956+
Request csvRequest = new Request("POST", SQL_QUERY_REST_ENDPOINT + "?format=csv&delimiter=%3B");
957+
csvRequest.setEntity(
958+
new StringEntity(
959+
query(query).mode(randomValueOtherThan(Mode.JDBC.toString(), BaseRestSqlTestCase::randomMode)).toString(),
960+
ContentType.APPLICATION_JSON
961+
)
962+
);
963+
assertOK(client().performRequest(csvRequest));
964+
}
965+
937966
public void testQueryInTSV() throws IOException {
938967
index(
939968
"{\"name\":" + toJson("first") + ", \"number\" : 1 }",

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plugin/RestSqlQueryAction.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131

3232
import static java.util.Arrays.asList;
3333
import static java.util.Collections.emptyList;
34+
import static java.util.Collections.emptySet;
3435
import static java.util.Collections.unmodifiableList;
3536
import static org.elasticsearch.rest.RestRequest.Method.GET;
3637
import static org.elasticsearch.rest.RestRequest.Method.POST;
@@ -39,8 +40,6 @@
3940

4041
public class RestSqlQueryAction extends BaseRestHandler {
4142

42-
TextFormat textFormat;
43-
4443
@Override
4544
public List<Route> routes() {
4645
return emptyList();
@@ -106,13 +105,23 @@ protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient cli
106105
* which we turn into a 400 error.
107106
*/
108107
XContentType xContentType = accept == null ? XContentType.JSON : XContentType.fromMediaTypeOrFormat(accept);
109-
textFormat = xContentType == null ? TextFormat.fromMediaTypeOrFormat(accept) : null;
108+
TextFormat textFormat = xContentType == null ? TextFormat.fromMediaTypeOrFormat(accept) : null;
110109

111110
if (xContentType == null && sqlRequest.columnar()) {
112111
throw new IllegalArgumentException("Invalid use of [columnar] argument: cannot be used in combination with "
113112
+ "txt, csv or tsv formats");
114113
}
115114

115+
/*
116+
* Special handling for the "delimiter" parameter which should only be
117+
* checked for being present or not in the case of CSV format. We cannot
118+
* override {@link BaseRestHandler#responseParams()} because this
119+
* parameter should only be checked for CSV, not always.
120+
*/
121+
if ((textFormat == null || textFormat != TextFormat.CSV) && request.hasParam(URL_PARAM_DELIMITER)) {
122+
throw new IllegalArgumentException(unrecognized(request, Collections.singleton(URL_PARAM_DELIMITER), emptySet(), "parameter"));
123+
}
124+
116125
long startNanos = System.nanoTime();
117126
return channel -> client.execute(SqlQueryAction.INSTANCE, sqlRequest, new RestResponseListener<SqlQueryResponse>(channel) {
118127
@Override
@@ -145,7 +154,7 @@ public RestResponse buildResponse(SqlQueryResponse response) throws Exception {
145154

146155
@Override
147156
protected Set<String> responseParams() {
148-
return textFormat == TextFormat.CSV ? Collections.singleton(URL_PARAM_DELIMITER) : Collections.emptySet();
157+
return Collections.singleton(URL_PARAM_DELIMITER);
149158
}
150159

151160
@Override

0 commit comments

Comments
 (0)