Skip to content

Commit 99871b1

Browse files
authored
Catch InputCoercionException thrown by Jackson parser (#57287)
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.
1 parent 0b041cc commit 99871b1

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.fasterxml.jackson.core.JsonParseException;
2323

24+
import com.fasterxml.jackson.core.exc.InputCoercionException;
2425
import org.apache.lucene.document.DoublePoint;
2526
import org.apache.lucene.document.Field;
2627
import org.apache.lucene.document.FloatPoint;
@@ -1060,7 +1061,7 @@ protected void parseCreateField(ParseContext context) throws IOException {
10601061
} else {
10611062
try {
10621063
numericValue = fieldType().type.parse(parser, coerce.value());
1063-
} catch (IllegalArgumentException | JsonParseException e) {
1064+
} catch (InputCoercionException | IllegalArgumentException | JsonParseException e) {
10641065
if (ignoreMalformed.value() && parser.currentToken().isValue()) {
10651066
context.addIgnoredField(fieldType.name());
10661067
return;

server/src/test/java/org/elasticsearch/index/mapper/NumberFieldMapperTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.carrotsearch.randomizedtesting.annotations.Timeout;
2323
import org.apache.lucene.index.DocValuesType;
2424
import org.apache.lucene.index.IndexableField;
25+
import org.elasticsearch.action.index.IndexRequest;
26+
import org.elasticsearch.action.index.IndexResponse;
2527
import org.elasticsearch.common.Strings;
2628
import org.elasticsearch.common.bytes.BytesReference;
2729
import org.elasticsearch.common.compress.CompressedXContent;
@@ -32,6 +34,7 @@
3234
import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType;
3335
import org.elasticsearch.index.mapper.NumberFieldTypeTests.OutOfRangeSpec;
3436
import org.elasticsearch.index.termvectors.TermVectorsService;
37+
import org.elasticsearch.rest.RestStatus;
3538

3639
import java.io.ByteArrayInputStream;
3740
import java.io.IOException;
@@ -453,6 +456,22 @@ public void testOutOfRangeValues() throws IOException {
453456
parseRequest(NumberType.LONG, createIndexRequest("-9223372036854775808.9"));
454457
}
455458

459+
public void testLongIndexingOutOfRange() throws Exception {
460+
String mapping = Strings.toString(XContentFactory.jsonBuilder()
461+
.startObject().startObject("_doc")
462+
.startObject("properties")
463+
.startObject("number")
464+
.field("type", "long")
465+
.field("ignore_malformed", true)
466+
.endObject().endObject()
467+
.endObject().endObject());
468+
createIndex("test57287");
469+
client().admin().indices().preparePutMapping("test57287").setSource(mapping, XContentType.JSON).get();
470+
String doc = "{\"number\" : 9223372036854775808}";
471+
IndexResponse response = client().index(new IndexRequest("test57287").source(doc, XContentType.JSON)).get();
472+
assertTrue(response.status() == RestStatus.CREATED);
473+
}
474+
456475
private void parseRequest(NumberType type, BytesReference content) throws IOException {
457476
createDocumentMapper(type).parse(new SourceToParse("test", "1", content, XContentType.JSON));
458477
}

0 commit comments

Comments
 (0)