Skip to content

Commit 3c27dc1

Browse files
author
Christoph Büscher
committed
Improve error message for invalid field name (#70972)
The current error when a field name consists only of path separator dots is not very user friendly. Instead of throwing an array_index_out_of_bounds_exception we can detect this case and throw a more useful IAE. Closes #70960
1 parent 8459ccd commit 3c27dc1

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ private static MapperParsingException wrapInMapperParsingException(SourceToParse
186186
private static String[] splitAndValidatePath(String fullFieldPath) {
187187
if (fullFieldPath.contains(".")) {
188188
String[] parts = fullFieldPath.split("\\.");
189+
if (parts.length == 0) {
190+
throw new IllegalArgumentException("field name cannot contain only dots");
191+
}
189192
for (String part : parts) {
190193
if (Strings.hasText(part) == false) {
191194
// check if the field name contains only whitespace

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,6 +1674,16 @@ public void testBlankFieldNames() throws Exception {
16741674
assertThat(err.getCause().getMessage(), containsString("field name cannot be an empty string"));
16751675
}
16761676

1677+
public void testDotsOnlyFieldNames() throws Exception {
1678+
DocumentMapper mapper = createDocumentMapper(mapping(b -> {}));
1679+
MapperParsingException err = expectThrows(
1680+
MapperParsingException.class,
1681+
() -> mapper.parse(source(b -> b.field(randomFrom(".", "..", "..."), "bar")))
1682+
);
1683+
assertThat(err.getCause(), notNullValue());
1684+
assertThat(err.getCause().getMessage(), containsString("field name cannot contain only dots"));
1685+
}
1686+
16771687
public void testWriteToFieldAlias() throws Exception {
16781688
DocumentMapper mapper = createDocumentMapper(mapping(b -> {
16791689
b.startObject("alias-field");

0 commit comments

Comments
 (0)