Skip to content

Commit

Permalink
Revert "Validate field names when subobjects are disabled (#90950)" (#…
Browse files Browse the repository at this point in the history
…91096)

This reverts commit 9147be6.

In hindsight, there is a slight possibility that existing users are relying on Elasticsearch accepting field names that contain only dots when subobjects are disabled. The original change assumed that such fields were not accepted in mappings, but they actually are, as #90950 has surfaced. As a result, we end up rejecting field names that the mappings would happily accept, which is inconsistent.
  • Loading branch information
javanna committed Nov 4, 2022
1 parent 396f062 commit fd1aadf
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,6 @@ private static void innerParseObject(DocumentParserContext context, ObjectMapper
if (currentFieldName.isBlank()) {
throwFieldNameBlank(context, currentFieldName);
}
if (currentFieldName.replace(".", "").length() == 0) {
throwFieldNameOnlyDots();
}
break;
case START_OBJECT:
parseObject(context, mapper, currentFieldName);
Expand All @@ -342,10 +339,6 @@ private static void throwFieldNameBlank(DocumentParserContext context, String cu
);
}

private static void throwFieldNameOnlyDots() {
throw new IllegalArgumentException("field name cannot contain only dots");
}

private static void throwEOF(ObjectMapper mapper, DocumentParserContext context) throws IOException {
throw new MapperParsingException(
"object mapping for ["
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ private void expandDots() throws IOException {
XContentParser delegate = delegate();
String field = delegate.currentName();
String[] subpaths = splitAndValidatePath(field);
if (subpaths.length == 0) {
throw new IllegalArgumentException("field name cannot contain only dots: [" + field + "]");
}
// Corner case: if the input has a single trailing '.', eg 'field.', then we will get a single
// subpath due to the way String.split() works. We can only return fast here if this is not
// the case
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1847,27 +1847,8 @@ public void testBlankFieldNames() throws Exception {
assertThat(err.getCause().getMessage(), containsString("field name cannot be an empty string"));
}

public void testBlankFieldNamesSubobjectsFalse() throws Exception {
DocumentMapper mapper = createDocumentMapper(topMapping(b -> b.field("subobjects", false)));
{
MapperParsingException err = expectThrows(MapperParsingException.class, () -> mapper.parse(source(b -> b.field("", "foo"))));
assertThat(err.getMessage(), containsString("Field name cannot contain only whitespace: []"));
}
{
MapperParsingException err = expectThrows(MapperParsingException.class, () -> mapper.parse(source(b -> b.field(" ", "foo"))));
assertThat(err.getMessage(), containsString("Field name cannot contain only whitespace: [ ]"));
}
}

public void testDotsOnlyFieldNames() throws Exception {
dotsOnlyFieldNames(createDocumentMapper(mapping(b -> {})));
}

public void testDotsOnlyFieldNamesSubobjectsFalse() throws Exception {
dotsOnlyFieldNames(createDocumentMapper(topMapping(b -> b.field("subobjects", false))));
}

private void dotsOnlyFieldNames(DocumentMapper mapper) {
DocumentMapper mapper = createDocumentMapper(mapping(b -> {}));
MapperParsingException err = expectThrows(
MapperParsingException.class,
() -> mapper.parse(source(b -> b.field(randomFrom(".", "..", "..."), "bar")))
Expand Down

0 comments on commit fd1aadf

Please sign in to comment.