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

closes: https://github.com/elastic/elasticsearch/issues/123661
This commit is contained in:
Benjamin Trent 2025-03-06 12:24:54 -05:00 committed by GitHub
parent 296cae8a30
commit a1ee3c9291
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 56 additions and 3 deletions

View File

@ -0,0 +1,5 @@
pr: 123761
summary: Have create index return a bad request on poor formatting
area: Infra/Core
type: bug
issues: []

View File

@ -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 ]

View File

@ -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;
}

View File

@ -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
);
}