Skip to content

Commit 7e85fc4

Browse files
Throw better exception on wrong dynamic_templates syntax (elastic#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 elastic#51486
1 parent d5bc6d6 commit 7e85fc4

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

server/src/main/java/org/elasticsearch/index/mapper/RootObjectMapper.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ protected boolean processField(RootObjectMapper.Builder builder, String fieldNam
168168
}
169169
]
170170
*/
171+
if ((fieldNode instanceof List) == false) {
172+
throw new MapperParsingException("Dynamic template syntax error. An array of named objects is expected.");
173+
}
171174
List<?> tmplNodes = (List<?>) fieldNode;
172175
List<DynamicTemplate> templates = new ArrayList<>();
173176
for (Object tmplNode : tmplNodes) {

server/src/test/java/org/elasticsearch/index/mapper/RootObjectMapperTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,19 @@ public void testIllegalFormatField() throws Exception {
185185
assertEquals("Invalid format: [[test_format]]: expected string value", e.getMessage());
186186
}
187187
}
188+
189+
public void testIllegalDynamicTemplates() throws Exception {
190+
String mapping = Strings.toString(XContentFactory.jsonBuilder()
191+
.startObject()
192+
.startObject("type")
193+
.startObject("dynamic_templates")
194+
.endObject()
195+
.endObject()
196+
.endObject());
197+
198+
DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();
199+
MapperParsingException e = expectThrows(MapperParsingException.class,
200+
() -> parser.parse("type", new CompressedXContent(mapping)));
201+
assertEquals("Dynamic template syntax error. An array of named objects is expected.", e.getMessage());
202+
}
188203
}

0 commit comments

Comments
 (0)