4
4
5
5
import com .fasterxml .jackson .annotation .JsonAnyGetter ;
6
6
import com .fasterxml .jackson .annotation .JsonAnySetter ;
7
+ import com .fasterxml .jackson .annotation .JsonIgnoreProperties ;
8
+ import com .fasterxml .jackson .annotation .JsonInclude ;
9
+
7
10
import com .fasterxml .jackson .databind .*;
8
11
12
+ // [databind#4316] : NPE when deserializing JsonAnySetter in Throwable
9
13
public class ExceptionWithAnySetter4316Test extends BaseMapTest
10
14
{
11
15
static class Problem extends Exception {
12
16
private static final long serialVersionUID = 1L ;
17
+ @ JsonInclude (content =JsonInclude .Include .NON_NULL )
18
+ @ JsonAnySetter
19
+ @ JsonAnyGetter
20
+ Map <String , Object > additionalProperties = new HashMap <>();
21
+ }
13
22
23
+ @ JsonIgnoreProperties ({ "cause" , "stackTrace" , "response" , "message" , "localizedMessage" , "suppressed" })
24
+ static class ProblemWithIgnorals extends Exception {
14
25
@ JsonAnySetter
15
26
@ JsonAnyGetter
16
27
Map <String , Object > additionalProperties = new HashMap <>();
17
28
}
18
29
19
30
private final ObjectMapper MAPPER = newJsonMapper ();
20
31
21
- // [databind#4316]
22
32
public void testWithAnySetter () throws Exception
23
33
{
24
34
Problem problem = new Problem ();
@@ -28,4 +38,53 @@ public void testWithAnySetter() throws Exception
28
38
assertEquals (Collections .singletonMap ("key" , "value" ),
29
39
result .additionalProperties );
30
40
}
41
+
42
+ // Map with ignored props keys specified in @JsonIgnoreProperties
43
+ public void testWithAnySetterAndIgnoralsPut () throws Exception
44
+ {
45
+ // Given
46
+ ProblemWithIgnorals problem = new ProblemWithIgnorals ();
47
+ problem .additionalProperties .put ("key" , "value" );
48
+ // Below key-value pairs also ignored from here....
49
+ problem .additionalProperties .put ("cause" , "ignored" );
50
+ problem .additionalProperties .put ("stackTrace" , "ignored" );
51
+ problem .additionalProperties .put ("response" , "ignored" );
52
+ problem .additionalProperties .put ("message" , "ignored" );
53
+ problem .additionalProperties .put ("localizedMessage" , "ignored" );
54
+ problem .additionalProperties .put ("suppressed" , "ignored" );
55
+
56
+ // When
57
+ String json = MAPPER .writeValueAsString (problem );
58
+ ProblemWithIgnorals result = MAPPER .readValue (json , ProblemWithIgnorals .class );
59
+
60
+ // Then
61
+ assertEquals (Collections .singletonMap ("key" , "value" ),
62
+ result .additionalProperties );
63
+ }
64
+
65
+ // With ignorals
66
+ public void testWithAnySetterAndIgnoralSimple () throws Exception
67
+ {
68
+ // Given
69
+ ProblemWithIgnorals problem = new ProblemWithIgnorals ();
70
+ problem .additionalProperties .put ("key" , "value" );
71
+
72
+ // When
73
+ String json = MAPPER .writeValueAsString (problem );
74
+ ProblemWithIgnorals result = MAPPER .readValue (json , ProblemWithIgnorals .class );
75
+
76
+ // Then
77
+ assertEquals (Collections .singletonMap ("key" , "value" ),
78
+ result .additionalProperties );
79
+ }
80
+
81
+ // With Include.NON_NULL
82
+ public void testWithAnySetterButEmptyIncludedFalse () throws Exception
83
+ {
84
+ Problem problem = new Problem ();
85
+ problem .additionalProperties .put ("exclude" , null );
86
+ String json = MAPPER .writeValueAsString (problem );
87
+ Problem result = MAPPER .readValue (json , Problem .class );
88
+ assertEquals (Collections .emptyMap (), result .additionalProperties );
89
+ }
31
90
}
0 commit comments