|
| 1 | +package com.fasterxml.jackson.failing; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.*; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.databind.*; |
| 8 | +import com.fasterxml.jackson.databind.annotation.*; |
| 9 | +import com.fasterxml.jackson.databind.jsontype.TypeIdResolver; |
| 10 | +import com.fasterxml.jackson.databind.type.SimpleType; |
| 11 | + |
| 12 | +public class TestMartinsExternalId extends BaseMapTest |
| 13 | +{ |
| 14 | + public void testExternal() throws IOException |
| 15 | + { |
| 16 | + ObjectMapper mapper = new ObjectMapper(); |
| 17 | + Parent parent = mapper.readValue("{\"type\":\"a\",\"child\":{\"value\":\"foo\"}}", Parent.class); |
| 18 | + assertNotNull(parent); |
| 19 | + } |
| 20 | + |
| 21 | + @JsonTypeIdResolver(Resolver.class) |
| 22 | + public static class Parent |
| 23 | + { |
| 24 | + @JsonProperty |
| 25 | + public String type; |
| 26 | + |
| 27 | + @JsonTypeIdResolver(Resolver.class) |
| 28 | + @JsonProperty |
| 29 | + @JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, include= JsonTypeInfo.As.EXTERNAL_PROPERTY, property="type" |
| 30 | + , visible = true |
| 31 | + ) |
| 32 | + public Child child; |
| 33 | + } |
| 34 | + |
| 35 | + public static interface Child { } |
| 36 | + |
| 37 | + @JsonTypeIdResolver(Resolver.class) |
| 38 | + public static class Child1 implements Child |
| 39 | + { |
| 40 | + @JsonProperty |
| 41 | + public String value; |
| 42 | + } |
| 43 | + |
| 44 | + public static class Child2 |
| 45 | + implements Child |
| 46 | + { |
| 47 | + @JsonProperty |
| 48 | + public String value; |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + public static class Resolver implements TypeIdResolver |
| 53 | + { |
| 54 | + public Resolver() { } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void init(JavaType baseType) { } |
| 58 | + |
| 59 | + @Override |
| 60 | + public String idFromValue(Object value) |
| 61 | + { |
| 62 | + if (value instanceof Child1) { |
| 63 | + return "a"; |
| 64 | + } |
| 65 | + else if (value instanceof Child2) { |
| 66 | + return "b"; |
| 67 | + } |
| 68 | + |
| 69 | + throw new IllegalArgumentException(); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public String idFromValueAndType(Object value, Class<?> suggestedType) |
| 74 | + { |
| 75 | + return idFromValue(value); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public String idFromBaseType() |
| 80 | + { |
| 81 | + throw new UnsupportedOperationException(); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public JavaType typeFromId(String id) |
| 86 | + { |
| 87 | + if (id.equals("a")) { |
| 88 | + return SimpleType.construct(Child1.class); |
| 89 | + } |
| 90 | + else if (id.equals("b")) { |
| 91 | + return SimpleType.construct(Child2.class); |
| 92 | + } |
| 93 | + throw new IllegalArgumentException(); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public JsonTypeInfo.Id getMechanism() |
| 98 | + { |
| 99 | + return JsonTypeInfo.Id.CUSTOM; |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments