Throw a better error message for empty field names (#26543)

* Throw a better error message for empty field names

When a document is parsed with a `""` for a field name, we currently throw a
confusing error about `.` being present in the field. This changes the error
message to be clearer about what's causing the problem.

Resolves #23348

* Fix exception message in test
This commit is contained in:
Lee Hinman 2017-09-08 13:30:17 -06:00 committed by GitHub
parent b391425da1
commit dd90cf1bbb
3 changed files with 37 additions and 7 deletions

View File

@ -175,6 +175,7 @@ final class DocumentParser {
} }
private static String[] splitAndValidatePath(String fullFieldPath) { private static String[] splitAndValidatePath(String fullFieldPath) {
if (fullFieldPath.contains(".")) {
String[] parts = fullFieldPath.split("\\."); String[] parts = fullFieldPath.split("\\.");
for (String part : parts) { for (String part : parts) {
if (Strings.hasText(part) == false) { if (Strings.hasText(part) == false) {
@ -183,6 +184,12 @@ final class DocumentParser {
} }
} }
return parts; return parts;
} else {
if (Strings.isEmpty(fullFieldPath)) {
throw new IllegalArgumentException("field name cannot be an empty string");
}
return new String[] {fullFieldPath};
}
} }
/** Creates a Mapping containing any dynamically added fields, or returns null if there were no dynamic mappings. */ /** Creates a Mapping containing any dynamically added fields, or returns null if there were no dynamic mappings. */

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.mapper; package org.elasticsearch.index.mapper;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.Version; import org.elasticsearch.Version;
import org.elasticsearch.cluster.metadata.IndexMetaData; import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesArray;
@ -1375,4 +1376,26 @@ public class DocumentParserTests extends ESSingleNodeTestCase {
containsString("object field starting or ending with a [.] makes object resolution ambiguous: [top..foo..bar]")); containsString("object field starting or ending with a [.] makes object resolution ambiguous: [top..foo..bar]"));
} }
} }
public void testBlankFieldNames() throws Exception {
final BytesReference bytes = XContentFactory.jsonBuilder()
.startObject()
.field("", "foo")
.endObject().bytes();
MapperParsingException err = expectThrows(MapperParsingException.class, () ->
client().prepareIndex("idx", "type").setSource(bytes, XContentType.JSON).get());
assertThat(ExceptionsHelper.detailedMessage(err), containsString("field name cannot be an empty string"));
final BytesReference bytes2 = XContentFactory.jsonBuilder()
.startObject()
.startObject("foo")
.field("", "bar")
.endObject()
.endObject().bytes();
err = expectThrows(MapperParsingException.class, () ->
client().prepareIndex("idx", "type").setSource(bytes2, XContentType.JSON).get());
assertThat(ExceptionsHelper.detailedMessage(err), containsString("field name cannot be an empty string"));
}
} }

View File

@ -250,6 +250,6 @@ public class IndexActionIT extends ESIntegTestCase {
); );
assertThat(e.getMessage(), containsString("failed to parse")); assertThat(e.getMessage(), containsString("failed to parse"));
assertThat(e.getRootCause().getMessage(), assertThat(e.getRootCause().getMessage(),
containsString("object field starting or ending with a [.] makes object resolution ambiguous: []")); containsString("field name cannot be an empty string"));
} }
} }