|
| 1 | +package com.fasterxml.jackson.databind.deser; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonMerge; |
| 4 | +import com.fasterxml.jackson.annotation.JsonSubTypes; |
| 5 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
| 6 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 7 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 8 | +import com.fasterxml.jackson.databind.DeserializationFeature; |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 10 | + |
| 11 | +public class MergePolymorphicTest extends BaseMapTest { |
| 12 | + |
| 13 | + static class Root { |
| 14 | + @JsonMerge |
| 15 | + public Child child; |
| 16 | + } |
| 17 | + |
| 18 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME) |
| 19 | + @JsonSubTypes({ |
| 20 | + @JsonSubTypes.Type(value = ChildA.class, name = "ChildA"), |
| 21 | + @JsonSubTypes.Type(value = ChildB.class, name = "ChildB") |
| 22 | + }) |
| 23 | + static abstract class Child { |
| 24 | + } |
| 25 | + |
| 26 | + static class ChildA extends Child { |
| 27 | + public String name; |
| 28 | + } |
| 29 | + |
| 30 | + static class ChildB extends Child { |
| 31 | + public String code; |
| 32 | + } |
| 33 | + |
| 34 | + public void testPolymorphicNewObject() throws JsonProcessingException { |
| 35 | + ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 36 | + Root root = mapper.readValue("{\"child\": { \"@type\": \"ChildA\", \"name\": \"I'm child A\" }}", Root.class); |
| 37 | + assertTrue(root.child instanceof ChildA); |
| 38 | + assertEquals("I'm child A", ((ChildA) root.child).name); |
| 39 | + } |
| 40 | + |
| 41 | + public void testPolymorphicPropertyCanBeMerged() throws JsonProcessingException { |
| 42 | + Root root = new Root(); |
| 43 | + ChildA childA = new ChildA(); |
| 44 | + childA.name = "I'm child A"; |
| 45 | + root.child = childA; |
| 46 | + ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 47 | + mapper.readerForUpdating(root).readValue("{\"child\": { \"@type\": \"ChildA\", \"name\": \"I'm the new name\" }}"); |
| 48 | + assertTrue(root.child instanceof ChildA); |
| 49 | + assertEquals("I'm the new name", ((ChildA) root.child).name); |
| 50 | + } |
| 51 | + |
| 52 | + public void testPolymorphicPropertyTypeCanNotBeChanged() throws JsonProcessingException { |
| 53 | + Root root = new Root(); |
| 54 | + ChildA childA = new ChildA(); |
| 55 | + childA.name = "I'm child A"; |
| 56 | + root.child = childA; |
| 57 | + ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 58 | + mapper.readerForUpdating(root).readValue("{\"child\": { \"@type\": \"ChildB\", \"code\": \"I'm the code\" }}"); |
| 59 | + // The polymorphic type can't be changed |
| 60 | + assertTrue(root.child instanceof ChildA); |
| 61 | + assertEquals("I'm child A", ((ChildA) root.child).name); |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments