|
| 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 testPolymorphicFromNullToNewObject() throws JsonProcessingException { |
| 42 | + Root root = new Root(); |
| 43 | + ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 44 | + mapper.readerForUpdating(root).readValue("{\"child\": { \"@type\": \"ChildA\", \"name\": \"I'm the new name\" }}"); |
| 45 | + assertTrue(root.child instanceof ChildA); |
| 46 | + assertEquals("I'm the new name", ((ChildA) root.child).name); |
| 47 | + } |
| 48 | + |
| 49 | + public void testPolymorphicFromObjectToNull() throws JsonProcessingException { |
| 50 | + Root root = new Root(); |
| 51 | + ChildA childA = new ChildA(); |
| 52 | + childA.name = "I'm child A"; |
| 53 | + root.child = childA; |
| 54 | + ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 55 | + mapper.readerForUpdating(root).readValue("{\"child\": null }"); |
| 56 | + assertTrue(root.child == null); |
| 57 | + } |
| 58 | + |
| 59 | + public void testPolymorphicPropertyCanBeMerged() throws JsonProcessingException { |
| 60 | + Root root = new Root(); |
| 61 | + ChildA childA = new ChildA(); |
| 62 | + childA.name = "I'm child A"; |
| 63 | + root.child = childA; |
| 64 | + ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 65 | + mapper.readerForUpdating(root).readValue("{\"child\": { \"@type\": \"ChildA\", \"name\": \"I'm the new name\" }}"); |
| 66 | + assertTrue(root.child instanceof ChildA); |
| 67 | + assertEquals("I'm the new name", ((ChildA) root.child).name); |
| 68 | + } |
| 69 | + |
| 70 | + public void testPolymorphicPropertyTypeCanNotBeChanged() throws JsonProcessingException { |
| 71 | + Root root = new Root(); |
| 72 | + ChildA childA = new ChildA(); |
| 73 | + childA.name = "I'm child A"; |
| 74 | + root.child = childA; |
| 75 | + ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
| 76 | + mapper.readerForUpdating(root).readValue("{\"child\": { \"@type\": \"ChildB\", \"code\": \"I'm the code\" }}"); |
| 77 | + // The polymorphic type can't be changed |
| 78 | + assertTrue(root.child instanceof ChildA); |
| 79 | + assertEquals("I'm child A", ((ChildA) root.child).name); |
| 80 | + } |
| 81 | + |
| 82 | +} |
0 commit comments