diff --git a/docs/changelog/123761.yaml b/docs/changelog/123761.yaml new file mode 100644 index 0000000000000..340a235a5bfc1 --- /dev/null +++ b/docs/changelog/123761.yaml @@ -0,0 +1,5 @@ +pr: 123761 +summary: Have create index return a bad request on poor formatting +area: Infra/Core +type: bug +issues: [] diff --git a/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/indices.create/10_basic.yml b/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/indices.create/10_basic.yml index 607f7acde5337..378bc909ca3c5 100644 --- a/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/indices.create/10_basic.yml +++ b/rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/indices.create/10_basic.yml @@ -215,6 +215,44 @@ index.number_of_shards: 2 --- +"Poorly formatted request returns bad_request": + - requires: + test_runner_features: [ capabilities ] + capabilities: + - method: PUT + path: /{index} + capabilities: [ poorly_formatted_bad_request ] + reason: "requires poorly_formatted_bad_request bug fix" + + - do: + catch: bad_request + indices.create: + index: test_index + body: + mappings: "bad mappings" + + - do: + catch: bad_request + indices.create: + index: test_index + body: + mappings: + properties: "bad properties" + + - do: + catch: bad_request + indices.create: + index: test_index + body: + settings: "bad settings" + + - do: + catch: bad_request + indices.create: + index: test_index + body: + aliases: "bad alias" +--- "Create index with hunspell missing dict": - requires: test_runner_features: [ capabilities ] diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java b/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java index cc96954c8a8e4..dba86df160955 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java @@ -410,16 +410,17 @@ public CreateIndexRequest source(Map source, DeprecationHandler depre for (Map.Entry entry : source.entrySet()) { String name = entry.getKey(); if (SETTINGS.match(name, deprecationHandler)) { - if (entry.getValue() instanceof Map == false) { - throw new ElasticsearchParseException("key [settings] must be an object"); - } + validateIsMap(SETTINGS.getPreferredName(), entry.getValue()); settings((Map) entry.getValue()); } else if (MAPPINGS.match(name, deprecationHandler)) { + validateIsMap(MAPPINGS.getPreferredName(), entry.getValue()); Map mappings = (Map) entry.getValue(); for (Map.Entry entry1 : mappings.entrySet()) { + validateIsMap(entry1.getKey(), entry1.getValue()); mapping(entry1.getKey(), (Map) entry1.getValue()); } } else if (ALIASES.match(name, deprecationHandler)) { + validateIsMap(ALIASES.getPreferredName(), entry.getValue()); aliases((Map) entry.getValue()); } else { throw new ElasticsearchParseException("unknown key [{}] for create index", name); @@ -428,6 +429,12 @@ public CreateIndexRequest source(Map source, DeprecationHandler depre return this; } + static void validateIsMap(String key, Object value) { + if (value instanceof Map == false) { + throw new ElasticsearchParseException("key [{}] must be an object", key); + } + } + public String mappings() { return this.mappings; } diff --git a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/CreateIndexCapabilities.java b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/CreateIndexCapabilities.java index 928c872b6ad71..fa2de167ea17e 100644 --- a/server/src/main/java/org/elasticsearch/rest/action/admin/indices/CreateIndexCapabilities.java +++ b/server/src/main/java/org/elasticsearch/rest/action/admin/indices/CreateIndexCapabilities.java @@ -28,12 +28,15 @@ public class CreateIndexCapabilities { private static final String NESTED_DENSE_VECTOR_SYNTHETIC_TEST = "nested_dense_vector_synthetic_test"; + private static final String POORLY_FORMATTED_BAD_REQUEST = "poorly_formatted_bad_request"; + private static final String HUNSPELL_DICT_400 = "hunspell_dict_400"; public static final Set CAPABILITIES = Set.of( LOGSDB_INDEX_MODE_CAPABILITY, LOOKUP_INDEX_MODE_CAPABILITY, NESTED_DENSE_VECTOR_SYNTHETIC_TEST, + POORLY_FORMATTED_BAD_REQUEST, HUNSPELL_DICT_400 ); }