Skip to content

Commit

Permalink
[7.14] Make sure copy_to accepts null values (#76768)
Browse files Browse the repository at this point in the history
In #72820 we improved the error message when attempting to `copy_to` with an
object value. The change accidentally started throwing errors for null values
too, although they're allowed. This PR fixes the regression and adds an
explicit test for `copy_to` with null values.
  • Loading branch information
jtibshirani committed Aug 20, 2021
1 parent dab9469 commit b90ed56
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ static void parseObjectOrField(ParseContext context, Mapper mapper) throws IOExc
List<String> copyToFields = fieldMapper.copyTo().copyToFields();
if (context.isWithinCopyTo() == false && copyToFields.isEmpty() == false) {
XContentParser.Token currentToken = context.parser().currentToken();
if (currentToken.isValue() == false) {
if (currentToken.isValue() == false && currentToken != XContentParser.Token.VALUE_NULL) {
// sanity check, we currently support copy-to only for value-type field, not objects
throw new MapperParsingException("Cannot copy field [" + mapper.name() + "] to fields " + copyToFields +
". Copy-to currently only works for value-type fields, not objects.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.hamcrest.Matchers;

Expand Down Expand Up @@ -644,6 +645,31 @@ public void testCopyToDateRangeFailure() throws Exception {
);
}

public void testCopyToWithNullValue() throws Exception {
DocumentMapper docMapper = createDocumentMapper(topMapping(b ->
b.startObject("properties")
.startObject("keyword_copy")
.field("type", "keyword")
.field("null_value", "default-value")
.endObject()
.startObject("keyword")
.field("type", "keyword")
.array("copy_to", "keyword_copy")
.endObject()
.endObject()));

BytesReference json = BytesReference.bytes(jsonBuilder().startObject()
.nullField("keyword")
.endObject());

LuceneDocument document = docMapper.parse(new SourceToParse("test", MapperService.SINGLE_MAPPING_NAME,
"1", json, XContentType.JSON)).rootDoc();
assertEquals(0, document.getFields("keyword").length);

IndexableField[] fields = document.getFields("keyword_copy");
assertEquals(2, fields.length);
}

public void testCopyToGeoPoint() throws Exception {
DocumentMapper docMapper = createDocumentMapper(topMapping(b -> {
b.startObject("properties");
Expand Down

0 comments on commit b90ed56

Please sign in to comment.