From e142d693c725f6315cd9a671681fa9a687666479 Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Mon, 8 Jun 2020 14:21:26 -0600 Subject: [PATCH] [7.8] Throw exception on duplicate mappings metadata fields (#57840) This is a backport of #57835 In #57701 we changed mappings merging so that duplicate fields specified in mappings caused an exception during validation. This change makes the same exception thrown when metadata fields are duplicated. This will allow us to be strict currently with plans to make the merging more fine-grained in a later release. --- .../cluster/metadata/MetadataCreateIndexService.java | 8 ++------ .../cluster/metadata/MetadataIndexTemplateService.java | 9 ++++----- .../metadata/MetadataCreateIndexServiceTests.java | 2 ++ 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java index d9693258f41d7..9951be06c010f 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java @@ -585,9 +585,7 @@ static Map> parseV2Mappings(String mappingsJson, Lis Map innerTemplateNonProperties = new HashMap<>(innerTemplateMapping); Map maybeProperties = (Map) innerTemplateNonProperties.remove("properties"); - nonProperties = removeDuplicatedDynamicTemplates(nonProperties, innerTemplateNonProperties); - XContentHelper.mergeDefaults(innerTemplateNonProperties, nonProperties); - nonProperties = innerTemplateNonProperties; + nonProperties = mergeFailingOnReplacement(nonProperties, innerTemplateNonProperties); if (maybeProperties != null) { properties = mergeFailingOnReplacement(properties, maybeProperties); @@ -600,9 +598,7 @@ static Map> parseV2Mappings(String mappingsJson, Lis Map innerRequestNonProperties = new HashMap<>(innerRequestMappings); Map maybeRequestProperties = (Map) innerRequestNonProperties.remove("properties"); - nonProperties = removeDuplicatedDynamicTemplates(nonProperties, innerRequestMappings); - XContentHelper.mergeDefaults(innerRequestNonProperties, nonProperties); - nonProperties = innerRequestNonProperties; + nonProperties = mergeFailingOnReplacement(nonProperties, innerRequestNonProperties); if (maybeRequestProperties != null) { properties = mergeFailingOnReplacement(properties, maybeRequestProperties); diff --git a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java index 3c2dd9e55910b..b8cd1a6e7429a 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java @@ -949,12 +949,11 @@ private static void validateCompositeTemplate(final ClusterState state, // Parse mappings to ensure they are valid after being composed List mappings = resolveMappings(stateWithIndex, templateName); try { + Map> finalMappings = + MetadataCreateIndexService.parseV2Mappings("{}", mappings, xContentRegistry); MapperService dummyMapperService = tempIndexService.mapperService(); - for (CompressedXContent mapping : mappings) { - // TODO: Eventually change this to: - // dummyMapperService.merge(MapperService.SINGLE_MAPPING_NAME, mapping, MergeReason.INDEX_TEMPLATE); - dummyMapperService.merge(MapperService.SINGLE_MAPPING_NAME, mapping, MergeReason.MAPPING_UPDATE); - } + // TODO: Eventually change this to use MergeReason.INDEX_TEMPLATE + dummyMapperService.merge(finalMappings, MergeReason.MAPPING_UPDATE); } catch (Exception e) { throw new IllegalArgumentException("invalid composite mappings for [" + templateName + "]", e); } diff --git a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexServiceTests.java b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexServiceTests.java index 132eca91a6761..859b99da11505 100644 --- a/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexServiceTests.java +++ b/server/src/test/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexServiceTests.java @@ -1174,6 +1174,7 @@ public void testDedupTemplateDynamicTemplates() throws Exception { dynamicMapping.get("path_match"), is("docker.container.labels.*")); } + @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/pull/57393") public void testDedupRequestDynamicTemplates() throws Exception { String requestMappingJson = "{\"_doc\":{\"_source\":{\"enabled\": false}, \"dynamic_templates\": [" + "{\n" + @@ -1235,6 +1236,7 @@ public void testDedupRequestDynamicTemplates() throws Exception { assertThat(mapping.get("type"), is("keyword")); } + @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/pull/57393") public void testMultipleComponentTemplatesDefineSameDynamicTemplate() throws Exception { String ct1Mapping = "{\"_doc\":{\"_source\":{\"enabled\": false}, \"dynamic_templates\": [" + "{\n" +