Skip to content

Commit

Permalink
Catch InputCoercionException thrown by Jackson parser (elastic#57287) (
Browse files Browse the repository at this point in the history
…elastic#57330)

Jackson 2.10 library has added a new type of error that is thrown when a numeric value is out 
of range. This error should be catch and handle properly in case the flag ignore_malformed 
has been set to true.
  • Loading branch information
iverase committed May 29, 2020
1 parent 2405676 commit aa59355
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.fasterxml.jackson.core.JsonParseException;

import com.fasterxml.jackson.core.exc.InputCoercionException;
import org.apache.lucene.document.DoublePoint;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FloatPoint;
Expand Down Expand Up @@ -1048,7 +1049,7 @@ protected void parseCreateField(ParseContext context, List<IndexableField> field
} else {
try {
numericValue = fieldType().type.parse(parser, coerce.value());
} catch (IllegalArgumentException | JsonParseException e) {
} catch (InputCoercionException | IllegalArgumentException | JsonParseException e) {
if (ignoreMalformed.value() && parser.currentToken().isValue()) {
context.addIgnoredField(fieldType.name());
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.carrotsearch.randomizedtesting.annotations.Timeout;
import org.apache.lucene.index.DocValuesType;
import org.apache.lucene.index.IndexableField;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.compress.CompressedXContent;
Expand All @@ -32,6 +34,7 @@
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType;
import org.elasticsearch.index.mapper.NumberFieldTypeTests.OutOfRangeSpec;
import org.elasticsearch.index.termvectors.TermVectorsService;
import org.elasticsearch.rest.RestStatus;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -453,6 +456,23 @@ public void testOutOfRangeValues() throws IOException {
parseRequest(NumberType.LONG, createIndexRequest("-9223372036854775808.9"));
}

public void testLongIndexingOutOfRange() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder()
.startObject().startObject("_doc")
.startObject("properties")
.startObject("number")
.field("type", "long")
.field("ignore_malformed", true)
.endObject().endObject()
.endObject().endObject());
createIndex("test57287");
client().admin().indices().preparePutMapping("test57287")
.setType("_doc").setSource(mapping, XContentType.JSON).get();
String doc = "{\"number\" : 9223372036854775808}";
IndexResponse response = client().index(new IndexRequest("test57287").source(doc, XContentType.JSON)).get();
assertTrue(response.status() == RestStatus.CREATED);
}

private void parseRequest(NumberType type, BytesReference content) throws IOException {
createDocumentMapper(type).parse(new SourceToParse("test", "type", "1", content, XContentType.JSON));
}
Expand Down

0 comments on commit aa59355

Please sign in to comment.