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:
parent
b391425da1
commit
dd90cf1bbb
|
@ -175,14 +175,21 @@ final class DocumentParser {
|
|||
}
|
||||
|
||||
private static String[] splitAndValidatePath(String fullFieldPath) {
|
||||
String[] parts = fullFieldPath.split("\\.");
|
||||
for (String part : parts) {
|
||||
if (Strings.hasText(part) == false) {
|
||||
throw new IllegalArgumentException(
|
||||
"object field starting or ending with a [.] makes object resolution ambiguous: [" + fullFieldPath + "]");
|
||||
if (fullFieldPath.contains(".")) {
|
||||
String[] parts = fullFieldPath.split("\\.");
|
||||
for (String part : parts) {
|
||||
if (Strings.hasText(part) == false) {
|
||||
throw new IllegalArgumentException(
|
||||
"object field starting or ending with a [.] makes object resolution ambiguous: [" + fullFieldPath + "]");
|
||||
}
|
||||
}
|
||||
return parts;
|
||||
} else {
|
||||
if (Strings.isEmpty(fullFieldPath)) {
|
||||
throw new IllegalArgumentException("field name cannot be an empty string");
|
||||
}
|
||||
return new String[] {fullFieldPath};
|
||||
}
|
||||
return parts;
|
||||
}
|
||||
|
||||
/** Creates a Mapping containing any dynamically added fields, or returns null if there were no dynamic mappings. */
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
package org.elasticsearch.index.mapper;
|
||||
|
||||
import org.elasticsearch.ExceptionsHelper;
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.cluster.metadata.IndexMetaData;
|
||||
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]"));
|
||||
}
|
||||
}
|
||||
|
||||
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"));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -250,6 +250,6 @@ public class IndexActionIT extends ESIntegTestCase {
|
|||
);
|
||||
assertThat(e.getMessage(), containsString("failed to parse"));
|
||||
assertThat(e.getRootCause().getMessage(),
|
||||
containsString("object field starting or ending with a [.] makes object resolution ambiguous: []"));
|
||||
containsString("field name cannot be an empty string"));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue