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"
This commit is contained in:
olcbean 2017-12-08 21:46:56 +01:00 committed by Lee Hinman
parent b66a0721da
commit f50f99ef11
2 changed files with 22 additions and 0 deletions

View File

@ -180,6 +180,11 @@ final class DocumentParser {
String[] parts = fullFieldPath.split("\\."); String[] parts = fullFieldPath.split("\\.");
for (String part : parts) { for (String part : parts) {
if (Strings.hasText(part) == false) { 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( throw new IllegalArgumentException(
"object field starting or ending with a [.] makes object resolution ambiguous: [" + fullFieldPath + "]"); "object field starting or ending with a [.] makes object resolution ambiguous: [" + fullFieldPath + "]");
} }

View File

@ -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 { public void testBlankFieldNames() throws Exception {
final BytesReference bytes = XContentFactory.jsonBuilder() final BytesReference bytes = XContentFactory.jsonBuilder()
.startObject() .startObject()