|
| 1 | +package com.fasterxml.jackson.databind.tofix; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.*; |
| 6 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 7 | +import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; |
| 8 | +import com.fasterxml.jackson.databind.testutil.failure.JacksonTestFailureExpected; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 11 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 12 | + |
| 13 | +import static com.fasterxml.jackson.annotation.JsonProperty.Access.READ_ONLY; |
| 14 | + |
| 15 | +// [databind#4792] JsonUnwrapped throwing "Unrecognized field" after upgrade to 2.18 |
| 16 | +public class PolymorphicDeserWithJsonUnwrapped4792Test |
| 17 | + extends DatabindTestUtil |
| 18 | +{ |
| 19 | + |
| 20 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "name") |
| 21 | + @JsonSubTypes({ |
| 22 | + @JsonSubTypes.Type(value = SubA.class, name = "A"), |
| 23 | + }) |
| 24 | + interface Parent { } |
| 25 | + |
| 26 | + static class SubA implements Parent { |
| 27 | + @JsonUnwrapped |
| 28 | + @JsonProperty(access = READ_ONLY) |
| 29 | + private Model model; |
| 30 | + |
| 31 | + @JsonCreator |
| 32 | + public SubA(@JsonProperty("model") Model model) { |
| 33 | + this.model = model; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + static class Model { |
| 38 | + public String name; |
| 39 | + } |
| 40 | + |
| 41 | + public static class Wrapper { |
| 42 | + public Parent model; |
| 43 | + } |
| 44 | + |
| 45 | + private final ObjectMapper objectMapper = newJsonMapper(); |
| 46 | + |
| 47 | + @JacksonTestFailureExpected |
| 48 | + @Test |
| 49 | + public void testMainTest() |
| 50 | + throws Exception |
| 51 | + { |
| 52 | + Wrapper w = objectMapper.readValue(a2q("{'model':{'name':'A','name': 'Rick'}}"), Wrapper.class); |
| 53 | + |
| 54 | + assertInstanceOf(SubA.class, w.model); |
| 55 | + assertInstanceOf(Model.class, ((SubA) w.model).model); |
| 56 | + assertEquals("Rick", ((SubA) w.model).model.name); |
| 57 | + } |
| 58 | + |
| 59 | +} |
0 commit comments