|
| 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 | + // IMPORTANT! Need to annotate accessor too |
| 40 | + @JsonUnwrapped |
| 41 | + public TestGallery getGallery() { |
| 42 | + return gallery; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @JsonInclude(JsonInclude.Include.NON_EMPTY) |
| 47 | + public static class TestGallery { |
| 48 | + public String id; |
| 49 | + public String headline; |
| 50 | + public String intro; |
| 51 | + public String role; |
| 52 | + public List<TestImage> images; |
| 53 | + |
| 54 | + public TestGallery() { } |
| 55 | + |
| 56 | + public TestGallery(String id, String headline, String intro, String role, List<TestImage> images) { |
| 57 | + this.id = id; |
| 58 | + this.headline = headline; |
| 59 | + this.intro = intro; |
| 60 | + this.role = role; |
| 61 | + this.images = images; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public boolean equals(Object o) { |
| 66 | + if (this == o) return true; |
| 67 | + if (o == null || getClass() != o.getClass()) return false; |
| 68 | + |
| 69 | + TestGallery that = (TestGallery) o; |
| 70 | + |
| 71 | + if (id != null ? !id.equals(that.id) : that.id != null) return false; |
| 72 | + if (headline != null ? !headline.equals(that.headline) : that.headline != null) return false; |
| 73 | + if (intro != null ? !intro.equals(that.intro) : that.intro != null) return false; |
| 74 | + if (role != null ? !role.equals(that.role) : that.role != null) return false; |
| 75 | + return images != null ? images.equals(that.images) : that.images == null; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @JsonInclude(JsonInclude.Include.NON_EMPTY) |
| 80 | + public static class TestImage { |
| 81 | + public String id; |
| 82 | + public String escenicId; |
| 83 | + public String caption; |
| 84 | + public String copyright; |
| 85 | + public Map<String, String> crops; |
| 86 | + |
| 87 | + public TestImage() { } |
| 88 | + |
| 89 | + public TestImage(String id) { this.id = id; } |
| 90 | + |
| 91 | + @Override |
| 92 | + public boolean equals(Object o) { |
| 93 | + if (this == o) return true; |
| 94 | + if (o == null || getClass() != o.getClass()) return false; |
| 95 | + |
| 96 | + TestImage testImage = (TestImage) o; |
| 97 | + |
| 98 | + if (id != null ? !id.equals(testImage.id) : testImage.id != null) return false; |
| 99 | + if (escenicId != null ? !escenicId.equals(testImage.escenicId) : testImage.escenicId != null) |
| 100 | + return false; |
| 101 | + if (caption != null ? !caption.equals(testImage.caption) : testImage.caption != null) return false; |
| 102 | + if (copyright != null ? !copyright.equals(testImage.copyright) : testImage.copyright != null) |
| 103 | + return false; |
| 104 | + return crops != null ? crops.equals(testImage.crops) : testImage.crops == null; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private final String validJson = |
| 109 | + "{\"widgetReferenceId\":\"widgetReferenceId\",\"id\":\"id\",\"headline\":\"headline\",\"intro\":\"intro\",\"role\":\"role\",\"images\":[{\"id\":\"testImage1\"},{\"id\":\"testImage2\"}]}"; |
| 110 | + |
| 111 | + @Test |
| 112 | + public void serializes() throws Exception { |
| 113 | + TestGallery gallery = createGallery(); |
| 114 | + TestGalleryWidget_BAD widget = new TestGalleryWidget_BAD("widgetReferenceId", gallery); |
| 115 | + String result = mapper.writeValueAsString(widget); |
| 116 | + assertEquals(validJson, result); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void deserializesSuccessful() throws Exception { |
| 121 | + TestGalleryWidget_BAD obj = mapper.readValue(validJson, TestGalleryWidget_BAD.class); |
| 122 | + assertEquals("widgetReferenceId", obj.getWidgetReferenceId()); |
| 123 | + assertEquals(createGallery(), obj.getGallery()); |
| 124 | + } |
| 125 | + |
| 126 | + private TestGallery createGallery() { |
| 127 | + return new TestGallery( |
| 128 | + "id", |
| 129 | + "headline", |
| 130 | + "intro", |
| 131 | + "role", |
| 132 | + Arrays.asList( |
| 133 | + new TestImage("testImage1"), |
| 134 | + new TestImage("testImage2") |
| 135 | + ) |
| 136 | + ); |
| 137 | + } |
| 138 | +} |
0 commit comments