|
| 1 | +package tools.jackson.databind; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Optional; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.annotation.JsonSubTypes; |
| 9 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
| 10 | +import com.fasterxml.jackson.annotation.JsonTypeName; |
| 11 | + |
| 12 | +import tools.jackson.core.type.TypeReference; |
| 13 | + |
| 14 | +import tools.jackson.databind.testutil.DatabindTestUtil; |
| 15 | + |
| 16 | +import static org.junit.jupiter.api.Assertions.*; |
| 17 | + |
| 18 | +// [databind#5710]: ObjectWriter.valueToTree() generates incomplete tree model |
| 19 | +// for polymorphic types when wrapped in container/reference types |
| 20 | +class ObjectWriterValueToTree5710Test extends DatabindTestUtil |
| 21 | +{ |
| 22 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "@type") |
| 23 | + @JsonSubTypes({ |
| 24 | + @JsonSubTypes.Type(name = "subtype", value = Subtype.class) |
| 25 | + }) |
| 26 | + interface Supertype {} |
| 27 | + |
| 28 | + @JsonTypeName("subtype") |
| 29 | + static class Subtype implements Supertype { |
| 30 | + public String content = "hello"; |
| 31 | + } |
| 32 | + |
| 33 | + private final ObjectMapper MAPPER = newJsonMapper(); |
| 34 | + |
| 35 | + // Direct polymorphic type: valueToTree() should include "@type" property |
| 36 | + @Test |
| 37 | + void valueToTreeDirectPolymorphicType() throws Exception |
| 38 | + { |
| 39 | + ObjectWriter writer = MAPPER.writerFor(new TypeReference<Supertype>() {}); |
| 40 | + Subtype value = new Subtype(); |
| 41 | + |
| 42 | + JsonNode tree = writer.valueToTree(value); |
| 43 | + |
| 44 | + assertTrue(tree.has("@type"), "Expected '@type' property in tree for direct polymorphic type"); |
| 45 | + assertEquals("subtype", tree.get("@type").asString()); |
| 46 | + assertEquals("hello", tree.get("content").asString()); |
| 47 | + } |
| 48 | + |
| 49 | + // List-wrapped polymorphic type: valueToTree() should include "@type" in array elements |
| 50 | + @Test |
| 51 | + void valueToTreeListWrappedPolymorphicType() throws Exception |
| 52 | + { |
| 53 | + ObjectWriter writer = MAPPER.writerFor(new TypeReference<List<Supertype>>() {}); |
| 54 | + List<Subtype> value = List.of(new Subtype()); |
| 55 | + |
| 56 | + // writeValueAsString() works correctly |
| 57 | + String json = writer.writeValueAsString(value); |
| 58 | + assertTrue(json.contains("\"@type\""), "writeValueAsString() should include '@type': " + json); |
| 59 | + |
| 60 | + // valueToTree() should match writeValueAsString() |
| 61 | + assertEquals(MAPPER.readTree(json), writer.valueToTree(value), |
| 62 | + "valueToTree() should match writeValueAsString() for List<Supertype>"); |
| 63 | + } |
| 64 | + |
| 65 | + // Optional-wrapped polymorphic type: valueToTree() should include "@type" property |
| 66 | + @Test |
| 67 | + void valueToTreeOptionalWrappedPolymorphicType() throws Exception |
| 68 | + { |
| 69 | + ObjectWriter writer = MAPPER.writerFor(new TypeReference<Optional<Supertype>>() {}); |
| 70 | + Optional<Subtype> value = Optional.of(new Subtype()); |
| 71 | + |
| 72 | + // writeValueAsString() works correctly |
| 73 | + String json = writer.writeValueAsString(value); |
| 74 | + assertTrue(json.contains("\"@type\""), "writeValueAsString() should include '@type': " + json); |
| 75 | + |
| 76 | + // valueToTree() should match writeValueAsString() |
| 77 | + assertEquals(MAPPER.readTree(json), writer.valueToTree(value), |
| 78 | + "valueToTree() should match writeValueAsString() for Optional<Supertype>"); |
| 79 | + } |
| 80 | +} |
0 commit comments