|
| 1 | +package com.fasterxml.jackson.databind.convert; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | +import com.fasterxml.jackson.databind.ObjectReader; |
| 7 | +import com.fasterxml.jackson.databind.cfg.CoercionAction; |
| 8 | +import com.fasterxml.jackson.databind.cfg.CoercionInputShape; |
| 9 | +import com.fasterxml.jackson.databind.exc.MismatchedInputException; |
| 10 | +import com.fasterxml.jackson.databind.type.LogicalType; |
| 11 | + |
| 12 | +public class CoerceIntToStringTest extends BaseMapTest |
| 13 | +{ |
| 14 | + private final ObjectMapper DEFAULT_MAPPER = newJsonMapper(); |
| 15 | + |
| 16 | + private final ObjectMapper MAPPER_TO_FAIL = jsonMapperBuilder() |
| 17 | + .withCoercionConfig(LogicalType.Textual, cfg -> |
| 18 | + cfg.setCoercion(CoercionInputShape.Integer, CoercionAction.Fail)) |
| 19 | + .build(); |
| 20 | + |
| 21 | + private final ObjectMapper MAPPER_TRY_CONVERT = jsonMapperBuilder() |
| 22 | + .withCoercionConfig(LogicalType.Textual, cfg -> |
| 23 | + cfg.setCoercion(CoercionInputShape.Integer, CoercionAction.TryConvert)) |
| 24 | + .build(); |
| 25 | + |
| 26 | + private final ObjectMapper MAPPER_TO_NULL = jsonMapperBuilder() |
| 27 | + .withCoercionConfig(LogicalType.Textual, cfg -> |
| 28 | + cfg.setCoercion(CoercionInputShape.Integer, CoercionAction.AsNull)) |
| 29 | + .build(); |
| 30 | + |
| 31 | + private final ObjectMapper MAPPER_TO_EMPTY = jsonMapperBuilder() |
| 32 | + .withCoercionConfig(LogicalType.Textual, cfg -> |
| 33 | + cfg.setCoercion(CoercionInputShape.Integer, CoercionAction.AsEmpty)) |
| 34 | + .build(); |
| 35 | + |
| 36 | + public void testDefaultIntToStringCoercion() throws JsonProcessingException |
| 37 | + { |
| 38 | + assertSuccessfulIntToStringCoercionWith(DEFAULT_MAPPER); |
| 39 | + } |
| 40 | + |
| 41 | + public void testCoerceConfigToConvert() throws JsonProcessingException |
| 42 | + { |
| 43 | + assertSuccessfulIntToStringCoercionWith(MAPPER_TRY_CONVERT); |
| 44 | + } |
| 45 | + |
| 46 | + public void testCoerceConfigToNull() throws JsonProcessingException |
| 47 | + { |
| 48 | + assertNull(MAPPER_TO_NULL.readValue("1", String.class)); |
| 49 | + StringWrapper w = MAPPER_TO_NULL.readValue("{\"str\": -5}", StringWrapper.class); |
| 50 | + assertNull(w.str); |
| 51 | + String[] arr = MAPPER_TO_NULL.readValue("[ 2 ]", String[].class); |
| 52 | + assertEquals(1, arr.length); |
| 53 | + assertNull(arr[0]); |
| 54 | + } |
| 55 | + |
| 56 | + public void testCoerceConfigToEmpty() throws JsonProcessingException |
| 57 | + { |
| 58 | + assertEquals("", MAPPER_TO_EMPTY.readValue("3", String.class)); |
| 59 | + StringWrapper w = MAPPER_TO_EMPTY.readValue("{\"str\": -5}", StringWrapper.class); |
| 60 | + assertEquals("", w.str); |
| 61 | + String[] arr = MAPPER_TO_EMPTY.readValue("[ 2 ]", String[].class); |
| 62 | + assertEquals(1, arr.length); |
| 63 | + assertEquals("", arr[0]); |
| 64 | + } |
| 65 | + |
| 66 | + public void testCoerceConfigToFail() throws JsonProcessingException |
| 67 | + { |
| 68 | + _verifyCoerceFail(MAPPER_TO_FAIL, String.class, "3"); |
| 69 | + _verifyCoerceFail(MAPPER_TO_FAIL, StringWrapper.class, "{\"str\": -5}", "string"); |
| 70 | + _verifyCoerceFail(MAPPER_TO_FAIL, String[].class, "[ 2 ]", "element of `java.lang.String[]`"); |
| 71 | + } |
| 72 | + |
| 73 | + /* |
| 74 | + /******************************************************** |
| 75 | + /* Helper methods |
| 76 | + /******************************************************** |
| 77 | + */ |
| 78 | + |
| 79 | + private void assertSuccessfulIntToStringCoercionWith(ObjectMapper objectMapper) |
| 80 | + throws JsonProcessingException |
| 81 | + { |
| 82 | + assertEquals("3", objectMapper.readValue("3", String.class)); |
| 83 | + assertEquals("-2", objectMapper.readValue("-2", String.class)); |
| 84 | + { |
| 85 | + StringWrapper w = objectMapper.readValue("{\"str\": -5}", StringWrapper.class); |
| 86 | + assertEquals("-5", w.str); |
| 87 | + String[] arr = objectMapper.readValue("[ 2 ]", String[].class); |
| 88 | + assertEquals("2", arr[0]); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private void _verifyCoerceFail(ObjectMapper m, Class<?> targetType, |
| 93 | + String doc) throws JsonProcessingException |
| 94 | + { |
| 95 | + _verifyCoerceFail(m.reader(), targetType, doc, targetType.getName()); |
| 96 | + } |
| 97 | + |
| 98 | + private void _verifyCoerceFail(ObjectMapper m, Class<?> targetType, |
| 99 | + String doc, String targetTypeDesc) throws JsonProcessingException |
| 100 | + { |
| 101 | + _verifyCoerceFail(m.reader(), targetType, doc, targetTypeDesc); |
| 102 | + } |
| 103 | + |
| 104 | + private void _verifyCoerceFail(ObjectReader r, Class<?> targetType, |
| 105 | + String doc, String targetTypeDesc) throws JsonProcessingException |
| 106 | + { |
| 107 | + try { |
| 108 | + r.forType(targetType).readValue(doc); |
| 109 | + fail("Should not accept Integer for "+targetType.getName()+" when configured to"); |
| 110 | + } catch (MismatchedInputException e) { |
| 111 | + verifyException(e, "Cannot coerce Integer"); |
| 112 | + verifyException(e, targetTypeDesc); |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments