Skip to content

Commit

Permalink
Try to modify an existing test to help resolve #4354
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Mar 2, 2024
1 parent 5ff1cb0 commit 23f3b7a
Showing 1 changed file with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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" +
Expand All @@ -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" +
Expand All @@ -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);
}
}

0 comments on commit 23f3b7a

Please sign in to comment.