|
| 1 | +package io.openapitoools.jackson.dataformat.hal.ser; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonSubTypes; |
| 6 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
| 7 | +import com.fasterxml.jackson.annotation.JsonTypeInfo.As; |
| 8 | +import com.fasterxml.jackson.annotation.JsonTypeInfo.Id; |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | +import io.openapitools.jackson.dataformat.hal.HALLink; |
| 11 | +import io.openapitools.jackson.dataformat.hal.HALMapper; |
| 12 | +import io.openapitools.jackson.dataformat.hal.annotation.EmbeddedResource; |
| 13 | +import io.openapitools.jackson.dataformat.hal.annotation.Link; |
| 14 | +import io.openapitools.jackson.dataformat.hal.annotation.Resource; |
| 15 | +import java.net.URI; |
| 16 | +import java.util.Arrays; |
| 17 | +import java.util.Collection; |
| 18 | +import java.util.List; |
| 19 | +import org.junit.Test; |
| 20 | + |
| 21 | +public class HALBeanSerializerPolymorphicWithExistingFieldIT { |
| 22 | + |
| 23 | + ObjectMapper om = new HALMapper(); |
| 24 | + |
| 25 | + @Test |
| 26 | + public void testSerializationForResourceWithEmbeddableList() throws Exception { |
| 27 | + @Resource |
| 28 | + class TopResource { |
| 29 | + |
| 30 | + public String id = "1"; |
| 31 | + |
| 32 | + @Link public HALLink self = new HALLink.Builder(URI.create("/top/1")).build(); |
| 33 | + |
| 34 | + @Link("child") |
| 35 | + public List<HALLink> childResourcesLink = |
| 36 | + Arrays.asList( |
| 37 | + new HALLink.Builder(URI.create("/top/1/child/1")).build(), |
| 38 | + new HALLink.Builder(URI.create("/top/1/child/2")).build()); |
| 39 | + |
| 40 | + @EmbeddedResource("child") |
| 41 | + public Collection<ChildResource> children = |
| 42 | + Arrays.asList(new ChildResource("1"), new OtherChildResource("2", "Max")); |
| 43 | + } |
| 44 | + |
| 45 | + TopResource resource = new TopResource(); |
| 46 | + String json = om.writeValueAsString(resource); |
| 47 | + assertEquals( |
| 48 | + "{" |
| 49 | + + "\"_links\":{" |
| 50 | + + "\"child\":[{\"href\":\"/top/1/child/1\"},{\"href\":\"/top/1/child/2\"}]," |
| 51 | + + "\"self\":{\"href\":\"/top/1\"}" |
| 52 | + + "}," |
| 53 | + + "\"_embedded\":{" |
| 54 | + + "\"child\":[" |
| 55 | + + "{\"_links\":{\"self\":{\"href\":\"/top/1/child/1\"}},\"type\":\"ChildResource\",\"id\":\"1\"}," |
| 56 | + + "{\"_links\":{\"self\":{\"href\":\"/top/1/child/2\"}},\"type\":\"OtherChildResource\",\"id\":\"2\",\"name\":\"Max\"}]" |
| 57 | + + "}," |
| 58 | + + "\"id\":\"1\"}", |
| 59 | + json); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testSerializationForResourceWithList() throws Exception { |
| 64 | + @Resource |
| 65 | + class TopResourceWithoutEmbedded { |
| 66 | + public String id = "1"; |
| 67 | + |
| 68 | + @Link public HALLink self = new HALLink.Builder(URI.create("/top/1")).build(); |
| 69 | + |
| 70 | + public Collection<ChildResource> children = |
| 71 | + Arrays.asList(new ChildResource("1"), new OtherChildResource("2", "Max")); |
| 72 | + } |
| 73 | + |
| 74 | + TopResourceWithoutEmbedded resource = new TopResourceWithoutEmbedded(); |
| 75 | + String json = om.writeValueAsString(resource); |
| 76 | + assertEquals( |
| 77 | + "{" |
| 78 | + + "\"_links\":{" |
| 79 | + + "\"self\":{\"href\":\"/top/1\"}" |
| 80 | + + "}," |
| 81 | + + "\"id\":\"1\"," |
| 82 | + + "\"children\":[" |
| 83 | + + "{\"_links\":{\"self\":{\"href\":\"/top/1/child/1\"}},\"type\":\"ChildResource\",\"id\":\"1\"}," |
| 84 | + + "{\"_links\":{\"self\":{\"href\":\"/top/1/child/2\"}},\"type\":\"OtherChildResource\",\"id\":\"2\",\"name\":\"Max\"}]" |
| 85 | + + "}", |
| 86 | + json); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testSerializationForResourceEmbedded() throws Exception { |
| 91 | + @Resource |
| 92 | + class SimpleTopResourceEmbedded { |
| 93 | + public String id = "1"; |
| 94 | + |
| 95 | + @Link public HALLink self = new HALLink.Builder(URI.create("/top/1")).build(); |
| 96 | + |
| 97 | + @EmbeddedResource public ChildResource child = new ChildResource("1"); |
| 98 | + } |
| 99 | + |
| 100 | + SimpleTopResourceEmbedded resource = new SimpleTopResourceEmbedded(); |
| 101 | + String json = om.writeValueAsString(resource); |
| 102 | + assertEquals( |
| 103 | + "{" |
| 104 | + + "\"_links\":{" |
| 105 | + + "\"self\":{\"href\":\"/top/1\"}" |
| 106 | + + "}," |
| 107 | + + "\"_embedded\":{" |
| 108 | + + "\"child\":{\"_links\":{\"self\":{\"href\":\"/top/1/child/1\"}},\"type\":\"ChildResource\",\"id\":\"1\"}" |
| 109 | + + "}," |
| 110 | + + "\"id\":\"1\"}", |
| 111 | + json); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + public void testSerializationForResource() throws Exception { |
| 116 | + @Resource |
| 117 | + class SimpleTopResource { |
| 118 | + public String id = "1"; |
| 119 | + |
| 120 | + @Link public HALLink self = new HALLink.Builder(URI.create("/top/1")).build(); |
| 121 | + |
| 122 | + public ChildResource child = new ChildResource("1"); |
| 123 | + } |
| 124 | + |
| 125 | + SimpleTopResource resource = new SimpleTopResource(); |
| 126 | + String json = om.writeValueAsString(resource); |
| 127 | + assertEquals( |
| 128 | + "{" |
| 129 | + + "\"_links\":{" |
| 130 | + + "\"self\":{\"href\":\"/top/1\"}" |
| 131 | + + "}," |
| 132 | + + "\"id\":\"1\"," |
| 133 | + + "\"child\":{" |
| 134 | + + "\"_links\":{\"self\":{\"href\":\"/top/1/child/1\"}},\"type\":\"ChildResource\",\"id\":\"1\"" |
| 135 | + + "}}", |
| 136 | + json); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + public void testSerializationForAnnotatedPolymorphicObject() throws Exception { |
| 141 | + ChildResource resource = new ChildResource("1"); |
| 142 | + String json = om.writeValueAsString(resource); |
| 143 | + assertEquals( |
| 144 | + "{" |
| 145 | + + "\"_links\":{" |
| 146 | + + "\"self\":{\"href\":\"/top/1/child/1\"}" |
| 147 | + + "}," |
| 148 | + + "\"type\":\"ChildResource\"," |
| 149 | + + "\"id\":\"1\"}", |
| 150 | + json); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + public void testSerializationForNotAnnotatedPolymorphicObject() throws Exception { |
| 155 | + ChildResource resource = new OtherChildResource("1", "Max"); |
| 156 | + String json = om.writeValueAsString(resource); |
| 157 | + assertEquals( |
| 158 | + "{" |
| 159 | + + "\"_links\":{" |
| 160 | + + "\"self\":{\"href\":\"/top/1/child/1\"}" |
| 161 | + + "}," |
| 162 | + + "\"type\":\"OtherChildResource\"," |
| 163 | + + "\"id\":\"1\"," |
| 164 | + + "\"name\":\"Max\"}", |
| 165 | + json); |
| 166 | + } |
| 167 | + |
| 168 | + @JsonTypeInfo( |
| 169 | + use = Id.NAME, |
| 170 | + include = As.EXISTING_PROPERTY, |
| 171 | + property = "type", |
| 172 | + defaultImpl = ChildResource.class) |
| 173 | + @JsonSubTypes({@JsonSubTypes.Type(value = OtherChildResource.class, name = "OtherChildResource")}) |
| 174 | + @Resource |
| 175 | + public static class ChildResource { |
| 176 | + |
| 177 | + public String type = "ChildResource"; |
| 178 | + |
| 179 | + public String id; |
| 180 | + |
| 181 | + @Link public HALLink self; |
| 182 | + |
| 183 | + public ChildResource(String id) { |
| 184 | + this.id = id; |
| 185 | + self = new HALLink.Builder(URI.create("/top/1/child/" + id)).build(); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + @Resource |
| 190 | + public static class OtherChildResource extends ChildResource { |
| 191 | + |
| 192 | + public String type = "OtherChildResource"; |
| 193 | + |
| 194 | + public String name; |
| 195 | + |
| 196 | + public OtherChildResource(String id, String name) { |
| 197 | + super(id); |
| 198 | + this.name = name; |
| 199 | + } |
| 200 | + } |
| 201 | +} |
0 commit comments