Skip to content

Commit 09ac0e6

Browse files
authored
Have create index return a bad request on poor formatting (#123761) (#124343)
closes: #123661 (cherry picked from commit a1ee3c9)
1 parent d3fa3b7 commit 09ac0e6

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
@@ -410,16 +410,17 @@ public CreateIndexRequest source(Map<String, ?> source, DeprecationHandler depre
410410
for (Map.Entry<String, ?> entry : source.entrySet()) {
411411
String name = entry.getKey();
412412
if (SETTINGS.match(name, deprecationHandler)) {
413-
if (entry.getValue() instanceof Map == false) {
414-
throw new ElasticsearchParseException("key [settings] must be an object");
415-
}
413+
validateIsMap(SETTINGS.getPreferredName(), entry.getValue());
416414
settings((Map<String, Object>) entry.getValue());
417415
} else if (MAPPINGS.match(name, deprecationHandler)) {
416+
validateIsMap(MAPPINGS.getPreferredName(), entry.getValue());
418417
Map<String, Object> mappings = (Map<String, Object>) entry.getValue();
419418
for (Map.Entry<String, Object> entry1 : mappings.entrySet()) {
419+
validateIsMap(entry1.getKey(), entry1.getValue());
420420
mapping(entry1.getKey(), (Map<String, Object>) entry1.getValue());
421421
}
422422
} else if (ALIASES.match(name, deprecationHandler)) {
423+
validateIsMap(ALIASES.getPreferredName(), entry.getValue());
423424
aliases((Map<String, Object>) entry.getValue());
424425
} else {
425426
throw new ElasticsearchParseException("unknown key [{}] for create index", name);
@@ -428,6 +429,12 @@ public CreateIndexRequest source(Map<String, ?> source, DeprecationHandler depre
428429
return this;
429430
}
430431

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

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)