|
| 1 | +package com.fasterxml.jackson.databind.interop; |
| 2 | + |
| 3 | +import java.util.Optional; |
| 4 | +import java.util.OptionalDouble; |
| 5 | +import java.util.OptionalInt; |
| 6 | +import java.util.OptionalLong; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.core.*; |
| 9 | + |
| 10 | +import com.fasterxml.jackson.databind.*; |
| 11 | +import com.fasterxml.jackson.databind.exc.InvalidDefinitionException; |
| 12 | +import com.fasterxml.jackson.databind.util.TokenBuffer; |
| 13 | + |
| 14 | +// [databind#4082]: add fallback handling for Java 8 Optional types, to |
| 15 | +// prevent accidental serialization as POJOs, as well as give more information |
| 16 | +// on deserialization attempts |
| 17 | +// |
| 18 | +// @since 2.16 |
| 19 | +public class OptionalJava8Fallbacks4082Test extends BaseMapTest |
| 20 | +{ |
| 21 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 22 | + |
| 23 | + // Test to prevent serialization as POJO, without Java 8 date/time module: |
| 24 | + public void testPreventSerialization() throws Exception { |
| 25 | + _testPreventSerialization(Optional.empty()); |
| 26 | + _testPreventSerialization(OptionalInt.of(13)); |
| 27 | + _testPreventSerialization(OptionalLong.of(-1L)); |
| 28 | + _testPreventSerialization(OptionalDouble.of(0.5)); |
| 29 | + } |
| 30 | + |
| 31 | + private void _testPreventSerialization(Object value) throws Exception |
| 32 | + { |
| 33 | + try { |
| 34 | + String json = MAPPER.writeValueAsString(value); |
| 35 | + fail("Should not pass, wrote out as\n: "+json); |
| 36 | + } catch (InvalidDefinitionException e) { |
| 37 | + verifyException(e, "Java 8 optional type `"+value.getClass().getName() |
| 38 | + +"` not supported by default"); |
| 39 | + verifyException(e, "add Module \"com.fasterxml.jackson.datatype:jackson-datatype-jdk8\""); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + public void testBetterDeserializationError() throws Exception |
| 44 | + { |
| 45 | + _testBetterDeserializationError(Optional.class); |
| 46 | + _testBetterDeserializationError(OptionalInt.class); |
| 47 | + _testBetterDeserializationError(OptionalLong.class); |
| 48 | + _testBetterDeserializationError(OptionalDouble.class); |
| 49 | + } |
| 50 | + |
| 51 | + private void _testBetterDeserializationError(Class<?> target) throws Exception |
| 52 | + { |
| 53 | + try { |
| 54 | + Object result = MAPPER.readValue(" 0 ", target); |
| 55 | + fail("Not expecting to pass, resulted in: "+result); |
| 56 | + } catch (InvalidDefinitionException e) { |
| 57 | + verifyException(e, "Java 8 optional type `"+target.getName()+"` not supported by default"); |
| 58 | + verifyException(e, "add Module \"com.fasterxml.jackson.datatype:jackson-datatype-jdk8\""); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // But, [databind#3091], allow deser from JsonToken.VALUE_EMBEDDED_OBJECT |
| 63 | + public void testAllowAsEmbedded() throws Exception |
| 64 | + { |
| 65 | + Optional<Object> optValue = Optional.empty(); |
| 66 | + try (TokenBuffer tb = new TokenBuffer((ObjectCodec) null, false)) { |
| 67 | + tb.writeEmbeddedObject(optValue); |
| 68 | + |
| 69 | + try (JsonParser p = tb.asParser()) { |
| 70 | + Optional<?> result = MAPPER.readValue(p, Optional.class); |
| 71 | + assertSame(optValue, result); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // but also try deser into an array |
| 76 | + try (TokenBuffer tb = new TokenBuffer((ObjectCodec) null, false)) { |
| 77 | + tb.writeStartArray(); |
| 78 | + tb.writeEmbeddedObject(optValue); |
| 79 | + tb.writeEndArray(); |
| 80 | + |
| 81 | + try (JsonParser p = tb.asParser()) { |
| 82 | + Object[] result = MAPPER.readValue(p, Object[].class); |
| 83 | + assertNotNull(result); |
| 84 | + assertEquals(1, result.length); |
| 85 | + assertSame(optValue, result[0]); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments