Skip to content

Commit 337153b

Browse files
feifeiiiiiiiiiiiChristoph Büscher
authored and
Christoph Büscher
committed
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
1 parent b7aace4 commit 337153b

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
@@ -163,6 +163,9 @@ protected boolean processField(RootObjectMapper.Builder builder, String fieldNam
163163
// }
164164
// }
165165
// ]
166+
if ((fieldNode instanceof List) == false) {
167+
throw new MapperParsingException("Dynamic template syntax error. An array of named objects is expected.");
168+
}
166169
List<?> tmplNodes = (List<?>) fieldNode;
167170
List<DynamicTemplate> templates = new ArrayList<>();
168171
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)