|
| 1 | +package com.fasterxml.jackson.databind.deser; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonIdentityInfo; |
| 4 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 5 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 6 | +import com.fasterxml.jackson.annotation.JsonSubTypes; |
| 7 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
| 8 | +import com.fasterxml.jackson.annotation.ObjectIdGenerators; |
| 9 | +import com.fasterxml.jackson.core.JsonParseException; |
| 10 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 11 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 12 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 13 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | + |
| 19 | +/** |
| 20 | + * Test for testing forward reference handling |
| 21 | + */ |
| 22 | +public class TestForwardReference extends BaseMapTest { |
| 23 | + |
| 24 | + private final ObjectMapper MAPPER = new ObjectMapper() |
| 25 | + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) |
| 26 | + .enable(SerializationFeature.INDENT_OUTPUT) |
| 27 | + .setSerializationInclusion(JsonInclude.Include.NON_NULL); |
| 28 | + |
| 29 | + /** Tests that we can read a hierarchical structure with forward references*/ |
| 30 | + public void testForwardRef() throws IOException { |
| 31 | + MAPPER.readValue("{" + |
| 32 | + " \"@type\" : \"TestForwardReference$ForwardReferenceContainerClass\"," + |
| 33 | + " \"frc\" : \"willBeForwardReferenced\"," + |
| 34 | + " \"yac\" : {" + |
| 35 | + " \"@type\" : \"TestForwardReference$YetAnotherClass\"," + |
| 36 | + " \"frc\" : {" + |
| 37 | + " \"@type\" : \"One\"," + |
| 38 | + " \"id\" : \"willBeForwardReferenced\"" + |
| 39 | + " }," + |
| 40 | + " \"id\" : \"anId\"" + |
| 41 | + " }," + |
| 42 | + " \"id\" : \"ForwardReferenceContainerClass1\"" + |
| 43 | + "}", ForwardReferenceContainerClass.class); |
| 44 | + |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + @JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include= JsonTypeInfo.As.PROPERTY) |
| 49 | + public static class ForwardReferenceContainerClass |
| 50 | + { |
| 51 | + public ForwardReferenceClass frc; |
| 52 | + public YetAnotherClass yac; |
| 53 | + public String id; |
| 54 | + } |
| 55 | + |
| 56 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY) |
| 57 | + @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") |
| 58 | + @JsonSubTypes({ |
| 59 | + @JsonSubTypes.Type(value = ForwardReferenceClassOne.class, name = "One"), |
| 60 | + @JsonSubTypes.Type(value = ForwardReferenceClassTwo.class, name = "Two")}) |
| 61 | + private static abstract class ForwardReferenceClass |
| 62 | + { |
| 63 | + public String id; |
| 64 | + public void setId(String id) { |
| 65 | + this.id = id; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY) |
| 70 | + private static class YetAnotherClass |
| 71 | + { |
| 72 | + public YetAnotherClass() {} |
| 73 | + public ForwardReferenceClass frc; |
| 74 | + public String id; |
| 75 | + } |
| 76 | + |
| 77 | + public static class ForwardReferenceClassOne extends ForwardReferenceClass { } |
| 78 | + |
| 79 | + public static class ForwardReferenceClassTwo extends ForwardReferenceClass { } |
| 80 | +} |
0 commit comments