Skip to content

Commit

Permalink
Throw better exception on wrong dynamic_templates syntax (#51783)
Browse files Browse the repository at this point in the history
Currently, a mappings update request, where dynamic_mappings is an object
instead of an array, results in a http response with a 500 code. This PR checks
for this condition and throws a MapperParsingException like we do for other
malformed mapping cases.

Closes #51486
  • Loading branch information
feifeiiiiiiiiiii authored and Christoph Büscher committed Feb 4, 2020
1 parent b7aace4 commit 337153b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ protected boolean processField(RootObjectMapper.Builder builder, String fieldNam
// }
// }
// ]
if ((fieldNode instanceof List) == false) {
throw new MapperParsingException("Dynamic template syntax error. An array of named objects is expected.");
}
List<?> tmplNodes = (List<?>) fieldNode;
List<DynamicTemplate> templates = new ArrayList<>();
for (Object tmplNode : tmplNodes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,19 @@ public void testIllegalFormatField() throws Exception {
assertEquals("Invalid format: [[test_format]]: expected string value", e.getMessage());
}
}

public void testIllegalDynamicTemplates() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder()
.startObject()
.startObject("type")
.startObject("dynamic_templates")
.endObject()
.endObject()
.endObject());

DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();
MapperParsingException e = expectThrows(MapperParsingException.class,
() -> parser.parse("type", new CompressedXContent(mapping)));
assertEquals("Dynamic template syntax error. An array of named objects is expected.", e.getMessage());
}
}

0 comments on commit 337153b

Please sign in to comment.