|
| 1 | +package com.fasterxml.jackson.databind.jsontype.ext; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.*; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.databind.*; |
| 6 | + |
| 7 | +public class ExternalTypeIdWithUnwrapped2039Test extends BaseMapTest |
| 8 | +{ |
| 9 | + static class MainType2039 { |
| 10 | + public String text; |
| 11 | + |
| 12 | + @JsonUnwrapped public Wrapped2039 wrapped; |
| 13 | + |
| 14 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "subtype") |
| 15 | + @JsonSubTypes({ @JsonSubTypes.Type(value = SubA2039.class, name = "SubA") }) |
| 16 | + public SubType2039 sub; |
| 17 | + |
| 18 | + public void setSub(SubType2039 s) { |
| 19 | + sub = s; |
| 20 | + } |
| 21 | + |
| 22 | + public void setWrapped(Wrapped2039 w) { |
| 23 | + wrapped = w; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + static class Wrapped2039 { |
| 28 | + public String wrapped; |
| 29 | + } |
| 30 | + |
| 31 | + public static class SubType2039 { } |
| 32 | + |
| 33 | + public static class SubA2039 extends SubType2039 { |
| 34 | + @JsonProperty public boolean bool; |
| 35 | + } |
| 36 | + |
| 37 | + public void testExternalWithUnwrapped2039() throws Exception |
| 38 | + { |
| 39 | + final ObjectMapper mapper = newObjectMapper(); |
| 40 | + |
| 41 | + final String json = aposToQuotes("{\n" |
| 42 | + +"'text': 'this is A',\n" |
| 43 | + +"'wrapped': 'yes',\n" |
| 44 | + +"'subtype': 'SubA',\n" |
| 45 | + +"'sub': {\n" |
| 46 | + +" 'bool': true\n" |
| 47 | + +"}\n" |
| 48 | + +"}"); |
| 49 | + final MainType2039 main = mapper.readValue(json, MainType2039.class); |
| 50 | + |
| 51 | + assertEquals("this is A", main.text); |
| 52 | + assertEquals("yes", main.wrapped.wrapped); |
| 53 | + |
| 54 | + assertNotNull(main.sub); |
| 55 | + assertEquals(SubA2039.class, main.sub.getClass()); // <- fails here |
| 56 | + assertEquals(true, ((SubA2039) main.sub).bool); |
| 57 | + } |
| 58 | +} |
0 commit comments