From 742213d7105dddf91d4bc414e1ef72b198109541 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Fri, 16 Aug 2019 11:39:03 -0700 Subject: [PATCH] Improve error message when index settings are not a map (#45588) This commit adds an explicit error message when a create index request contains a settings key that is not a json object. Prior to this change the user would be given a ClassCastException with no explanation of what went wrong. closes #45126 --- .../admin/indices/create/CreateIndexRequest.java | 3 +++ .../admin/indices/create/CreateIndexRequestTests.java | 10 ++++++++++ 2 files changed, 13 insertions(+) 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 11f09f3c7bb..202ce364ebf 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 @@ -402,6 +402,9 @@ public class CreateIndexRequest extends AcknowledgedRequest 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"); + } settings((Map) entry.getValue()); } else if (MAPPINGS.match(name, deprecationHandler)) { Map mappings = (Map) entry.getValue(); diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestTests.java index 419d669d066..e95c4e43774 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/create/CreateIndexRequestTests.java @@ -44,6 +44,7 @@ import java.util.Set; import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_SHARDS; import static org.elasticsearch.common.xcontent.ToXContent.EMPTY_PARAMS; +import static org.hamcrest.CoreMatchers.equalTo; public class CreateIndexRequestTests extends ESTestCase { @@ -196,6 +197,15 @@ public class CreateIndexRequestTests extends ESTestCase { ElasticsearchAssertions.assertToXContentEquivalent(originalBytes, finalBytes, xContentType); } + public void testSettingsType() throws IOException { + XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values())); + builder.startObject().startArray("settings").endArray().endObject(); + + CreateIndexRequest parsedCreateIndexRequest = new CreateIndexRequest(); + ElasticsearchParseException e = expectThrows(ElasticsearchParseException.class, () -> parsedCreateIndexRequest.source(builder)); + assertThat(e.getMessage(), equalTo("key [settings] must be an object")); + } + public static void assertMappingsEqual(Map expected, Map actual) throws IOException { assertEquals(expected.keySet(), actual.keySet());