From 23f3b7ac6f61b1cf33ef67fc2c2172570d9f9d4a Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Fri, 1 Mar 2024 21:16:05 -0800 Subject: [PATCH] Try to modify an existing test to help resolve #4354 --- .../jsontype/ext/ExternalTypeId2588Test.java | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeId2588Test.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeId2588Test.java index ed70f2fa41..6a1ea4be60 100644 --- a/src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeId2588Test.java +++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/ext/ExternalTypeId2588Test.java @@ -9,18 +9,25 @@ import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase; import com.fasterxml.jackson.databind.testutil.DatabindTestUtil; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -// [databind#2588] / [databind#2610] +// [databind#2588] / [databind#2610] / [databind#4354] public class ExternalTypeId2588Test extends DatabindTestUtil { // [databind#2588] interface Animal { } - static class Cat implements Animal { } + static class Cat implements Animal { + public int lives = 9; + } public static class Dog implements Animal { } + static class Wolf implements Animal { + public boolean alive; + } + @JsonIgnoreProperties(ignoreUnknown = true) static class Pet { final String type; @@ -51,6 +58,8 @@ public String idFromValueAndType(Object value, Class suggestedType) { return "cat"; } else if (suggestedType.isAssignableFrom(Dog.class)) { return "dog"; + } else if (suggestedType.isAssignableFrom(Wolf.class)) { + return "wolf"; } return null; } @@ -71,16 +80,17 @@ public JsonTypeInfo.Id getMechanism() { } } + private final ObjectMapper MAPPER = newJsonMapper(); + // [databind#2588] @Test - public void testExternalTypeId2588() throws Exception + public void testExternalTypeId2588Read() throws Exception { - final ObjectMapper mapper = newJsonMapper(); Pet pet; // works? - pet = mapper.readValue(a2q( + pet = MAPPER.readValue(a2q( "{\n" + " 'type': 'cat',\n" + " 'animal': { },\n" + @@ -92,7 +102,7 @@ public void testExternalTypeId2588() throws Exception assertNotNull(pet); // fails: - pet = mapper.readValue(a2q( + pet = MAPPER.readValue(a2q( "{\n" + " 'animal\": { },\n" + " 'ignoredObject': {\n" + @@ -103,4 +113,11 @@ public void testExternalTypeId2588() throws Exception ), Pet.class); assertNotNull(pet); } + + @Test + public void testExternalTypeId2588Write() throws Exception + { + String json = MAPPER.writeValueAsString(new Pet("cat", new Wolf())); + assertEquals(a2q("{'animal':{'alive':false},'type':'wolf'}"), json); + } }