Skip to content

Commit 2bef4eb

Browse files
author
Christoph Büscher
authored
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 378f248 commit 2bef4eb

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
@@ -173,6 +173,9 @@ private static MapperParsingException wrapInMapperParsingException(SourceToParse
173173
private static String[] splitAndValidatePath(String fullFieldPath) {
174174
if (fullFieldPath.contains(".")) {
175175
String[] parts = fullFieldPath.split("\\.");
176+
if (parts.length == 0) {
177+
throw new IllegalArgumentException("field name cannot contain only dots");
178+
}
176179
for (String part : parts) {
177180
if (Strings.hasText(part) == false) {
178181
// 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
@@ -1656,6 +1656,16 @@ public void testBlankFieldNames() throws Exception {
16561656
assertThat(err.getCause().getMessage(), containsString("field name cannot be an empty string"));
16571657
}
16581658

1659+
public void testDotsOnlyFieldNames() throws Exception {
1660+
DocumentMapper mapper = createDocumentMapper(mapping(b -> {}));
1661+
MapperParsingException err = expectThrows(
1662+
MapperParsingException.class,
1663+
() -> mapper.parse(source(b -> b.field(randomFrom(".", "..", "..."), "bar")))
1664+
);
1665+
assertThat(err.getCause(), notNullValue());
1666+
assertThat(err.getCause().getMessage(), containsString("field name cannot contain only dots"));
1667+
}
1668+
16591669
public void testWriteToFieldAlias() throws Exception {
16601670
DocumentMapper mapper = createDocumentMapper(mapping(b -> {
16611671
b.startObject("alias-field");

0 commit comments

Comments
 (0)