Преглед изворни кода

Have create index return a bad request on poor formatting (#123761)

closes: https://github.com/elastic/elasticsearch/issues/123661
Benjamin Trent пре 7 месеци
родитељ
комит
a1ee3c9291

+ 5 - 0
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: []

+ 38 - 0
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 ]

+ 10 - 3
server/src/main/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequest.java

@@ -408,16 +408,17 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
         for (Map.Entry<String, ?> 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<String, Object>) entry.getValue());
             } else if (MAPPINGS.match(name, deprecationHandler)) {
+                validateIsMap(MAPPINGS.getPreferredName(), entry.getValue());
                 Map<String, Object> mappings = (Map<String, Object>) entry.getValue();
                 for (Map.Entry<String, Object> entry1 : mappings.entrySet()) {
+                    validateIsMap(entry1.getKey(), entry1.getValue());
                     mapping(entry1.getKey(), (Map<String, Object>) entry1.getValue());
                 }
             } else if (ALIASES.match(name, deprecationHandler)) {
+                validateIsMap(ALIASES.getPreferredName(), entry.getValue());
                 aliases((Map<String, Object>) entry.getValue());
             } else {
                 throw new ElasticsearchParseException("unknown key [{}] for create index", name);
@@ -426,6 +427,12 @@ public class CreateIndexRequest extends AcknowledgedRequest<CreateIndexRequest>
         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;
     }

+ 3 - 0
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<String> CAPABILITIES = Set.of(
         LOGSDB_INDEX_MODE_CAPABILITY,
         LOOKUP_INDEX_MODE_CAPABILITY,
         NESTED_DENSE_VECTOR_SYNTHETIC_TEST,
+        POORLY_FORMATTED_BAD_REQUEST,
         HUNSPELL_DICT_400
     );
 }