浏览代码

Improve error msg when a field name contains only white spaces (#27709)

* Explicitly check if a field name contains only
white spaces

* "white spaces" changed to "whitespace"
olcbean 7 年之前
父节点
当前提交
f50f99ef11

+ 5 - 0
core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java

@@ -180,6 +180,11 @@ final class DocumentParser {
             String[] parts = fullFieldPath.split("\\.");
             for (String part : parts) {
                 if (Strings.hasText(part) == false) {
+                    // check if the field name contains only whitespace
+                    if (Strings.isEmpty(part) == false) {
+                        throw new IllegalArgumentException(
+                                "object field cannot contain only whitespace: ['" + fullFieldPath + "']");
+                    }
                     throw new IllegalArgumentException(
                             "object field starting or ending with a [.] makes object resolution ambiguous: [" + fullFieldPath + "]");
                 }

+ 17 - 0
core/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java

@@ -1377,6 +1377,23 @@ public class DocumentParserTests extends ESSingleNodeTestCase {
         }
     }
 
+    public void testDynamicFieldsEmptyName() throws Exception {
+        BytesReference bytes = XContentFactory.jsonBuilder()
+                .startObject().startArray("top.")
+                .startObject()
+                .startObject("aoeu")
+                .field("a", 1).field(" ", 2)
+                .endObject()
+                .endObject().endArray()
+                .endObject().bytes();
+
+        IllegalArgumentException emptyFieldNameException = expectThrows(IllegalArgumentException.class,
+                () -> client().prepareIndex("idx", "type").setSource(bytes, XContentType.JSON).get());
+
+        assertThat(emptyFieldNameException.getMessage(), containsString(
+                "object field cannot contain only whitespace: ['top.aoeu. ']"));
+    }
+
     public void testBlankFieldNames() throws Exception {
         final BytesReference bytes = XContentFactory.jsonBuilder()
                 .startObject()