Skip to content

Commit

Permalink
Modifying metadata checking in document parser to use mapperRegistry …
Browse files Browse the repository at this point in the history
…and fix issue elastic#24422
  • Loading branch information
asettouf committed May 3, 2017
1 parent db16038 commit af79926
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 2 deletions.
Expand Up @@ -376,14 +376,15 @@ static void parseObjectOrNested(ParseContext context, ObjectMapper mapper, boole
}

private static void innerParseObject(ParseContext context, ObjectMapper mapper, XContentParser parser, String currentFieldName, XContentParser.Token token) throws IOException {
MapperService mapperService = context.docMapperParser().mapperService;
while (token != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.START_OBJECT) {
parseObject(context, mapper, currentFieldName);
} else if (token == XContentParser.Token.START_ARRAY) {
parseArray(context, mapper, currentFieldName);
} else if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
if (MapperService.isMetadataField(context.path().pathAsText(currentFieldName))) {
if (mapperService.isMetaDataFieldInMetaDataRegistry(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) {
Expand Down
Expand Up @@ -769,6 +769,10 @@ public void close() throws IOException {
indexAnalyzers.close();
}

public boolean isMetaDataFieldInMetaDataRegistry(String fieldName){
return mapperRegistry.getMetadataMapperParsers().containsKey(fieldName);
}

/**
* @return Whether a field is a metadata field.
*/
Expand Down
Expand Up @@ -941,7 +941,7 @@ public void testDocumentContainsMetadataField() throws Exception {
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();
BytesReference bytes = XContentFactory.jsonBuilder().startObject().field("_index", 0).endObject().bytes();
MapperParsingException e = expectThrows(MapperParsingException.class, () ->
mapper.parse(SourceToParse.source("test", "type", "1", bytes, XContentType.JSON)));
assertTrue(e.getMessage(), e.getMessage().contains("cannot be added inside a document"));
Expand Down
Expand Up @@ -321,4 +321,10 @@ public void testForbidMultipleTypes() throws IOException {
() -> mapperService.merge("type2", new CompressedXContent(mapping2), MergeReason.MAPPING_UPDATE, randomBoolean()));
assertThat(e.getMessage(), Matchers.startsWith("Rejecting mapping update to [test] as the final mapping would have more than 1 type: "));
}

public void testFieldIsMetadata(){
MapperService mapperService = createIndex("test").mapperService();
String field = "_field_names";
assertTrue("'_field_names' not recognized as a meta data field", mapperService.isMetaDataFieldInMetaDataRegistry(field));
}
}

0 comments on commit af79926

Please sign in to comment.