forked from FasterXML/jackson-databind
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix FasterXML#2541 (support merge polymorphic property)
- Loading branch information
JamesWang
committed
Jan 14, 2022
1 parent
abc6669
commit 713062f
Showing
2 changed files
with
73 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/test/java/com/fasterxml/jackson/databind/deser/MergePolymorphicTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.fasterxml.jackson.databind.deser; | ||
|
||
import com.fasterxml.jackson.annotation.JsonMerge; | ||
import com.fasterxml.jackson.annotation.JsonSubTypes; | ||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.BaseMapTest; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class MergePolymorphicTest extends BaseMapTest { | ||
|
||
static class Root { | ||
@JsonMerge | ||
public Child child; | ||
} | ||
|
||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME) | ||
@JsonSubTypes({ | ||
@JsonSubTypes.Type(value = ChildA.class, name = "ChildA"), | ||
@JsonSubTypes.Type(value = ChildB.class, name = "ChildB") | ||
}) | ||
static abstract class Child { | ||
} | ||
|
||
static class ChildA extends Child { | ||
public String name; | ||
} | ||
|
||
static class ChildB extends Child { | ||
public String code; | ||
} | ||
|
||
public void testPolymorphicNewObject() throws JsonProcessingException { | ||
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
Root root = mapper.readValue("{\"child\": { \"@type\": \"ChildA\", \"name\": \"I'm child A\" }}", Root.class); | ||
assertTrue(root.child instanceof ChildA); | ||
assertEquals("I'm child A", ((ChildA) root.child).name); | ||
} | ||
|
||
public void testPolymorphicPropertyCanBeMerged() throws JsonProcessingException { | ||
Root root = new Root(); | ||
ChildA childA = new ChildA(); | ||
childA.name = "I'm child A"; | ||
root.child = childA; | ||
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
mapper.readerForUpdating(root).readValue("{\"child\": { \"@type\": \"ChildA\", \"name\": \"I'm the new name\" }}"); | ||
assertTrue(root.child instanceof ChildA); | ||
assertEquals("I'm the new name", ((ChildA) root.child).name); | ||
} | ||
|
||
public void testPolymorphicPropertyTypeCanNotBeChanged() throws JsonProcessingException { | ||
Root root = new Root(); | ||
ChildA childA = new ChildA(); | ||
childA.name = "I'm child A"; | ||
root.child = childA; | ||
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
mapper.readerForUpdating(root).readValue("{\"child\": { \"@type\": \"ChildB\", \"code\": \"I'm the code\" }}"); | ||
// The polymorphic type can't be changed | ||
assertTrue(root.child instanceof ChildA); | ||
assertEquals("I'm child A", ((ChildA) root.child).name); | ||
} | ||
|
||
} |