Skip to content

Commit 5a5166a

Browse files
committed
[TEST] Improve validation of do sections (#34734)
We throw parsing exception when an unknown array is found, but we don't when an unknown top-level field is found. This commit makes sure that unsupported top-level fields are not ignored in a do section. Closes #34651
1 parent fbb5c10 commit 5a5166a

File tree

6 files changed

+34
-20
lines changed

6 files changed

+34
-20
lines changed

modules/lang-painless/src/test/resources/rest-api-spec/test/painless/30_search.yml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
indices.refresh: {}
2424

2525
- do:
26-
index: test
2726
search:
27+
index: test
2828
body:
2929
query:
3030
script:
@@ -45,8 +45,8 @@
4545
- match: { hits.hits.1.fields.sNum1.0: 3.0 }
4646

4747
- do:
48-
index: test
4948
search:
49+
index: test
5050
body:
5151
query:
5252
script:
@@ -70,8 +70,8 @@
7070
- match: { hits.hits.1.fields.sNum1.0: 3.0 }
7171

7272
- do:
73-
index: test
7473
search:
74+
index: test
7575
body:
7676
query:
7777
script:
@@ -96,8 +96,8 @@
9696
- match: { hits.hits.2.fields.sNum1.0: 3.0 }
9797

9898
- do:
99-
index: test
10099
search:
100+
index: test
101101
body:
102102
query:
103103
script:
@@ -127,8 +127,8 @@
127127
indices.refresh: {}
128128

129129
- do:
130-
index: test
131130
search:
131+
index: test
132132
body:
133133
query:
134134
function_score:
@@ -149,8 +149,8 @@
149149
- match: { hits.hits.1._id: "1" }
150150

151151
- do:
152-
index: test
153152
search:
153+
index: test
154154
body:
155155
query:
156156
function_score:
@@ -171,8 +171,8 @@
171171
- match: { hits.hits.1._id: "2" }
172172

173173
- do:
174-
index: test
175174
search:
175+
index: test
176176
body:
177177
query:
178178
function_score:
@@ -193,8 +193,8 @@
193193
- match: { hits.hits.1._id: "1" }
194194

195195
- do:
196-
index: test
197196
search:
197+
index: test
198198
body:
199199
query:
200200
function_score:
@@ -215,8 +215,8 @@
215215
- match: { hits.hits.1._id: "1" }
216216

217217
- do:
218-
index: test
219218
search:
219+
index: test
220220
body:
221221
query:
222222
function_score:
@@ -237,8 +237,8 @@
237237
- match: { hits.hits.1._id: "1" }
238238

239239
- do:
240-
index: test
241240
search:
241+
index: test
242242
body:
243243
query:
244244
function_score:
@@ -274,8 +274,8 @@
274274
indices.refresh: {}
275275

276276
- do:
277-
index: test
278277
search:
278+
index: test
279279
body:
280280
query:
281281
function_score:
@@ -325,8 +325,8 @@
325325

326326

327327
- do:
328-
index: test
329328
search:
329+
index: test
330330
body:
331331
query:
332332
function_score:
@@ -364,8 +364,8 @@
364364

365365

366366
- do:
367-
index: test
368367
search:
368+
index: test
369369
body:
370370
script_fields:
371371
foobar:
@@ -391,8 +391,8 @@
391391

392392

393393
- do:
394-
index: test
395394
search:
395+
index: test
396396
body:
397397
aggs:
398398
value_agg:
@@ -428,8 +428,8 @@
428428

429429
- do:
430430
catch: bad_request
431-
index: test
432431
search:
432+
index: test
433433
body:
434434
aggs:
435435
genre:

plugins/examples/painless-whitelist/src/test/resources/rest-api-spec/test/painless_whitelist/20_whitelist.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
indices.refresh: {}
1212

1313
- do:
14-
index: test
1514
search:
15+
index: test
1616
body:
1717
query:
1818
match_all: {}

plugins/examples/painless-whitelist/src/test/resources/rest-api-spec/test/painless_whitelist/30_static.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
indices.refresh: {}
1212

1313
- do:
14-
index: test
1514
search:
15+
index: test
1616
body:
1717
query:
1818
match_all: {}

plugins/examples/painless-whitelist/src/test/resources/rest-api-spec/test/painless_whitelist/40_instance.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
indices.refresh: {}
1212

1313
- do:
14-
index: test
1514
search:
15+
index: test
1616
body:
1717
query:
1818
match_all: {}
@@ -26,8 +26,8 @@
2626
- match: { hits.hits.0.fields.sNum1.0: 2 }
2727

2828
- do:
29-
index: test
3029
search:
30+
index: test
3131
body:
3232
query:
3333
match_all: {}

test/framework/src/main/java/org/elasticsearch/test/rest/yaml/section/DoSection.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ public static DoSection parse(XContentParser parser) throws IOException {
106106
} else if (token.isValue()) {
107107
if ("catch".equals(currentFieldName)) {
108108
doSection.setCatch(parser.text());
109+
} else {
110+
throw new ParsingException(parser.getTokenLocation(), "unsupported field [" + currentFieldName + "]");
109111
}
110112
} else if (token == XContentParser.Token.START_ARRAY) {
111113
if ("warnings".equals(currentFieldName)) {

test/framework/src/test/java/org/elasticsearch/test/rest/yaml/section/DoSectionTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.http.HttpHost;
2323
import org.elasticsearch.client.Node;
2424
import org.elasticsearch.client.NodeSelector;
25+
import org.elasticsearch.common.ParsingException;
2526
import org.elasticsearch.common.logging.DeprecationLogger;
2627
import org.elasticsearch.common.xcontent.XContent;
2728
import org.elasticsearch.common.xcontent.XContentLocation;
@@ -52,7 +53,7 @@
5253

5354
public class DoSectionTests extends AbstractClientYamlTestFragmentParserTestCase {
5455

55-
public void testWarningHeaders() throws IOException {
56+
public void testWarningHeaders() {
5657
{
5758
final DoSection section = new DoSection(new XContentLocation(1, 1));
5859

@@ -424,6 +425,17 @@ public void testParseDoSectionWithCatch() throws Exception {
424425
assertThat(doSection.getApiCallSection().hasBody(), equalTo(false));
425426
}
426427

428+
public void testUnsupportedTopLevelField() throws Exception {
429+
parser = createParser(YamlXContent.yamlXContent,
430+
"max_concurrent_shard_requests: 1"
431+
);
432+
433+
ParsingException e = expectThrows(ParsingException.class, () -> DoSection.parse(parser));
434+
assertThat(e.getMessage(), is("unsupported field [max_concurrent_shard_requests]"));
435+
parser.nextToken();
436+
parser.nextToken();
437+
}
438+
427439
public void testParseDoSectionWithHeaders() throws Exception {
428440
parser = createParser(YamlXContent.yamlXContent,
429441
"headers:\n" +

0 commit comments

Comments
 (0)