|
31 | 31 | import org.junit.Before; |
32 | 32 |
|
33 | 33 | import java.io.IOException; |
| 34 | +import java.util.Arrays; |
34 | 35 | import java.util.Collection; |
| 36 | +import java.util.List; |
35 | 37 |
|
36 | 38 | import static org.hamcrest.Matchers.containsString; |
37 | 39 |
|
@@ -223,37 +225,46 @@ public void testCoerce() throws Exception { |
223 | 225 | } |
224 | 226 |
|
225 | 227 | public void testIgnoreMalformed() throws Exception { |
| 228 | + doTestIgnoreMalformed("a", "For input string: \"a\""); |
| 229 | + |
| 230 | + List<String> values = Arrays.asList("NaN", "Infinity", "-Infinity"); |
| 231 | + for (String value : values) { |
| 232 | + doTestIgnoreMalformed(value, "[scaled_float] only supports finite values, but got [" + value + "]"); |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + private void doTestIgnoreMalformed(String value, String exceptionMessageContains) throws Exception { |
226 | 237 | String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") |
227 | | - .startObject("properties").startObject("field").field("type", "scaled_float") |
228 | | - .field("scaling_factor", 10.0).endObject().endObject() |
229 | | - .endObject().endObject().string(); |
| 238 | + .startObject("properties").startObject("field").field("type", "scaled_float") |
| 239 | + .field("scaling_factor", 10.0).endObject().endObject() |
| 240 | + .endObject().endObject().string(); |
230 | 241 |
|
231 | 242 | DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); |
232 | 243 |
|
233 | 244 | assertEquals(mapping, mapper.mappingSource().toString()); |
234 | 245 |
|
235 | 246 | ThrowingRunnable runnable = () -> mapper.parse(SourceToParse.source("test", "type", "1", XContentFactory.jsonBuilder() |
236 | 247 | .startObject() |
237 | | - .field("field", "a") |
| 248 | + .field("field", value) |
238 | 249 | .endObject() |
239 | 250 | .bytes(), |
240 | | - XContentType.JSON)); |
| 251 | + XContentType.JSON)); |
241 | 252 | MapperParsingException e = expectThrows(MapperParsingException.class, runnable); |
242 | | - assertThat(e.getCause().getMessage(), containsString("For input string: \"a\"")); |
| 253 | + assertThat(e.getCause().getMessage(), containsString(exceptionMessageContains)); |
243 | 254 |
|
244 | 255 | mapping = XContentFactory.jsonBuilder().startObject().startObject("type") |
245 | | - .startObject("properties").startObject("field").field("type", "scaled_float") |
246 | | - .field("scaling_factor", 10.0).field("ignore_malformed", true).endObject().endObject() |
247 | | - .endObject().endObject().string(); |
| 256 | + .startObject("properties").startObject("field").field("type", "scaled_float") |
| 257 | + .field("scaling_factor", 10.0).field("ignore_malformed", true).endObject().endObject() |
| 258 | + .endObject().endObject().string(); |
248 | 259 |
|
249 | 260 | DocumentMapper mapper2 = parser.parse("type", new CompressedXContent(mapping)); |
250 | 261 |
|
251 | 262 | ParsedDocument doc = mapper2.parse(SourceToParse.source("test", "type", "1", XContentFactory.jsonBuilder() |
252 | 263 | .startObject() |
253 | | - .field("field", "a") |
| 264 | + .field("field", value) |
254 | 265 | .endObject() |
255 | 266 | .bytes(), |
256 | | - XContentType.JSON)); |
| 267 | + XContentType.JSON)); |
257 | 268 |
|
258 | 269 | IndexableField[] fields = doc.rootDoc().getFields("field"); |
259 | 270 | assertEquals(0, fields.length); |
|
0 commit comments