Skip to content

Commit f50f99e

Browse files
olcbeandakrone
authored andcommitted
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"
1 parent b66a072 commit f50f99e

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

core/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ private static String[] splitAndValidatePath(String fullFieldPath) {
180180
String[] parts = fullFieldPath.split("\\.");
181181
for (String part : parts) {
182182
if (Strings.hasText(part) == false) {
183+
// check if the field name contains only whitespace
184+
if (Strings.isEmpty(part) == false) {
185+
throw new IllegalArgumentException(
186+
"object field cannot contain only whitespace: ['" + fullFieldPath + "']");
187+
}
183188
throw new IllegalArgumentException(
184189
"object field starting or ending with a [.] makes object resolution ambiguous: [" + fullFieldPath + "]");
185190
}

core/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,6 +1377,23 @@ public void testDynamicFieldsStartingAndEndingWithDot() throws Exception {
13771377
}
13781378
}
13791379

1380+
public void testDynamicFieldsEmptyName() throws Exception {
1381+
BytesReference bytes = XContentFactory.jsonBuilder()
1382+
.startObject().startArray("top.")
1383+
.startObject()
1384+
.startObject("aoeu")
1385+
.field("a", 1).field(" ", 2)
1386+
.endObject()
1387+
.endObject().endArray()
1388+
.endObject().bytes();
1389+
1390+
IllegalArgumentException emptyFieldNameException = expectThrows(IllegalArgumentException.class,
1391+
() -> client().prepareIndex("idx", "type").setSource(bytes, XContentType.JSON).get());
1392+
1393+
assertThat(emptyFieldNameException.getMessage(), containsString(
1394+
"object field cannot contain only whitespace: ['top.aoeu. ']"));
1395+
}
1396+
13801397
public void testBlankFieldNames() throws Exception {
13811398
final BytesReference bytes = XContentFactory.jsonBuilder()
13821399
.startObject()

0 commit comments

Comments
 (0)