Skip to content

Commit fe2eaf0

Browse files
authored
[7.x] Throw exception on duplicate mappings metadata fields (#57839)
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 952cf77 commit fe2eaf0

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,7 @@ static Map<String, Map<String, Object>> parseV2Mappings(String mappingsJson, Lis
591591
Map<String, Object> innerTemplateNonProperties = new HashMap<>(innerTemplateMapping);
592592
Map<String, Object> maybeProperties = (Map<String, Object>) innerTemplateNonProperties.remove("properties");
593593

594-
XContentHelper.mergeDefaults(innerTemplateNonProperties, nonProperties);
595-
nonProperties = innerTemplateNonProperties;
594+
nonProperties = mergeFailingOnReplacement(nonProperties, innerTemplateNonProperties);
596595

597596
if (maybeProperties != null) {
598597
properties = mergeFailingOnReplacement(properties, maybeProperties);
@@ -605,8 +604,7 @@ static Map<String, Map<String, Object>> parseV2Mappings(String mappingsJson, Lis
605604
Map<String, Object> innerRequestNonProperties = new HashMap<>(innerRequestMappings);
606605
Map<String, Object> maybeRequestProperties = (Map<String, Object>) innerRequestNonProperties.remove("properties");
607606

608-
XContentHelper.mergeDefaults(innerRequestNonProperties, nonProperties);
609-
nonProperties = innerRequestNonProperties;
607+
nonProperties = mergeFailingOnReplacement(nonProperties, innerRequestNonProperties);
610608

611609
if (maybeRequestProperties != null) {
612610
properties = mergeFailingOnReplacement(properties, maybeRequestProperties);

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -987,14 +987,12 @@ private static void validateCompositeTemplate(final ClusterState state,
987987

988988
// Parse mappings to ensure they are valid after being composed
989989
List<CompressedXContent> mappings = resolveMappings(stateWithIndex, templateName);
990-
final Map<String, Map<String, Object>> finalMappings;
991990
try {
991+
Map<String, Map<String, Object>> finalMappings =
992+
MetadataCreateIndexService.parseV2Mappings("{}", mappings, xContentRegistry);
992993
MapperService dummyMapperService = tempIndexService.mapperService();
993-
for (CompressedXContent mapping : mappings) {
994-
// TODO: Eventually change this to:
995-
// dummyMapperService.merge(MapperService.SINGLE_MAPPING_NAME, mapping, MergeReason.INDEX_TEMPLATE);
996-
dummyMapperService.merge(MapperService.SINGLE_MAPPING_NAME, mapping, MergeReason.MAPPING_UPDATE);
997-
}
994+
// TODO: Eventually change this to use MergeReason.INDEX_TEMPLATE
995+
dummyMapperService.merge(finalMappings, MergeReason.MAPPING_UPDATE);
998996
} catch (Exception e) {
999997
throw new IllegalArgumentException("invalid composite mappings for [" + templateName + "]", e);
1000998
}

0 commit comments

Comments
 (0)