Skip to content

Commit f00a419

Browse files
author
Christoph Büscher
committed
Also handle cases where mappings stored without a type in template
1 parent 67e44ae commit f00a419

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

x-pack/plugin/deprecation/src/main/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecks.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.elasticsearch.common.Strings;
1414
import org.elasticsearch.common.unit.TimeValue;
1515
import org.elasticsearch.common.xcontent.XContentHelper;
16-
import org.elasticsearch.common.xcontent.XContentType;
1716
import org.elasticsearch.index.IndexSettings;
1817
import org.elasticsearch.index.mapper.FieldNamesFieldMapper;
1918
import org.elasticsearch.ingest.IngestService;
@@ -105,12 +104,15 @@ static DeprecationIssue checkTemplatesWithFieldNamesDisabled(ClusterState state)
105104
state.getMetaData().getTemplates().forEach((templateCursor) -> {
106105
String templateName = templateCursor.key;
107106
templateCursor.value.getMappings().forEach((mappingCursor) -> {
108-
Map<String, Object> map = XContentHelper.convertToMap(mappingCursor.value.compressedReference(), false, XContentType.JSON)
109-
.v2();
110-
// map should contain only type name at this level
111-
assert map.size() == 1;
112-
String type = map.keySet().iterator().next();
113-
if (mapContainsFieldNamesDisabled((Map<?,?>) map.get(type))) {
107+
String type = mappingCursor.key;
108+
// there should be the type name at this level, but there was a bug where mappings could be stored without a type (#45120)
109+
// to make sure, we try to detect this like we try to do in MappingMetaData#sourceAsMap()
110+
Map<String, Object> mapping = XContentHelper.convertToMap(mappingCursor.value.compressedReference(), true).v2();
111+
if (mapping.size() == 1 && mapping.containsKey(type)) {
112+
// the type name is the root value, reduce it
113+
mapping = (Map<String, Object>) mapping.get(type);
114+
}
115+
if (mapContainsFieldNamesDisabled(mapping)) {
114116
templatesContainingFieldNames.add(templateName);
115117
}
116118
});

x-pack/plugin/deprecation/src/test/java/org/elasticsearch/xpack/deprecation/ClusterDeprecationChecksTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,21 @@ public void testTemplatesWithFieldNamesDisabled() throws IOException {
192192
}
193193
badMappingBuilder.endObject();
194194
assertFieldNamesEnabledTemplate(badMappingBuilder, true);
195+
196+
// however, there was a bug where mappings could be stored without a type (#45120)
197+
// so we also should try to check these cases
198+
199+
XContentBuilder badMappingWithoutTypeBuilder = jsonBuilder();
200+
badMappingWithoutTypeBuilder.startObject();
201+
{
202+
badMappingWithoutTypeBuilder.startObject(FieldNamesFieldMapper.NAME);
203+
{
204+
badMappingWithoutTypeBuilder.field("enabled", randomBoolean());
205+
}
206+
badMappingWithoutTypeBuilder.endObject();
207+
}
208+
badMappingWithoutTypeBuilder.endObject();
209+
assertFieldNamesEnabledTemplate(badMappingWithoutTypeBuilder, true);
195210
}
196211

197212
private void assertFieldNamesEnabledTemplate(XContentBuilder templateBuilder, boolean expectIssue) throws IOException {

0 commit comments

Comments
 (0)