-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
3942c2e
commit 91782dc
Showing
2 changed files
with
91 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
82 changes: 82 additions & 0 deletions
82
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,82 @@ | ||
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 testPolymorphicFromNullToNewObject() throws JsonProcessingException { | ||
Root root = new Root(); | ||
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 testPolymorphicFromObjectToNull() 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\": null }"); | ||
assertTrue(root.child == null); | ||
} | ||
|
||
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); | ||
} | ||
|
||
} |