Skip to content

Commit 0d378ec

Browse files
committed
Reproduct Kotlin Github56 test regression
1 parent 212bd79 commit 0d378ec

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package tools.jackson.databind.interop;
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonInclude;
5+
import com.fasterxml.jackson.annotation.JsonUnwrapped;
6+
import tools.jackson.databind.ObjectMapper;
7+
import tools.jackson.databind.testutil.DatabindTestUtil;
8+
9+
import org.junit.jupiter.api.Test;
10+
11+
import java.util.Arrays;
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
import static org.junit.jupiter.api.Assertions.assertEquals;
16+
17+
// Reproduction of Kotlin modules `Github56.kt` test wrt failure
18+
public class KotlinIssue56UnwrappedWithCreatorTest
19+
extends DatabindTestUtil
20+
{
21+
private final ObjectMapper mapper = newJsonMapper();
22+
23+
// BAD version - with constructor parameter
24+
public static class TestGalleryWidget_BAD {
25+
private String widgetReferenceId;
26+
private TestGallery gallery;
27+
28+
@JsonCreator
29+
public TestGalleryWidget_BAD(String widgetReferenceId,
30+
@JsonUnwrapped TestGallery gallery) {
31+
this.widgetReferenceId = widgetReferenceId;
32+
this.gallery = gallery;
33+
}
34+
35+
public String getWidgetReferenceId() {
36+
return widgetReferenceId;
37+
}
38+
39+
public TestGallery getGallery() {
40+
return gallery;
41+
}
42+
}
43+
44+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
45+
public static class TestGallery {
46+
public String id;
47+
public String headline;
48+
public String intro;
49+
public String role;
50+
public List<TestImage> images;
51+
52+
public TestGallery() { }
53+
54+
public TestGallery(String id, String headline, String intro, String role, List<TestImage> images) {
55+
this.id = id;
56+
this.headline = headline;
57+
this.intro = intro;
58+
this.role = role;
59+
this.images = images;
60+
}
61+
62+
@Override
63+
public boolean equals(Object o) {
64+
if (this == o) return true;
65+
if (o == null || getClass() != o.getClass()) return false;
66+
67+
TestGallery that = (TestGallery) o;
68+
69+
if (id != null ? !id.equals(that.id) : that.id != null) return false;
70+
if (headline != null ? !headline.equals(that.headline) : that.headline != null) return false;
71+
if (intro != null ? !intro.equals(that.intro) : that.intro != null) return false;
72+
if (role != null ? !role.equals(that.role) : that.role != null) return false;
73+
return images != null ? images.equals(that.images) : that.images == null;
74+
}
75+
}
76+
77+
@JsonInclude(JsonInclude.Include.NON_EMPTY)
78+
public static class TestImage {
79+
public String id;
80+
public String escenicId;
81+
public String caption;
82+
public String copyright;
83+
public Map<String, String> crops;
84+
85+
public TestImage() { }
86+
87+
public TestImage(String id) { this.id = id; }
88+
89+
@Override
90+
public boolean equals(Object o) {
91+
if (this == o) return true;
92+
if (o == null || getClass() != o.getClass()) return false;
93+
94+
TestImage testImage = (TestImage) o;
95+
96+
if (id != null ? !id.equals(testImage.id) : testImage.id != null) return false;
97+
if (escenicId != null ? !escenicId.equals(testImage.escenicId) : testImage.escenicId != null)
98+
return false;
99+
if (caption != null ? !caption.equals(testImage.caption) : testImage.caption != null) return false;
100+
if (copyright != null ? !copyright.equals(testImage.copyright) : testImage.copyright != null)
101+
return false;
102+
return crops != null ? crops.equals(testImage.crops) : testImage.crops == null;
103+
}
104+
}
105+
106+
private final String validJson =
107+
"{\"widgetReferenceId\":\"widgetReferenceId\",\"id\":\"id\",\"headline\":\"headline\",\"intro\":\"intro\",\"role\":\"role\",\"images\":[{\"id\":\"testImage1\"},{\"id\":\"testImage2\"}]}";
108+
109+
@Test
110+
public void serializes() throws Exception {
111+
TestGallery gallery = createGallery();
112+
TestGalleryWidget_BAD widget = new TestGalleryWidget_BAD("widgetReferenceId", gallery);
113+
String result = mapper.writeValueAsString(widget);
114+
assertEquals(validJson, result);
115+
}
116+
117+
@Test
118+
public void deserializesSuccessful() throws Exception {
119+
TestGalleryWidget_BAD obj = mapper.readValue(validJson, TestGalleryWidget_BAD.class);
120+
assertEquals("widgetReferenceId", obj.getWidgetReferenceId());
121+
assertEquals(createGallery(), obj.getGallery());
122+
}
123+
124+
private TestGallery createGallery() {
125+
return new TestGallery(
126+
"id",
127+
"headline",
128+
"intro",
129+
"role",
130+
Arrays.asList(
131+
new TestImage("testImage1"),
132+
new TestImage("testImage2")
133+
)
134+
);
135+
}
136+
}

0 commit comments

Comments
 (0)