Browse Source

Improve doc parsing errors for badly formed metadata fields (#80696)

We currently throw a message that always refers to the `_parent` field,
even though it could be throw from any metadata field, and indeed the
`_parent` field hasn't existed for several versions now.
weizijun 3 years ago
parent
commit
3ca033f8d4

+ 1 - 1
server/src/main/java/org/elasticsearch/index/mapper/MappingParser.java

@@ -112,7 +112,7 @@ public final class MappingParser {
             if (typeParser != null) {
                 iterator.remove();
                 if (false == fieldNode instanceof Map) {
-                    throw new IllegalArgumentException("[_parent] must be an object containing [type]");
+                    throw new IllegalArgumentException("[" + fieldName + "] config must be an object");
                 }
                 @SuppressWarnings("unchecked")
                 Map<String, Object> fieldNodeMap = (Map<String, Object>) fieldNode;

+ 9 - 0
server/src/test/java/org/elasticsearch/index/mapper/MappingParserTests.java

@@ -133,4 +133,13 @@ public class MappingParserTests extends MapperServiceTestCase {
         );
         assertEquals("Type [alias] cannot be used in multi field", e.getMessage());
     }
+
+    public void testBadMetadataMapper() throws IOException {
+        XContentBuilder builder = topMapping(b -> { b.field(RoutingFieldMapper.NAME, "required"); });
+        IllegalArgumentException e = expectThrows(
+            IllegalArgumentException.class,
+            () -> createMappingParser(Settings.EMPTY).parse("_doc", new CompressedXContent(BytesReference.bytes(builder)))
+        );
+        assertEquals("[_routing] config must be an object", e.getMessage());
+    }
 }