Skip to content

Commit

Permalink
Improve error msg when a field name contains only white spaces (#27709)
Browse files Browse the repository at this point in the history
* Explicitly check if a field name contains only
white spaces

* "white spaces" changed to "whitespace"
  • Loading branch information
olcbean authored and dakrone committed Dec 8, 2017
1 parent b66a072 commit f50f99e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ private static String[] splitAndValidatePath(String fullFieldPath) {
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 + "]");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,23 @@ public void testDynamicFieldsStartingAndEndingWithDot() throws Exception {
}
}

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()
Expand Down

0 comments on commit f50f99e

Please sign in to comment.