Skip to content

Commit 18618de

Browse files
committed
ESQL: Fail profile on text response formats
Fails requests with `profile` when the request has a text response format. This information is just not included in that response.
1 parent dc2fbe1 commit 18618de

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public static MediaType getResponseMediaType(RestRequest request, EsqlQueryReque
4545
var mediaType = getResponseMediaType(request, (MediaType) null);
4646
validateColumnarRequest(esqlRequest.columnar(), mediaType);
4747
validateIncludeCCSMetadata(esqlRequest.includeCCSMetadata(), mediaType);
48+
validateProfile(esqlRequest.profile(), mediaType);
4849
return checkNonNullMediaType(mediaType, request);
4950
}
5051

@@ -67,24 +68,32 @@ private static MediaType mediaTypeFromParams(RestRequest request) {
6768

6869
private static void validateColumnarRequest(boolean requestIsColumnar, MediaType fromMediaType) {
6970
if (requestIsColumnar && fromMediaType instanceof TextFormat) {
70-
throw new IllegalArgumentException(
71-
"Invalid use of [columnar] argument: cannot be used in combination with "
72-
+ Arrays.stream(TextFormat.values()).map(MediaType::queryParameter).toList()
73-
+ " formats"
74-
);
71+
throw invalid("columnar");
7572
}
7673
}
7774

7875
private static void validateIncludeCCSMetadata(boolean includeCCSMetadata, MediaType fromMediaType) {
7976
if (includeCCSMetadata && fromMediaType instanceof TextFormat) {
80-
throw new IllegalArgumentException(
81-
"Invalid use of [include_ccs_metadata] argument: cannot be used in combination with "
82-
+ Arrays.stream(TextFormat.values()).map(MediaType::queryParameter).toList()
83-
+ " formats"
84-
);
77+
throw invalid("include_ccs_metadata");
8578
}
8679
}
8780

81+
private static void validateProfile(boolean profile, MediaType fromMediaType) {
82+
if (profile && fromMediaType instanceof TextFormat) {
83+
throw invalid("profile");
84+
}
85+
}
86+
87+
private static IllegalArgumentException invalid(String argument) {
88+
return new IllegalArgumentException(
89+
"Invalid use of ["
90+
+ argument
91+
+ "] argument: cannot be used in combination with "
92+
+ Arrays.stream(TextFormat.values()).map(MediaType::queryParameter).toList()
93+
+ " formats"
94+
);
95+
}
96+
8897
private static MediaType checkNonNullMediaType(MediaType mediaType, RestRequest request) {
8998
if (mediaType == null) {
9099
String msg = String.format(

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public void testIncludeCCSMetadataWithAcceptText() {
8585
var accept = randomFrom("text/plain", "text/csv", "text/tab-separated-values");
8686
IllegalArgumentException e = expectThrows(
8787
IllegalArgumentException.class,
88-
() -> getResponseMediaType(reqWithAccept(accept), createTestInstance(false, true))
88+
() -> getResponseMediaType(reqWithAccept(accept), createTestInstance(false, true, false))
8989
);
9090
assertEquals(
9191
"Invalid use of [include_ccs_metadata] argument: cannot be used in combination with [txt, csv, tsv] formats",
@@ -106,7 +106,7 @@ public void testIncludeCCSMetadataWithNonJSONMediaTypesInParams() {
106106
RestRequest restRequest = reqWithParams(Map.of("format", randomFrom("txt", "csv", "tsv")));
107107
IllegalArgumentException e = expectThrows(
108108
IllegalArgumentException.class,
109-
() -> getResponseMediaType(restRequest, createTestInstance(false, true))
109+
() -> getResponseMediaType(restRequest, createTestInstance(false, true, false))
110110
);
111111
assertEquals(
112112
"Invalid use of [include_ccs_metadata] argument: cannot be used in combination with [txt, csv, tsv] formats",
@@ -116,7 +116,27 @@ public void testIncludeCCSMetadataWithNonJSONMediaTypesInParams() {
116116
{
117117
// check that no exception is thrown for the XContent types
118118
RestRequest restRequest = reqWithParams(Map.of("format", randomFrom("SMILE", "YAML", "CBOR", "JSON")));
119-
MediaType responseMediaType = getResponseMediaType(restRequest, createTestInstance(true, true));
119+
MediaType responseMediaType = getResponseMediaType(restRequest, createTestInstance(true, true, false));
120+
assertNotNull(responseMediaType);
121+
}
122+
}
123+
124+
public void testProfileWithNonJSONMediaTypesInParams() {
125+
{
126+
RestRequest restRequest = reqWithParams(Map.of("format", randomFrom("txt", "csv", "tsv")));
127+
IllegalArgumentException e = expectThrows(
128+
IllegalArgumentException.class,
129+
() -> getResponseMediaType(restRequest, createTestInstance(false, false, true))
130+
);
131+
assertEquals(
132+
"Invalid use of [profile] argument: cannot be used in combination with [txt, csv, tsv] formats",
133+
e.getMessage()
134+
);
135+
}
136+
{
137+
// check that no exception is thrown for the XContent types
138+
RestRequest restRequest = reqWithParams(Map.of("format", randomFrom("SMILE", "YAML", "CBOR", "JSON")));
139+
MediaType responseMediaType = getResponseMediaType(restRequest, createTestInstance(true, false, true));
120140
assertNotNull(responseMediaType);
121141
}
122142
}
@@ -157,9 +177,10 @@ protected EsqlQueryRequest createTestInstance(boolean columnar) {
157177
return request;
158178
}
159179

160-
protected EsqlQueryRequest createTestInstance(boolean columnar, boolean includeCCSMetadata) {
180+
protected EsqlQueryRequest createTestInstance(boolean columnar, boolean includeCCSMetadata, boolean profile) {
161181
var request = createTestInstance(columnar);
162182
request.includeCCSMetadata(includeCCSMetadata);
183+
request.profile(profile);
163184
return request;
164185
}
165186
}

0 commit comments

Comments
 (0)