|
| 1 | +package tools.jackson.dataformat.xml.misc; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import com.fasterxml.jackson.annotation.JsonIdentityInfo; |
| 6 | +import com.fasterxml.jackson.annotation.ObjectIdGenerators; |
| 7 | + |
| 8 | +import tools.jackson.dataformat.xml.XmlMapper; |
| 9 | +import tools.jackson.dataformat.xml.XmlTestUtil; |
| 10 | +import tools.jackson.dataformat.xml.annotation.JacksonXmlProperty; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.*; |
| 13 | + |
| 14 | +// [dataformat-xml#248]: @JacksonXmlProperty(isAttribute=true) with @JsonIdentityInfo |
| 15 | +// throws "Trying to write an attribute when there is no open start element" |
| 16 | +public class ObjectIdWithAttribute248Test extends XmlTestUtil |
| 17 | +{ |
| 18 | + @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "name") |
| 19 | + static class NodeA { |
| 20 | + @JacksonXmlProperty(isAttribute = true) |
| 21 | + public String name; |
| 22 | + |
| 23 | + public NodeB ref; |
| 24 | + |
| 25 | + public NodeA() { } |
| 26 | + public NodeA(String name) { this.name = name; } |
| 27 | + } |
| 28 | + |
| 29 | + static class NodeB { |
| 30 | + public NodeA ref; |
| 31 | + |
| 32 | + public NodeB() { } |
| 33 | + public NodeB(NodeA a) { this.ref = a; } |
| 34 | + } |
| 35 | + |
| 36 | + private final XmlMapper MAPPER = newMapper(); |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testSerializeAttributeWithIdentityInfo() throws Exception |
| 40 | + { |
| 41 | + NodeA a = new NodeA("test"); |
| 42 | + NodeB b = new NodeB(a); |
| 43 | + a.ref = b; |
| 44 | + |
| 45 | + String xml = MAPPER.writeValueAsString(a); |
| 46 | + assertNotNull(xml); |
| 47 | + assertTrue(xml.contains("name=\"test\""), |
| 48 | + "Expected 'name' as XML attribute, got: " + xml); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testRoundTripAttributeWithIdentityInfo() throws Exception |
| 53 | + { |
| 54 | + NodeA a = new NodeA("test"); |
| 55 | + NodeB b = new NodeB(a); |
| 56 | + a.ref = b; |
| 57 | + |
| 58 | + String xml = MAPPER.writeValueAsString(a); |
| 59 | + NodeA result = MAPPER.readValue(xml, NodeA.class); |
| 60 | + |
| 61 | + assertNotNull(result); |
| 62 | + assertEquals("test", result.name); |
| 63 | + assertNotNull(result.ref); |
| 64 | + assertSame(result, result.ref.ref); |
| 65 | + } |
| 66 | +} |
0 commit comments