Throw better exception on wrong `dynamic_templates` syntax (#51783)

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
This commit is contained in:
feifeiiiiiiiiii 2020-02-04 23:58:53 +08:00 committed by Christoph Büscher
parent b7aace44f3
commit 337153b29f
2 changed files with 18 additions and 0 deletions

View File

@ -163,6 +163,9 @@ public class RootObjectMapper extends ObjectMapper {
// }
// }
// ]
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) {

View File

@ -185,4 +185,19 @@ public class RootObjectMapperTests extends ESSingleNodeTestCase {
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());
}
}