Skip to content

Commit e142d69

Browse files
authored
[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.
1 parent e070789 commit e142d69

File tree

3 files changed

+8
-11
lines changed

3 files changed

+8
-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
}

server/src/test/java/org/elasticsearch/cluster/metadata/MetadataCreateIndexServiceTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,7 @@ public void testDedupTemplateDynamicTemplates() throws Exception {
11741174
dynamicMapping.get("path_match"), is("docker.container.labels.*"));
11751175
}
11761176

1177+
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/pull/57393")
11771178
public void testDedupRequestDynamicTemplates() throws Exception {
11781179
String requestMappingJson = "{\"_doc\":{\"_source\":{\"enabled\": false}, \"dynamic_templates\": [" +
11791180
"{\n" +
@@ -1235,6 +1236,7 @@ public void testDedupRequestDynamicTemplates() throws Exception {
12351236
assertThat(mapping.get("type"), is("keyword"));
12361237
}
12371238

1239+
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/pull/57393")
12381240
public void testMultipleComponentTemplatesDefineSameDynamicTemplate() throws Exception {
12391241
String ct1Mapping = "{\"_doc\":{\"_source\":{\"enabled\": false}, \"dynamic_templates\": [" +
12401242
"{\n" +

0 commit comments

Comments
 (0)