From 64031865c1368e67036c8155e0dc4217688391d4 Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Mon, 8 Jun 2020 12:49:12 -0600 Subject: [PATCH] Throw exception on duplicate mappings metadata fields 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 | 6 ++---- .../cluster/metadata/MetadataIndexTemplateService.java | 10 ++++------ 2 files changed, 6 insertions(+), 10 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 5b1df19dfaaed..4453e8167fa11 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java @@ -591,8 +591,7 @@ static Map> parseV2Mappings(String mappingsJson, Lis Map innerTemplateNonProperties = new HashMap<>(innerTemplateMapping); Map maybeProperties = (Map) innerTemplateNonProperties.remove("properties"); - XContentHelper.mergeDefaults(innerTemplateNonProperties, nonProperties); - nonProperties = innerTemplateNonProperties; + nonProperties = mergeFailingOnReplacement(nonProperties, innerTemplateNonProperties); if (maybeProperties != null) { properties = mergeFailingOnReplacement(properties, maybeProperties); @@ -605,8 +604,7 @@ static Map> parseV2Mappings(String mappingsJson, Lis Map innerRequestNonProperties = new HashMap<>(innerRequestMappings); Map maybeRequestProperties = (Map) innerRequestNonProperties.remove("properties"); - 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 5c4d5db655d34..6efa323e5947f 100644 --- a/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java +++ b/server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java @@ -987,14 +987,12 @@ private static void validateCompositeTemplate(final ClusterState state, // Parse mappings to ensure they are valid after being composed List mappings = resolveMappings(stateWithIndex, templateName); - final Map> finalMappings; 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); }