|
| 1 | +package com.fasterxml.jackson.databind.interop; |
| 2 | + |
| 3 | +import java.time.LocalDate; |
| 4 | + |
| 5 | +import org.assertj.core.api.Assertions; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.core.JacksonException; |
| 9 | +import com.fasterxml.jackson.databind.*; |
| 10 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 11 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 12 | + |
| 13 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 15 | +import static org.junit.jupiter.api.Assertions.fail; |
| 16 | + |
| 17 | +public class Java8Datetime4533Test |
| 18 | + extends DatabindTestUtil |
| 19 | +{ |
| 20 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 21 | + |
| 22 | + private final ObjectMapper LENIENT_MAPPER = JsonMapper.builder() |
| 23 | + .disable(MapperFeature.REQUIRE_HANDLERS_FOR_JAVA8_TIMES) |
| 24 | + .disable(MapperFeature.CAN_OVERRIDE_ACCESS_MODIFIERS) |
| 25 | + .build(); |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testPreventSerialization() throws Exception |
| 29 | + { |
| 30 | + // [databind#4533]: prevent accidental serialization of Java 8 date/time types |
| 31 | + // as POJOs, without Java 8 date/time module: |
| 32 | + _testPreventSerialization(java.time.LocalDateTime.now()); |
| 33 | + _testPreventSerialization(java.time.LocalDate.now()); |
| 34 | + _testPreventSerialization(java.time.LocalTime.now()); |
| 35 | + _testPreventSerialization(java.time.OffsetDateTime.now()); |
| 36 | + _testPreventSerialization(java.time.ZonedDateTime.now()); |
| 37 | + } |
| 38 | + |
| 39 | + private void _testPreventSerialization(Object value) throws Exception |
| 40 | + { |
| 41 | + try { |
| 42 | + String json = MAPPER.writeValueAsString(value); |
| 43 | + fail("Should not pass, wrote out as\n: "+json); |
| 44 | + } catch (com.fasterxml.jackson.databind.exc.InvalidDefinitionException e) { |
| 45 | + verifyException(e, "Java 8 date/time type `"+value.getClass().getName() |
| 46 | + +"` not supported by default"); |
| 47 | + verifyException(e, "add Module \"com.fasterxml.jackson.datatype:jackson-datatype-jsr310\""); |
| 48 | + verifyException(e, "(or disable `MapperFeature.%s`)", |
| 49 | + MapperFeature.REQUIRE_HANDLERS_FOR_JAVA8_TIMES.name()); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testPreventDeserialization() throws Exception |
| 55 | + { |
| 56 | + // [databind#4533]: prevent accidental deserialization of Java 8 date/time types |
| 57 | + // as POJOs, without Java 8 date/time module: |
| 58 | + _testPreventDeserialization(java.time.LocalDateTime.class); |
| 59 | + _testPreventDeserialization(java.time.LocalDate.class); |
| 60 | + _testPreventDeserialization(java.time.LocalTime.class); |
| 61 | + _testPreventDeserialization(java.time.OffsetDateTime.class); |
| 62 | + _testPreventDeserialization(java.time.ZonedDateTime.class); |
| 63 | + } |
| 64 | + |
| 65 | + private void _testPreventDeserialization(Class<?> value) throws Exception |
| 66 | + { |
| 67 | + try { |
| 68 | + Object result = MAPPER.readValue(" 0 ", value); |
| 69 | + fail("Not expecting to pass, resulted in: "+result); |
| 70 | + } catch (com.fasterxml.jackson.databind.exc.InvalidDefinitionException e) { |
| 71 | + verifyException(e, "Java 8 date/time type `"+value.getName() |
| 72 | + +"` not supported by default"); |
| 73 | + verifyException(e, "add Module \"com.fasterxml.jackson.datatype:jackson-datatype-jsr310\""); |
| 74 | + verifyException(e, "(or disable `MapperFeature.%s`)", |
| 75 | + MapperFeature.REQUIRE_HANDLERS_FOR_JAVA8_TIMES.name()); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testLenientSerailization() throws Exception |
| 81 | + { |
| 82 | + _testLenientSerialization(java.time.LocalDateTime.now()); |
| 83 | + _testLenientSerialization(java.time.LocalDate.now()); |
| 84 | + _testLenientSerialization(java.time.LocalTime.now()); |
| 85 | + _testLenientSerialization(java.time.OffsetDateTime.now()); |
| 86 | + |
| 87 | + // Except ZonedDateTime serialization fails with ... |
| 88 | + try { |
| 89 | + _testLenientSerialization(java.time.ZonedDateTime.now()); |
| 90 | + fail("Should not pass, wrote out as\n: "+java.time.ZonedDateTime.now()); |
| 91 | + } catch (JsonMappingException e) { |
| 92 | + verifyException(e, "Class com.fasterxml.jackson.databind.ser.BeanPropertyWriter"); |
| 93 | + verifyException(e, "with modifiers \"public\""); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private void _testLenientSerialization(Object value) throws Exception |
| 98 | + { |
| 99 | + String json = LENIENT_MAPPER.writeValueAsString(value); |
| 100 | + assertThat(json).isNotNull(); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void testAllowDeserializationWithFeature() throws Exception |
| 105 | + { |
| 106 | + _testAllowDeserializationLenient(java.time.LocalDateTime.class); |
| 107 | + _testAllowDeserializationLenient(java.time.LocalDate.class); |
| 108 | + _testAllowDeserializationLenient(java.time.LocalTime.class); |
| 109 | + _testAllowDeserializationLenient(java.time.OffsetDateTime.class); |
| 110 | + _testAllowDeserializationLenient(java.time.ZonedDateTime.class); |
| 111 | + } |
| 112 | + |
| 113 | + private void _testAllowDeserializationLenient(Class<?> target) throws Exception { |
| 114 | + JacksonException e = assertThrows(JacksonException.class, () -> |
| 115 | + LENIENT_MAPPER.readValue("{}", target)); |
| 116 | + Assertions.assertThat(e).hasMessageContaining("Cannot construct instance of `"+target.getName()+"`"); |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments