From ded84c71c68d7042c91eac93e402bbeda3de0673 Mon Sep 17 00:00:00 2001 From: iverase Date: Thu, 28 May 2020 15:16:14 +0200 Subject: [PATCH 1/2] Catch InputCoercionException thrown by Jackson parser --- .../index/mapper/NumberFieldMapper.java | 3 +- .../index/mapper/LongIndexingDocTests.java | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 server/src/test/java/org/elasticsearch/index/mapper/LongIndexingDocTests.java diff --git a/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java index 9c55caece6447..34560a6587f20 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java @@ -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; @@ -1060,7 +1061,7 @@ protected void parseCreateField(ParseContext context) throws IOException { } 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; diff --git a/server/src/test/java/org/elasticsearch/index/mapper/LongIndexingDocTests.java b/server/src/test/java/org/elasticsearch/index/mapper/LongIndexingDocTests.java new file mode 100644 index 0000000000000..9b3d0998cde9a --- /dev/null +++ b/server/src/test/java/org/elasticsearch/index/mapper/LongIndexingDocTests.java @@ -0,0 +1,46 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.elasticsearch.index.mapper; + +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.xcontent.XContentFactory; +import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.rest.RestStatus; +import org.elasticsearch.test.ESSingleNodeTestCase; + +public class LongIndexingDocTests extends ESSingleNodeTestCase { + + 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("test"); + client().admin().indices().preparePutMapping("test").setSource(mapping, XContentType.JSON).get(); + String doc = "{\"number\" : 9223372036854775808}"; + IndexResponse response = client().index(new IndexRequest("test").source(doc, XContentType.JSON)).get(); + assertTrue(response.status() == RestStatus.CREATED); + } +} From 31af8976d5e3b445db289d366bd5c860b87b5c0f Mon Sep 17 00:00:00 2001 From: iverase Date: Thu, 28 May 2020 16:51:02 +0200 Subject: [PATCH 2/2] move test to NumberFieldMapperTests --- .../index/mapper/LongIndexingDocTests.java | 46 ------------------- .../index/mapper/NumberFieldMapperTests.java | 19 ++++++++ 2 files changed, 19 insertions(+), 46 deletions(-) delete mode 100644 server/src/test/java/org/elasticsearch/index/mapper/LongIndexingDocTests.java diff --git a/server/src/test/java/org/elasticsearch/index/mapper/LongIndexingDocTests.java b/server/src/test/java/org/elasticsearch/index/mapper/LongIndexingDocTests.java deleted file mode 100644 index 9b3d0998cde9a..0000000000000 --- a/server/src/test/java/org/elasticsearch/index/mapper/LongIndexingDocTests.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.elasticsearch.index.mapper; - -import org.elasticsearch.action.index.IndexRequest; -import org.elasticsearch.action.index.IndexResponse; -import org.elasticsearch.common.Strings; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.rest.RestStatus; -import org.elasticsearch.test.ESSingleNodeTestCase; - -public class LongIndexingDocTests extends ESSingleNodeTestCase { - - 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("test"); - client().admin().indices().preparePutMapping("test").setSource(mapping, XContentType.JSON).get(); - String doc = "{\"number\" : 9223372036854775808}"; - IndexResponse response = client().index(new IndexRequest("test").source(doc, XContentType.JSON)).get(); - assertTrue(response.status() == RestStatus.CREATED); - } -} diff --git a/server/src/test/java/org/elasticsearch/index/mapper/NumberFieldMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/NumberFieldMapperTests.java index e0984ded48835..bf8269a27cdde 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/NumberFieldMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/NumberFieldMapperTests.java @@ -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; @@ -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; @@ -453,6 +456,22 @@ 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").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", "1", content, XContentType.JSON)); }