From f00a419f81e0e1aa1b07ea69d5f1074eb4ff5511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Tue, 24 Sep 2019 21:00:58 +0200 Subject: [PATCH] Also handle cases where mappings stored without a type in template --- .../deprecation/ClusterDeprecationChecks.java | 16 +++++++++------- .../ClusterDeprecationChecksTests.java | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java index 8abdb37ba7ac1..c1948d3446b5c 100644 --- a/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java +++ b/x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java @@ -13,7 +13,6 @@ import org.elasticsearch.common.Strings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentHelper; -import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.mapper.FieldNamesFieldMapper; import org.elasticsearch.ingest.IngestService; @@ -105,12 +104,15 @@ static DeprecationIssue checkTemplatesWithFieldNamesDisabled(ClusterState state) state.getMetaData().getTemplates().forEach((templateCursor) -> { String templateName = templateCursor.key; templateCursor.value.getMappings().forEach((mappingCursor) -> { - Map map = XContentHelper.convertToMap(mappingCursor.value.compressedReference(), false, XContentType.JSON) - .v2(); - // map should contain only type name at this level - assert map.size() == 1; - String type = map.keySet().iterator().next(); - if (mapContainsFieldNamesDisabled((Map) map.get(type))) { + String type = mappingCursor.key; + // there should be the type name at this level, but there was a bug where mappings could be stored without a type (#45120) + // to make sure, we try to detect this like we try to do in MappingMetaData#sourceAsMap() + Map mapping = XContentHelper.convertToMap(mappingCursor.value.compressedReference(), true).v2(); + if (mapping.size() == 1 && mapping.containsKey(type)) { + // the type name is the root value, reduce it + mapping = (Map) mapping.get(type); + } + if (mapContainsFieldNamesDisabled(mapping)) { templatesContainingFieldNames.add(templateName); } }); diff --git a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java index cf19dc61b8fb9..f63a62331da70 100644 --- a/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java +++ b/x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java @@ -192,6 +192,21 @@ public void testTemplatesWithFieldNamesDisabled() throws IOException { } badMappingBuilder.endObject(); assertFieldNamesEnabledTemplate(badMappingBuilder, true); + + // however, there was a bug where mappings could be stored without a type (#45120) + // so we also should try to check these cases + + XContentBuilder badMappingWithoutTypeBuilder = jsonBuilder(); + badMappingWithoutTypeBuilder.startObject(); + { + badMappingWithoutTypeBuilder.startObject(FieldNamesFieldMapper.NAME); + { + badMappingWithoutTypeBuilder.field("enabled", randomBoolean()); + } + badMappingWithoutTypeBuilder.endObject(); + } + badMappingWithoutTypeBuilder.endObject(); + assertFieldNamesEnabledTemplate(badMappingWithoutTypeBuilder, true); } private void assertFieldNamesEnabledTemplate(XContentBuilder templateBuilder, boolean expectIssue) throws IOException {