Skip to content

Commit a1ee3c9

Browse files
authored
Have create index return a bad request on poor formatting (#123761)
closes: #123661
1 parent 296cae8 commit a1ee3c9

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

docs/changelog/123761.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 123761
2+
summary: Have create index return a bad request on poor formatting
3+
area: Infra/Core
4+
type: bug
5+
issues: []

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/indices.create/10_basic.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,44 @@
274274
- match: { error.type: "mapper_parsing_exception" }
275275
- match: { error.reason: "Failed to parse mapping: The mapper type [invalid] declared on field [raw] does not exist. It might have been created within a future version or requires a plugin to be installed. Check the documentation." }
276276
---
277+
"Poorly formatted request returns bad_request":
278+
- requires:
279+
test_runner_features: [ capabilities ]
280+
capabilities:
281+
- method: PUT
282+
path: /{index}
283+
capabilities: [ poorly_formatted_bad_request ]
284+
reason: "requires poorly_formatted_bad_request bug fix"
285+
286+
- do:
287+
catch: bad_request
288+
indices.create:
289+
index: test_index
290+
body:
291+
mappings: "bad mappings"
292+
293+
- do:
294+
catch: bad_request
295+
indices.create:
296+
index: test_index
297+
body:
298+
mappings:
299+
properties: "bad properties"
300+
301+
- do:
302+
catch: bad_request
303+
indices.create:
304+
index: test_index
305+
body:
306+
settings: "bad settings"
307+
308+
- do:
309+
catch: bad_request
310+
indices.create:
311+
index: test_index
312+
body:
313+
aliases: "bad alias"
314+
---
277315
"Create index with hunspell missing dict":
278316
- requires:
279317
test_runner_features: [ capabilities ]

server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,16 +408,17 @@ public CreateIndexRequest source(Map<String, ?> source, DeprecationHandler depre
408408
for (Map.Entry<String, ?> entry : source.entrySet()) {
409409
String name = entry.getKey();
410410
if (SETTINGS.match(name, deprecationHandler)) {
411-
if (entry.getValue() instanceof Map == false) {
412-
throw new ElasticsearchParseException("key [settings] must be an object");
413-
}
411+
validateIsMap(SETTINGS.getPreferredName(), entry.getValue());
414412
settings((Map<String, Object>) entry.getValue());
415413
} else if (MAPPINGS.match(name, deprecationHandler)) {
414+
validateIsMap(MAPPINGS.getPreferredName(), entry.getValue());
416415
Map<String, Object> mappings = (Map<String, Object>) entry.getValue();
417416
for (Map.Entry<String, Object> entry1 : mappings.entrySet()) {
417+
validateIsMap(entry1.getKey(), entry1.getValue());
418418
mapping(entry1.getKey(), (Map<String, Object>) entry1.getValue());
419419
}
420420
} else if (ALIASES.match(name, deprecationHandler)) {
421+
validateIsMap(ALIASES.getPreferredName(), entry.getValue());
421422
aliases((Map<String, Object>) entry.getValue());
422423
} else {
423424
throw new ElasticsearchParseException("unknown key [{}] for create index", name);
@@ -426,6 +427,12 @@ public CreateIndexRequest source(Map<String, ?> source, DeprecationHandler depre
426427
return this;
427428
}
428429

430+
static void validateIsMap(String key, Object value) {
431+
if (value instanceof Map == false) {
432+
throw new ElasticsearchParseException("key [{}] must be an object", key);
433+
}
434+
}
435+
429436
public String mappings() {
430437
return this.mappings;
431438
}

server/src/main/java/org/elasticsearch/rest/action/admin/indices/CreateIndexCapabilities.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ public class CreateIndexCapabilities {
2828

2929
private static final String NESTED_DENSE_VECTOR_SYNTHETIC_TEST = "nested_dense_vector_synthetic_test";
3030

31+
private static final String POORLY_FORMATTED_BAD_REQUEST = "poorly_formatted_bad_request";
32+
3133
private static final String HUNSPELL_DICT_400 = "hunspell_dict_400";
3234

3335
public static final Set<String> CAPABILITIES = Set.of(
3436
LOGSDB_INDEX_MODE_CAPABILITY,
3537
LOOKUP_INDEX_MODE_CAPABILITY,
3638
NESTED_DENSE_VECTOR_SYNTHETIC_TEST,
39+
POORLY_FORMATTED_BAD_REQUEST,
3740
HUNSPELL_DICT_400
3841
);
3942
}

0 commit comments

Comments
 (0)