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 dc5fcf1596e89..f9ea2f63351ff 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 @@ -274,6 +274,44 @@ - match: { error.type: "mapper_parsing_exception" } - 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." } --- +"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 580da139aa0f1..f191ab3827347 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 @@ -408,16 +408,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); @@ -426,6 +427,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 ); }