|
| 1 | +package com.fasterxml.jackson.databind.tofix; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 8 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 9 | +import com.fasterxml.jackson.annotation.JsonSubTypes; |
| 10 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
| 11 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 12 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 13 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 14 | +import com.fasterxml.jackson.databind.testutil.failure.JacksonTestFailureExpected; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 17 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 18 | + |
| 19 | +// [databind#4742] Deserialization with Builder, External type id, |
| 20 | +// @JsonCreator not yet implemented |
| 21 | +public class JacksonBuilderCreatorSubtype4742Test |
| 22 | + extends DatabindTestUtil |
| 23 | +{ |
| 24 | + |
| 25 | + public static class Animals { |
| 26 | + @JsonProperty("animals") |
| 27 | + public List<Animal> animals; |
| 28 | + } |
| 29 | + |
| 30 | + @JsonDeserialize(builder = Animal.Builder.class) |
| 31 | + public static class Animal { |
| 32 | + @JsonProperty("kind") |
| 33 | + public String kind; |
| 34 | + |
| 35 | + @JsonProperty("properties") |
| 36 | + public AnimalProperties properties; |
| 37 | + |
| 38 | + @Override |
| 39 | + public String toString() { |
| 40 | + return "Animal{kind='" + kind + '\'' + ", properties=" + properties + '}'; |
| 41 | + } |
| 42 | + |
| 43 | + public static abstract class Builder { |
| 44 | + @JsonProperty("kind") |
| 45 | + public abstract Builder kind(String kind); |
| 46 | + |
| 47 | + @JsonProperty("properties") |
| 48 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "kind") |
| 49 | + @JsonSubTypes({ |
| 50 | + @JsonSubTypes.Type(name = "bird", value = BirdProperties.class), |
| 51 | + @JsonSubTypes.Type(name = "mammal", value = MammalProperties.class) |
| 52 | + }) |
| 53 | + public abstract Builder properties(AnimalProperties properties); |
| 54 | + |
| 55 | + @JsonCreator |
| 56 | + public static BuilderImpl create() { |
| 57 | + return new BuilderImpl(); |
| 58 | + } |
| 59 | + |
| 60 | + public abstract Animal build(); |
| 61 | + } |
| 62 | + |
| 63 | + public static class BuilderImpl extends Builder { |
| 64 | + private String kind; |
| 65 | + private AnimalProperties properties; |
| 66 | + |
| 67 | + public BuilderImpl kind(String kind) { |
| 68 | + this.kind = kind; |
| 69 | + return this; |
| 70 | + } |
| 71 | + |
| 72 | + public BuilderImpl properties(AnimalProperties properties) { |
| 73 | + this.properties = properties; |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public Animal build() { |
| 79 | + final Animal animal = new Animal(); |
| 80 | + animal.kind = kind; |
| 81 | + animal.properties = properties; |
| 82 | + return animal; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public interface AnimalProperties { |
| 88 | + } |
| 89 | + |
| 90 | + public static class MammalProperties implements AnimalProperties { |
| 91 | + @JsonProperty("num_teeth") |
| 92 | + public int teeth; |
| 93 | + |
| 94 | + @Override |
| 95 | + public String toString() { |
| 96 | + return "MammalProperties{teeth=" + teeth + '}'; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + public static class BirdProperties implements AnimalProperties { |
| 101 | + @JsonProperty("color") |
| 102 | + public String color; |
| 103 | + |
| 104 | + @Override |
| 105 | + public String toString() { |
| 106 | + return "BirdProperties{color='" + color + '\'' + '}'; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + final ObjectMapper MAPPER = newJsonMapper(); |
| 111 | + |
| 112 | + @JacksonTestFailureExpected |
| 113 | + @Test |
| 114 | + public void testDeser() |
| 115 | + throws Exception |
| 116 | + { |
| 117 | + final Animals animals = MAPPER.readValue( |
| 118 | + "{\n" + |
| 119 | + " \"animals\": [\n" + |
| 120 | + " {\"kind\": \"bird\", \"properties\": {\"color\": \"yellow\"}},\n" + |
| 121 | + " {\"kind\": \"mammal\", \"properties\": {\"num_teeth\": 2}}\n" + |
| 122 | + " ]\n" + |
| 123 | + "}", Animals.class); |
| 124 | + |
| 125 | + assertEquals(2, animals.animals.size()); |
| 126 | + assertInstanceOf(BirdProperties.class, animals.animals.get(0).properties); |
| 127 | + assertInstanceOf(MammalProperties.class, animals.animals.get(1).properties); |
| 128 | + |
| 129 | + } |
| 130 | +} |
0 commit comments