Skip to content

Commit

Permalink
Also handle cases where mappings stored without a type in template
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Büscher committed Sep 24, 2019
1 parent 67e44ae commit f00a419
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.mapper.FieldNamesFieldMapper;
import org.elasticsearch.ingest.IngestService;
Expand Down Expand Up @@ -105,12 +104,15 @@ static DeprecationIssue checkTemplatesWithFieldNamesDisabled(ClusterState state)
state.getMetaData().getTemplates().forEach((templateCursor) -> {
String templateName = templateCursor.key;
templateCursor.value.getMappings().forEach((mappingCursor) -> {
Map<String, Object> map = XContentHelper.convertToMap(mappingCursor.value.compressedReference(), false, XContentType.JSON)
.v2();
// map should contain only type name at this level
assert map.size() == 1;
String type = map.keySet().iterator().next();
if (mapContainsFieldNamesDisabled((Map<?,?>) map.get(type))) {
String type = mappingCursor.key;
// there should be the type name at this level, but there was a bug where mappings could be stored without a type (#45120)
// to make sure, we try to detect this like we try to do in MappingMetaData#sourceAsMap()
Map<String, Object> mapping = XContentHelper.convertToMap(mappingCursor.value.compressedReference(), true).v2();
if (mapping.size() == 1 && mapping.containsKey(type)) {
// the type name is the root value, reduce it
mapping = (Map<String, Object>) mapping.get(type);
}
if (mapContainsFieldNamesDisabled(mapping)) {
templatesContainingFieldNames.add(templateName);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ public void testTemplatesWithFieldNamesDisabled() throws IOException {
}
badMappingBuilder.endObject();
assertFieldNamesEnabledTemplate(badMappingBuilder, true);

// however, there was a bug where mappings could be stored without a type (#45120)
// so we also should try to check these cases

XContentBuilder badMappingWithoutTypeBuilder = jsonBuilder();
badMappingWithoutTypeBuilder.startObject();
{
badMappingWithoutTypeBuilder.startObject(FieldNamesFieldMapper.NAME);
{
badMappingWithoutTypeBuilder.field("enabled", randomBoolean());
}
badMappingWithoutTypeBuilder.endObject();
}
badMappingWithoutTypeBuilder.endObject();
assertFieldNamesEnabledTemplate(badMappingWithoutTypeBuilder, true);
}

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

0 comments on commit f00a419

Please sign in to comment.