1
1
package com .fasterxml .jackson .dataformat .xml .failing ;
2
2
3
3
import com .fasterxml .jackson .annotation .*;
4
+
4
5
import com .fasterxml .jackson .databind .ObjectMapper ;
5
- import com .fasterxml .jackson .databind .module .SimpleModule ;
6
+ import com .fasterxml .jackson .databind .json .JsonMapper ;
7
+
6
8
import com .fasterxml .jackson .dataformat .xml .XmlMapper ;
7
9
import com .fasterxml .jackson .dataformat .xml .XmlTestBase ;
8
10
9
- import java .io .IOException ;
10
- import java .nio .charset .StandardCharsets ;
11
-
12
11
/**
13
12
* Reproduces <i>no default no-arg ctor found</i> deserialization regression introduced to {@link XmlMapper} in 2.12.0.
14
13
*
15
14
* @see <a href="https://github.com/FasterXML/jackson-dataformat-xml/issues/491">jackson-dataformat-xml issue 491</a>
16
15
*/
17
- public class Issue491NoArgCtorDeserRegressionTest extends XmlTestBase {
18
-
16
+ public class Issue491NoArgCtorDeserRegressionTest extends XmlTestBase
17
+ {
18
+ @ JsonTypeInfo (
19
+ use = JsonTypeInfo .Id .NAME ,
20
+ include = JsonTypeInfo .As .EXISTING_PROPERTY ,
21
+ property = "type" ,
22
+ defaultImpl = DefaultProblem .class ,
23
+ visible = true )
24
+ @ JsonRootName ("problem" )
19
25
interface Problem {
20
-
21
26
String DEFAULT_TYPE = "about:blank" ;
22
-
23
27
int DEFAULT_STATUS = 500 ;
24
-
25
28
String getType ();
26
-
27
29
int getStatus ();
28
-
29
30
}
30
31
31
32
static class DefaultProblem implements Problem {
@@ -42,7 +43,8 @@ static class DefaultProblem implements Problem {
42
43
*
43
44
* @see <a href="https://github.com/FasterXML/jackson-databind/issues/1820">jackson-databind issue 1820</a>
44
45
*/
45
- DefaultProblem (String type , Integer status ) {
46
+ @ JsonCreator
47
+ DefaultProblem (@ JsonProperty ("type" ) String type , @ JsonProperty ("status" ) Integer status ) {
46
48
this .type = type != null ? type : Problem .DEFAULT_TYPE ;
47
49
this .status = status != null ? status : Problem .DEFAULT_STATUS ;
48
50
}
@@ -56,82 +58,34 @@ public String getType() {
56
58
public int getStatus () {
57
59
return status ;
58
60
}
59
-
60
- }
61
-
62
- @ JsonTypeInfo (
63
- use = JsonTypeInfo .Id .NAME ,
64
- include = JsonTypeInfo .As .EXISTING_PROPERTY ,
65
- property = "type" ,
66
- defaultImpl = DefaultProblem .class ,
67
- visible = true )
68
- @ JsonInclude (JsonInclude .Include .NON_EMPTY )
69
- @ JsonRootName ("problem" )
70
- interface ProblemMixIn extends Problem {
71
-
72
- @ Override
73
- @ JsonProperty ("type" )
74
- String getType ();
75
-
76
- @ Override
77
- @ JsonProperty ("status" )
78
- int getStatus ();
79
-
80
- }
81
-
82
- abstract static class DefaultProblemMixIn extends DefaultProblem {
83
-
84
- @ JsonCreator
85
- DefaultProblemMixIn (
86
- @ JsonProperty ("type" ) String type ,
87
- @ JsonProperty ("status" ) Integer status ) {
88
- super (type , status );
89
- throw new IllegalStateException (
90
- "mix-in constructor is there only for extracting the JSON mapping, " +
91
- "it should not have been called" );
92
- }
93
-
94
- }
95
-
96
- static class ProblemModule extends SimpleModule {
97
-
98
- @ Override
99
- public void setupModule (SetupContext context ) {
100
- super .setupModule (context );
101
- registerMixIns (context );
102
- }
103
-
104
- private static void registerMixIns (SetupContext context ) {
105
- context .setMixInAnnotations (DefaultProblem .class , DefaultProblemMixIn .class );
106
- context .setMixInAnnotations (Problem .class , ProblemMixIn .class );
107
- }
108
-
109
61
}
110
62
111
- private static final ProblemModule MODULE = new ProblemModule ();
112
-
113
- private static final ObjectMapper JSON_MAPPER = new ObjectMapper ().registerModule (MODULE );
63
+ private static final ObjectMapper JSON_MAPPER = new JsonMapper ();
114
64
115
- private static final XmlMapper XML_MAPPER = ( XmlMapper ) new XmlMapper (). registerModule ( MODULE );
65
+ private static final XmlMapper XML_MAPPER = newMapper ( );
116
66
117
67
/**
118
68
* Passes on 2.11.4 and 2.12.{0..4}.
119
69
*/
120
- public void test_empty_Problem_JSON_deserialization () throws IOException {
121
- byte [] problemJsonBytes = "{}" . getBytes ( StandardCharsets . UTF_8 );
122
- Problem problem = JSON_MAPPER .readValue (problemJsonBytes , Problem .class );
70
+ public void test_empty_Problem_JSON_deserialization () throws Exception
71
+ {
72
+ Problem problem = JSON_MAPPER .readValue ("{}" , Problem .class );
123
73
assertEquals (Problem .DEFAULT_TYPE , problem .getType ());
124
74
assertEquals (Problem .DEFAULT_STATUS , problem .getStatus ());
125
75
}
126
76
127
77
/**
128
78
* Passes on 2.11.4, but fails on 2.12.{0..4}.
129
79
*/
130
- public void test_empty_Problem_XML_deserialization () throws IOException {
131
- byte [] problemXmlBytes = "<problem/>" .getBytes (StandardCharsets .UTF_8 );
132
- Problem problem = XML_MAPPER .readValue (problemXmlBytes , Problem .class );
80
+ public void test_empty_Problem_XML_deserialization () throws Exception
81
+ {
82
+ Problem problem = XML_MAPPER .readValue (
83
+ // This WOULD work:
84
+ // "<problem><status>500</status></problem>",
85
+ // but not missing
86
+ "<problem />" ,
87
+ Problem .class );
133
88
assertEquals (Problem .DEFAULT_TYPE , problem .getType ());
134
89
assertEquals (Problem .DEFAULT_STATUS , problem .getStatus ());
135
90
}
136
-
137
91
}
0 commit comments