Skip to content

Commit 7743104

Browse files
committed
Throw exception on duplicate mappings metadata fields
In elastic#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.
1 parent eda4da1 commit 7743104

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexService.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,7 @@ static Map<String, Map<String, Object>> parseV2Mappings(String mappingsJson, Lis
585585
Map<String, Object> innerTemplateNonProperties = new HashMap<>(innerTemplateMapping);
586586
Map<String, Object> maybeProperties = (Map<String, Object>) innerTemplateNonProperties.remove("properties");
587587

588-
nonProperties = removeDuplicatedDynamicTemplates(nonProperties, innerTemplateNonProperties);
589-
XContentHelper.mergeDefaults(innerTemplateNonProperties, nonProperties);
590-
nonProperties = innerTemplateNonProperties;
588+
nonProperties = mergeFailingOnReplacement(nonProperties, innerTemplateNonProperties);
591589

592590
if (maybeProperties != null) {
593591
properties = mergeFailingOnReplacement(properties, maybeProperties);
@@ -600,9 +598,7 @@ static Map<String, Map<String, Object>> parseV2Mappings(String mappingsJson, Lis
600598
Map<String, Object> innerRequestNonProperties = new HashMap<>(innerRequestMappings);
601599
Map<String, Object> maybeRequestProperties = (Map<String, Object>) innerRequestNonProperties.remove("properties");
602600

603-
nonProperties = removeDuplicatedDynamicTemplates(nonProperties, innerRequestMappings);
604-
XContentHelper.mergeDefaults(innerRequestNonProperties, nonProperties);
605-
nonProperties = innerRequestNonProperties;
601+
nonProperties = mergeFailingOnReplacement(nonProperties, innerRequestNonProperties);
606602

607603
if (maybeRequestProperties != null) {
608604
properties = mergeFailingOnReplacement(properties, maybeRequestProperties);

server/src/main/java/org/elasticsearch/cluster/metadata/MetadataIndexTemplateService.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -949,12 +949,11 @@ private static void validateCompositeTemplate(final ClusterState state,
949949
// Parse mappings to ensure they are valid after being composed
950950
List<CompressedXContent> mappings = resolveMappings(stateWithIndex, templateName);
951951
try {
952+
Map<String, Map<String, Object>> finalMappings =
953+
MetadataCreateIndexService.parseV2Mappings("{}", mappings, xContentRegistry);
952954
MapperService dummyMapperService = tempIndexService.mapperService();
953-
for (CompressedXContent mapping : mappings) {
954-
// TODO: Eventually change this to:
955-
// dummyMapperService.merge(MapperService.SINGLE_MAPPING_NAME, mapping, MergeReason.INDEX_TEMPLATE);
956-
dummyMapperService.merge(MapperService.SINGLE_MAPPING_NAME, mapping, MergeReason.MAPPING_UPDATE);
957-
}
955+
// TODO: Eventually change this to use MergeReason.INDEX_TEMPLATE
956+
dummyMapperService.merge(finalMappings, MergeReason.MAPPING_UPDATE);
958957
} catch (Exception e) {
959958
throw new IllegalArgumentException("invalid composite mappings for [" + templateName + "]", e);
960959
}

0 commit comments

Comments
 (0)