Skip to content

Commit

Permalink
Improve error message for invalid field name (#70972)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Christoph Büscher authored Mar 29, 2021
1 parent 378f248 commit 2bef4eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ private static MapperParsingException wrapInMapperParsingException(SourceToParse
private static String[] splitAndValidatePath(String fullFieldPath) {
if (fullFieldPath.contains(".")) {
String[] parts = fullFieldPath.split("\\.");
if (parts.length == 0) {
throw new IllegalArgumentException("field name cannot contain only dots");
}
for (String part : parts) {
if (Strings.hasText(part) == false) {
// check if the field name contains only whitespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1656,6 +1656,16 @@ public void testBlankFieldNames() throws Exception {
assertThat(err.getCause().getMessage(), containsString("field name cannot be an empty string"));
}

public void testDotsOnlyFieldNames() throws Exception {
DocumentMapper mapper = createDocumentMapper(mapping(b -> {}));
MapperParsingException err = expectThrows(
MapperParsingException.class,
() -> mapper.parse(source(b -> b.field(randomFrom(".", "..", "..."), "bar")))
);
assertThat(err.getCause(), notNullValue());
assertThat(err.getCause().getMessage(), containsString("field name cannot contain only dots"));
}

public void testWriteToFieldAlias() throws Exception {
DocumentMapper mapper = createDocumentMapper(mapping(b -> {
b.startObject("alias-field");
Expand Down

0 comments on commit 2bef4eb

Please sign in to comment.