1
- package com .fasterxml .jackson .databind .tofix ;
1
+ package com .fasterxml .jackson .databind .deser ;
2
2
3
3
import org .junit .jupiter .api .Test ;
4
4
8
8
9
9
import com .fasterxml .jackson .databind .ObjectMapper ;
10
10
import com .fasterxml .jackson .databind .SerializationFeature ;
11
+ import com .fasterxml .jackson .databind .exc .InvalidDefinitionException ;
11
12
import com .fasterxml .jackson .databind .testutil .DatabindTestUtil ;
12
- import com .fasterxml .jackson .databind .testutil .failure .JacksonTestFailureExpected ;
13
13
14
- import static org .junit .jupiter .api .Assertions .assertEquals ;
15
- import static org .junit .jupiter .api .Assertions .assertTrue ;
14
+ import static org .junit .jupiter .api .Assertions .*;
16
15
17
16
// [databind#4773] Test to verify `SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS` behavior
18
17
// when serializing `Map` instances with un-comparable keys.
@@ -32,9 +31,8 @@ public static class ObjectContainer4773 {
32
31
.configure (SerializationFeature .ORDER_MAP_ENTRIES_BY_KEYS , true )
33
32
.build ();
34
33
35
- @ JacksonTestFailureExpected
36
34
@ Test
37
- void testSerializationWithIncomparableKeys ()
35
+ void testSerializationFailureWhenEnabledWithIncomparableKeys ()
38
36
throws Exception
39
37
{
40
38
// Given
@@ -44,11 +42,15 @@ void testSerializationWithIncomparableKeys()
44
42
45
43
// When : Throws exception
46
44
// com.fasterxml.jackson.databind.JsonMappingException: class java.util.Currency cannot be cast to class java.lang.Comparable
47
- String jsonResult = objectMapper .writeValueAsString (entity );
48
-
49
- // Then : Order should not matter, just plain old serialize
50
- assertTrue (jsonResult .contains ("GBP" ));
51
- assertTrue (jsonResult .contains ("AUD" ));
45
+ try {
46
+ objectMapper .writer ()
47
+ .with (SerializationFeature .FAIL_ON_ORDER_MAP_BY_INCOMPARABLE_KEY )
48
+ .writeValueAsString (entity );
49
+ fail ("Should not pass" );
50
+ } catch (InvalidDefinitionException e ) {
51
+ // Then
52
+ verifyException (e , "Cannot order Map entries by key of incomparable type" );
53
+ }
52
54
}
53
55
54
56
@ Test
@@ -75,4 +77,24 @@ void testSerializationWithGenericObjectKeys()
75
77
"'5':'N_TEXT'}}" ), jsonResult );
76
78
}
77
79
80
+ @ Test
81
+ void testSerWithNullType ()
82
+ throws Exception
83
+ {
84
+ // Given : Mixed keys with incomparable `Currency` and comparable `Integer`
85
+ ObjectContainer4773 entity = new ObjectContainer4773 ();
86
+ entity .exampleMap .put (null , "AUD_TEXT" );
87
+
88
+ // When : Throws exception
89
+ try {
90
+ objectMapper .writer ()
91
+ .with (SerializationFeature .FAIL_ON_ORDER_MAP_BY_INCOMPARABLE_KEY )
92
+ .writeValueAsString (entity );
93
+ fail ("Should not pass" );
94
+ } catch (InvalidDefinitionException e ) {
95
+ // Then
96
+ verifyException (e , "Cannot order Map entries by key of incomparable type [null]" );
97
+ }
98
+ }
99
+
78
100
}
0 commit comments