|
20 | 20 | package org.elasticsearch.index.mapper; |
21 | 21 |
|
22 | 22 | import com.carrotsearch.randomizedtesting.annotations.Timeout; |
| 23 | + |
23 | 24 | import org.apache.lucene.index.DocValuesType; |
24 | 25 | import org.apache.lucene.index.IndexableField; |
25 | 26 | import org.elasticsearch.common.Strings; |
26 | 27 | import org.elasticsearch.common.bytes.BytesReference; |
27 | 28 | import org.elasticsearch.common.compress.CompressedXContent; |
| 29 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 30 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
28 | 31 | import org.elasticsearch.common.xcontent.XContentFactory; |
29 | 32 | import org.elasticsearch.common.xcontent.XContentType; |
30 | 33 | import org.elasticsearch.index.mapper.NumberFieldMapper.NumberType; |
|
37 | 40 | import java.util.HashSet; |
38 | 41 | import java.util.List; |
39 | 42 |
|
| 43 | +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
40 | 44 | import static org.hamcrest.Matchers.containsString; |
41 | 45 |
|
42 | 46 | public class NumberFieldMapperTests extends AbstractNumericFieldMapperTestCase { |
@@ -218,45 +222,65 @@ protected void doTestDecimalCoerce(String type) throws IOException { |
218 | 222 |
|
219 | 223 | public void testIgnoreMalformed() throws Exception { |
220 | 224 | for (String type : TYPES) { |
221 | | - doTestIgnoreMalformed(type); |
222 | | - } |
223 | | - } |
| 225 | + for (Object malformedValue : new Object[] { "a", Boolean.FALSE }) { |
| 226 | + String mapping = Strings.toString(jsonBuilder().startObject().startObject("type").startObject("properties") |
| 227 | + .startObject("field").field("type", type).endObject().endObject().endObject().endObject()); |
224 | 228 |
|
225 | | - private void doTestIgnoreMalformed(String type) throws IOException { |
226 | | - String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") |
227 | | - .startObject("properties").startObject("field").field("type", type).endObject().endObject() |
228 | | - .endObject().endObject()); |
| 229 | + DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); |
229 | 230 |
|
230 | | - DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); |
| 231 | + assertEquals(mapping, mapper.mappingSource().toString()); |
231 | 232 |
|
232 | | - assertEquals(mapping, mapper.mappingSource().toString()); |
| 233 | + ThrowingRunnable runnable = () -> mapper.parse(new SourceToParse("test", "type", "1", |
| 234 | + BytesReference.bytes(jsonBuilder().startObject().field("field", malformedValue).endObject()), XContentType.JSON)); |
| 235 | + MapperParsingException e = expectThrows(MapperParsingException.class, runnable); |
| 236 | + if (malformedValue instanceof String) { |
| 237 | + assertThat(e.getCause().getMessage(), containsString("For input string: \"a\"")); |
| 238 | + } else { |
| 239 | + assertThat(e.getCause().getMessage(), containsString("Current token")); |
| 240 | + assertThat(e.getCause().getMessage(), containsString("not numeric, can not use numeric value accessors")); |
| 241 | + } |
233 | 242 |
|
234 | | - ThrowingRunnable runnable = () -> mapper.parse(new SourceToParse("test", "type", "1", BytesReference |
235 | | - .bytes(XContentFactory.jsonBuilder() |
236 | | - .startObject() |
237 | | - .field("field", "a") |
238 | | - .endObject()), |
239 | | - XContentType.JSON)); |
240 | | - MapperParsingException e = expectThrows(MapperParsingException.class, runnable); |
241 | | - |
242 | | - assertThat(e.getCause().getMessage(), containsString("For input string: \"a\"")); |
| 243 | + mapping = Strings.toString(jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field") |
| 244 | + .field("type", type).field("ignore_malformed", true).endObject().endObject().endObject().endObject()); |
243 | 245 |
|
244 | | - mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type") |
245 | | - .startObject("properties").startObject("field").field("type", type).field("ignore_malformed", true).endObject().endObject() |
246 | | - .endObject().endObject()); |
| 246 | + DocumentMapper mapper2 = parser.parse("type", new CompressedXContent(mapping)); |
247 | 247 |
|
248 | | - DocumentMapper mapper2 = parser.parse("type", new CompressedXContent(mapping)); |
| 248 | + ParsedDocument doc = mapper2.parse(new SourceToParse("test", "type", "1", |
| 249 | + BytesReference.bytes(jsonBuilder().startObject().field("field", malformedValue).endObject()), XContentType.JSON)); |
249 | 250 |
|
250 | | - ParsedDocument doc = mapper2.parse(new SourceToParse("test", "type", "1", BytesReference |
251 | | - .bytes(XContentFactory.jsonBuilder() |
252 | | - .startObject() |
253 | | - .field("field", "a") |
254 | | - .endObject()), |
255 | | - XContentType.JSON)); |
| 251 | + IndexableField[] fields = doc.rootDoc().getFields("field"); |
| 252 | + assertEquals(0, fields.length); |
| 253 | + assertArrayEquals(new String[] { "field" }, doc.rootDoc().getValues("_ignored")); |
| 254 | + } |
| 255 | + } |
| 256 | + } |
256 | 257 |
|
257 | | - IndexableField[] fields = doc.rootDoc().getFields("field"); |
258 | | - assertEquals(0, fields.length); |
259 | | - assertArrayEquals(new String[] { "field" }, doc.rootDoc().getValues("_ignored")); |
| 258 | + /** |
| 259 | + * Test that in case the malformed value is an xContent object we throw error regardless of `ignore_malformed` |
| 260 | + */ |
| 261 | + public void testIgnoreMalformedWithObject() throws Exception { |
| 262 | + for (String type : TYPES) { |
| 263 | + Object malformedValue = new ToXContentObject() { |
| 264 | + @Override |
| 265 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 266 | + return builder.startObject().field("foo", "bar").endObject(); |
| 267 | + } |
| 268 | + }; |
| 269 | + for (Boolean ignoreMalformed : new Boolean[] { true, false }) { |
| 270 | + String mapping = Strings.toString( |
| 271 | + jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", type) |
| 272 | + .field("ignore_malformed", ignoreMalformed).endObject().endObject().endObject().endObject()); |
| 273 | + DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); |
| 274 | + assertEquals(mapping, mapper.mappingSource().toString()); |
| 275 | + |
| 276 | + MapperParsingException e = expectThrows(MapperParsingException.class, |
| 277 | + () -> mapper.parse(new SourceToParse("test", "type", "1", |
| 278 | + BytesReference.bytes(jsonBuilder().startObject().field("field", malformedValue).endObject()), |
| 279 | + XContentType.JSON))); |
| 280 | + assertThat(e.getCause().getMessage(), containsString("Current token")); |
| 281 | + assertThat(e.getCause().getMessage(), containsString("not numeric, can not use numeric value accessors")); |
| 282 | + } |
| 283 | + } |
260 | 284 | } |
261 | 285 |
|
262 | 286 | public void testRejectNorms() throws IOException { |
|
0 commit comments