Skip to content

Commit fbf0d8f

Browse files
authored
Have create index return a bad request on poor formatting (#123761) (#124344)
closes: #123661 (cherry picked from commit a1ee3c9)
1 parent 1f4d879 commit fbf0d8f

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
@@ -215,6 +215,44 @@
215215
index.number_of_shards: 2
216216

217217
---
218+
"Poorly formatted request returns bad_request":
219+
- requires:
220+
test_runner_features: [ capabilities ]
221+
capabilities:
222+
- method: PUT
223+
path: /{index}
224+
capabilities: [ poorly_formatted_bad_request ]
225+
reason: "requires poorly_formatted_bad_request bug fix"
226+
227+
- do:
228+
catch: bad_request
229+
indices.create:
230+
index: test_index
231+
body:
232+
mappings: "bad mappings"
233+
234+
- do:
235+
catch: bad_request
236+
indices.create:
237+
index: test_index
238+
body:
239+
mappings:
240+
properties: "bad properties"
241+
242+
- do:
243+
catch: bad_request
244+
indices.create:
245+
index: test_index
246+
body:
247+
settings: "bad settings"
248+
249+
- do:
250+
catch: bad_request
251+
indices.create:
252+
index: test_index
253+
body:
254+
aliases: "bad alias"
255+
---
218256
"Create index with hunspell missing dict":
219257
- requires:
220258
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)