|
| 1 | +package tools.jackson.databind.jsontype.ext; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.*; |
| 6 | + |
| 7 | +import tools.jackson.databind.MapperFeature; |
| 8 | +import tools.jackson.databind.ObjectMapper; |
| 9 | +import tools.jackson.databind.ObjectReader; |
| 10 | +import tools.jackson.databind.testutil.DatabindTestUtil; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.*; |
| 13 | + |
| 14 | +// [databind#5958]: When a POJO uses a property-based @JsonCreator together with |
| 15 | +// an EXTERNAL_PROPERTY type id whose `visible=true` settable property is |
| 16 | +// restricted by @JsonView, the deserializer must skip that type-id property in |
| 17 | +// views where it is not visible (instead of forwarding the type id into the |
| 18 | +// external type handler). |
| 19 | +public class ExternalTypeIdView5958Test extends DatabindTestUtil |
| 20 | +{ |
| 21 | + static class Views { |
| 22 | + static class Public { } |
| 23 | + static class Internal { } |
| 24 | + } |
| 25 | + |
| 26 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME) |
| 27 | + @JsonSubTypes({ |
| 28 | + @JsonSubTypes.Type(value = Dog.class, name = "dog"), |
| 29 | + @JsonSubTypes.Type(value = Cat.class, name = "cat") |
| 30 | + }) |
| 31 | + static abstract class Animal { |
| 32 | + public String name; |
| 33 | + } |
| 34 | + |
| 35 | + @JsonTypeName("dog") |
| 36 | + static class Dog extends Animal { } |
| 37 | + |
| 38 | + @JsonTypeName("cat") |
| 39 | + static class Cat extends Animal { } |
| 40 | + |
| 41 | + // Default impl used when the type id is filtered out by view processing. |
| 42 | + @JsonTypeName("unknown") |
| 43 | + static class UnknownAnimal extends Animal { } |
| 44 | + |
| 45 | + static class Container { |
| 46 | + // External type id, exposed (`visible=true`) as a settable property, |
| 47 | + // restricted to the Internal view. |
| 48 | + @JsonView(Views.Internal.class) |
| 49 | + public String petType; |
| 50 | + |
| 51 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, |
| 52 | + include = JsonTypeInfo.As.EXTERNAL_PROPERTY, |
| 53 | + property = "petType", |
| 54 | + visible = true, |
| 55 | + defaultImpl = UnknownAnimal.class) |
| 56 | + @JsonSubTypes({ |
| 57 | + @JsonSubTypes.Type(value = Dog.class, name = "dog"), |
| 58 | + @JsonSubTypes.Type(value = Cat.class, name = "cat") |
| 59 | + }) |
| 60 | + @JsonView(Views.Internal.class) |
| 61 | + public Animal pet; |
| 62 | + |
| 63 | + public String label; |
| 64 | + |
| 65 | + // Property-based creator forces the |
| 66 | + // deserializeUsingPropertyBasedWithExternalTypeId code path. |
| 67 | + @JsonCreator |
| 68 | + public Container(@JsonProperty("label") String label) { |
| 69 | + this.label = label; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private final ObjectMapper MAPPER = jsonMapperBuilder() |
| 74 | + // Properties without a @JsonView are visible in every view; ones |
| 75 | + // with @JsonView are only visible in (a subtype of) that view. |
| 76 | + .enable(MapperFeature.DEFAULT_VIEW_INCLUSION) |
| 77 | + .build(); |
| 78 | + |
| 79 | + private static final String JSON = a2q( |
| 80 | + "{'label':'box','petType':'dog','pet':{'name':'Rex'}}"); |
| 81 | + |
| 82 | + @Test |
| 83 | + public void typeIdHonoredInVisibleView() throws Exception |
| 84 | + { |
| 85 | + ObjectReader reader = MAPPER.readerFor(Container.class) |
| 86 | + .withView(Views.Internal.class); |
| 87 | + Container c = reader.readValue(JSON); |
| 88 | + |
| 89 | + assertEquals("box", c.label); |
| 90 | + assertEquals("dog", c.petType); |
| 91 | + assertNotNull(c.pet); |
| 92 | + assertInstanceOf(Dog.class, c.pet); |
| 93 | + assertEquals("Rex", c.pet.name); |
| 94 | + } |
| 95 | + |
| 96 | + // The core [databind#5958] assertion: under a view where the type-id property is |
| 97 | + // not visible, the value in the JSON must NOT be forwarded into the |
| 98 | + // external type handler. Without the patch, `petType:"dog"` would be picked |
| 99 | + // up via `_propsByIndex`/`handleTypePropertyValue` even though the field is |
| 100 | + // restricted to a different view, and `pet` would be deserialized as `Dog`. |
| 101 | + // With the patch, the type-id property is skipped and `defaultImpl` kicks in. |
| 102 | + @Test |
| 103 | + public void typeIdSkippedInRestrictedView() throws Exception |
| 104 | + { |
| 105 | + ObjectReader reader = MAPPER.readerFor(Container.class) |
| 106 | + .withView(Views.Public.class); |
| 107 | + Container c = reader.readValue(JSON); |
| 108 | + |
| 109 | + assertEquals("box", c.label); |
| 110 | + assertNull(c.petType, "petType is @JsonView-restricted; must be skipped under Public view"); |
| 111 | + assertNotNull(c.pet); |
| 112 | + assertInstanceOf(UnknownAnimal.class, c.pet, |
| 113 | + "type id must not leak across views; defaultImpl should be used instead of Dog"); |
| 114 | + } |
| 115 | +} |
0 commit comments