10
10
import com .fasterxml .jackson .core .*;
11
11
import com .fasterxml .jackson .databind .*;
12
12
import com .fasterxml .jackson .databind .exc .InvalidDefinitionException ;
13
+ import com .fasterxml .jackson .databind .json .JsonMapper ;
13
14
import com .fasterxml .jackson .databind .testutil .DatabindTestUtil ;
14
15
import com .fasterxml .jackson .databind .util .TokenBuffer ;
15
16
17
+ import static org .assertj .core .api .Assertions .assertThat ;
16
18
import static org .junit .jupiter .api .Assertions .*;
17
19
18
20
// [databind#4082]: add fallback handling for Java 8 Optional types, to
19
21
// prevent accidental serialization as POJOs, as well as give more information
20
22
// on deserialization attempts
23
+ // [databind#5006]: add `MapperFeature.REQUIRE_HANDLERS_FOR_JAVA8_OPTIONALS`
21
24
//
22
- // @since 2.16
25
+ // @since 2.16 (changed in 2.19)
23
26
public class OptionalJava8Fallbacks4082Test extends DatabindTestUtil
24
27
{
25
28
private final ObjectMapper MAPPER = newJsonMapper ();
26
29
30
+ ObjectMapper LENIENT_MAPPER = JsonMapper .builder ()
31
+ .disable (MapperFeature .REQUIRE_HANDLERS_FOR_JAVA8_OPTIONALS )
32
+ .disable (MapperFeature .CAN_OVERRIDE_ACCESS_MODIFIERS )
33
+ .build ();
34
+
27
35
// Test to prevent serialization as POJO, without Java 8 date/time module:
28
36
@ Test
29
37
public void testPreventSerialization () throws Exception {
@@ -42,6 +50,7 @@ private void _testPreventSerialization(Object value) throws Exception
42
50
verifyException (e , "Java 8 optional type `" +value .getClass ().getName ()
43
51
+"` not supported by default" );
44
52
verifyException (e , "add Module \" com.fasterxml.jackson.datatype:jackson-datatype-jdk8\" " );
53
+ verifyException (e , "(or disable `MapperFeature.REQUIRE_HANDLERS_FOR_JAVA8_OPTIONALS`" );
45
54
}
46
55
}
47
56
@@ -62,6 +71,7 @@ private void _testBetterDeserializationError(Class<?> target) throws Exception
62
71
} catch (InvalidDefinitionException e ) {
63
72
verifyException (e , "Java 8 optional type `" +target .getName ()+"` not supported by default" );
64
73
verifyException (e , "add Module \" com.fasterxml.jackson.datatype:jackson-datatype-jdk8\" " );
74
+ verifyException (e , "(or disable `MapperFeature.REQUIRE_HANDLERS_FOR_JAVA8_OPTIONALS`" );
65
75
}
66
76
}
67
77
@@ -93,4 +103,30 @@ public void testAllowAsEmbedded() throws Exception
93
103
}
94
104
}
95
105
}
106
+
107
+ // [databind#5006]
108
+ @ Test
109
+ public void testAllowDeserializationWithFeature () throws Exception
110
+ {
111
+ _testAllowDeserializationLenient (Optional .class );
112
+ _testAllowDeserializationLenient (OptionalInt .class );
113
+ _testAllowDeserializationLenient (OptionalLong .class );
114
+ _testAllowDeserializationLenient (OptionalDouble .class );
115
+ }
116
+
117
+ private void _testAllowDeserializationLenient (Class <?> target ) throws Exception {
118
+ JacksonException e = assertThrows (JacksonException .class , () ->
119
+ LENIENT_MAPPER .readValue ("{}" , target ));
120
+ assertThat (e ).hasMessageContaining ("Cannot construct instance of `" +target .getName ()+"`" );
121
+ }
122
+
123
+ // [databind#5006]
124
+ @ Test
125
+ public void testAllowSerializationWithFeature () throws Exception
126
+ {
127
+ assertThat (LENIENT_MAPPER .writeValueAsString (Optional .empty ())).contains ("\" empty\" :true" );
128
+ assertThat (LENIENT_MAPPER .writeValueAsString (OptionalInt .of (13 ))).contains ("\" asInt\" :13" );
129
+ assertThat (LENIENT_MAPPER .writeValueAsString (OptionalLong .of (-1L ))).contains ("\" asLong\" :-1" );
130
+ assertThat (LENIENT_MAPPER .writeValueAsString (OptionalDouble .of (0.5 ))).contains ("\" asDouble\" :0.5" );
131
+ }
96
132
}
0 commit comments