From 2bef4ebda8185302d5ee42c193ea73b7a59323c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Mon, 29 Mar 2021 12:35:24 +0200 Subject: [PATCH] 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 --- .../org/elasticsearch/index/mapper/DocumentParser.java | 3 +++ .../index/mapper/DocumentParserTests.java | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/server/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java b/server/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java index 296bdc4410143..a10c2e09849f6 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/DocumentParser.java @@ -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 diff --git a/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java b/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java index fbd9448aa2d5f..249c942142499 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/DocumentParserTests.java @@ -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");