Skip to content

Commit 90a06b6

Browse files
committed
Simplify #491 test a bit, to remove mix-ins
1 parent 7c684f6 commit 90a06b6

File tree

1 file changed

+27
-73
lines changed

1 file changed

+27
-73
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
package com.fasterxml.jackson.dataformat.xml.failing;
22

33
import com.fasterxml.jackson.annotation.*;
4+
45
import com.fasterxml.jackson.databind.ObjectMapper;
5-
import com.fasterxml.jackson.databind.module.SimpleModule;
6+
import com.fasterxml.jackson.databind.json.JsonMapper;
7+
68
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
79
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
810

9-
import java.io.IOException;
10-
import java.nio.charset.StandardCharsets;
11-
1211
/**
1312
* Reproduces <i>no default no-arg ctor found</i> deserialization regression introduced to {@link XmlMapper} in 2.12.0.
1413
*
1514
* @see <a href="https://github.com/FasterXML/jackson-dataformat-xml/issues/491">jackson-dataformat-xml issue 491</a>
1615
*/
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")
1925
interface Problem {
20-
2126
String DEFAULT_TYPE = "about:blank";
22-
2327
int DEFAULT_STATUS = 500;
24-
2528
String getType();
26-
2729
int getStatus();
28-
2930
}
3031

3132
static class DefaultProblem implements Problem {
@@ -42,7 +43,8 @@ static class DefaultProblem implements Problem {
4243
*
4344
* @see <a href="https://github.com/FasterXML/jackson-databind/issues/1820">jackson-databind issue 1820</a>
4445
*/
45-
DefaultProblem(String type, Integer status) {
46+
@JsonCreator
47+
DefaultProblem(@JsonProperty("type") String type, @JsonProperty("status") Integer status) {
4648
this.type = type != null ? type : Problem.DEFAULT_TYPE;
4749
this.status = status != null ? status : Problem.DEFAULT_STATUS;
4850
}
@@ -56,82 +58,34 @@ public String getType() {
5658
public int getStatus() {
5759
return status;
5860
}
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-
10961
}
11062

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();
11464

115-
private static final XmlMapper XML_MAPPER = (XmlMapper) new XmlMapper().registerModule(MODULE);
65+
private static final XmlMapper XML_MAPPER = newMapper();
11666

11767
/**
11868
* Passes on 2.11.4 and 2.12.{0..4}.
11969
*/
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);
12373
assertEquals(Problem.DEFAULT_TYPE, problem.getType());
12474
assertEquals(Problem.DEFAULT_STATUS, problem.getStatus());
12575
}
12676

12777
/**
12878
* Passes on 2.11.4, but fails on 2.12.{0..4}.
12979
*/
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);
13388
assertEquals(Problem.DEFAULT_TYPE, problem.getType());
13489
assertEquals(Problem.DEFAULT_STATUS, problem.getStatus());
13590
}
136-
13791
}

0 commit comments

Comments
 (0)