Mappings: Fix detection of metadata fields in documents
In 2.0, the ability to specify metadata fields like _routing and _ttl inside a document was removed. However, the ability to break through this restriction has lingered, and the check that enforced it is completely broken. This change fixes the check, and adds a parsing test.
This commit is contained in:
parent
227463c356
commit
4e48154130
|
@ -340,17 +340,13 @@ final class DocumentParser {
|
|||
return;
|
||||
}
|
||||
XContentParser parser = context.parser();
|
||||
|
||||
String currentFieldName = parser.currentName();
|
||||
if (atRoot && MapperService.isMetadataField(currentFieldName)) {
|
||||
throw new MapperParsingException("Field [" + currentFieldName + "] is a metadata field and cannot be added inside a document. Use the index API request parameters.");
|
||||
}
|
||||
XContentParser.Token token = parser.currentToken();
|
||||
if (token == XContentParser.Token.VALUE_NULL) {
|
||||
// the object is null ("obj1" : null), simply bail
|
||||
return;
|
||||
}
|
||||
|
||||
String currentFieldName = parser.currentName();
|
||||
if (token.isValue()) {
|
||||
throw new MapperParsingException("object mapping for [" + mapper.name() + "] tried to parse field [" + currentFieldName + "] as object, but found a concrete value");
|
||||
}
|
||||
|
@ -384,6 +380,9 @@ final class DocumentParser {
|
|||
parseArray(context, mapper, currentFieldName);
|
||||
} else if (token == XContentParser.Token.FIELD_NAME) {
|
||||
currentFieldName = parser.currentName();
|
||||
if (MapperService.isMetadataField(context.path().pathAsText(currentFieldName))) {
|
||||
throw new MapperParsingException("Field [" + currentFieldName + "] is a metadata field and cannot be added inside a document. Use the index API request parameters.");
|
||||
}
|
||||
} else if (token == XContentParser.Token.VALUE_NULL) {
|
||||
parseNullValue(context, mapper, currentFieldName);
|
||||
} else if (token == null) {
|
||||
|
|
|
@ -862,4 +862,16 @@ public class DocumentParserTests extends ESSingleNodeTestCase {
|
|||
() -> mapper.parse("test", "type", "1", bytes));
|
||||
assertEquals("mapping set to strict, dynamic introduction of [foo] within [type] is not allowed", exception.getMessage());
|
||||
}
|
||||
|
||||
public void testDocumentContainsMetadataField() throws Exception {
|
||||
DocumentMapperParser mapperParser = createIndex("test").mapperService().documentMapperParser();
|
||||
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
|
||||
DocumentMapper mapper = mapperParser.parse("type", new CompressedXContent(mapping));
|
||||
|
||||
BytesReference bytes = XContentFactory.jsonBuilder().startObject().field("_ttl", 0).endObject().bytes();
|
||||
MapperParsingException e = expectThrows(MapperParsingException.class, () ->
|
||||
mapper.parse("test", "type", "1", bytes)
|
||||
);
|
||||
assertTrue(e.getMessage(), e.getMessage().contains("cannot be added inside a document"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue