|
| 1 | +package tools.jackson.dataformat.xml.jaxb; |
| 2 | + |
| 3 | +import java.math.BigDecimal; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import jakarta.xml.bind.annotation.*; |
| 8 | + |
| 9 | +import tools.jackson.databind.AnnotationIntrospector; |
| 10 | +import tools.jackson.databind.introspect.JacksonAnnotationIntrospector; |
| 11 | +import tools.jackson.dataformat.xml.*; |
| 12 | +import tools.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationIntrospector; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | + |
| 16 | +// [dataformat-xml#510] @XmlValue and @XmlAttribute ignored after upgrade |
| 17 | +public class JakartaBindXmlValueWithAttribute510Test extends XmlTestUtil |
| 18 | +{ |
| 19 | + @XmlAccessorType(XmlAccessType.FIELD) |
| 20 | + @XmlType(name = "Test", propOrder = { "value" }) |
| 21 | + @XmlRootElement(name = "Reproducer") |
| 22 | + static class Reproducer510 { |
| 23 | + @XmlValue |
| 24 | + public BigDecimal value; |
| 25 | + @XmlAttribute(name = "node", required = true) |
| 26 | + public String node; |
| 27 | + |
| 28 | + public Reproducer510() { } |
| 29 | + public Reproducer510(BigDecimal value, String node) { |
| 30 | + this.value = value; |
| 31 | + this.node = node; |
| 32 | + } |
| 33 | + |
| 34 | + public BigDecimal getValue() { return value; } |
| 35 | + public void setValue(BigDecimal value) { this.value = value; } |
| 36 | + public String getNode() { return node; } |
| 37 | + public void setNode(String node) { this.node = node; } |
| 38 | + } |
| 39 | + |
| 40 | + // [dataformat-xml#510]: Serialization |
| 41 | + @Test |
| 42 | + public void testSerializationWithJaxbAnnotations510() throws Exception { |
| 43 | + // Use Jakarta JAXB with workaround for implicit "value" name |
| 44 | + final XmlMapper mapper = XmlMapper.builder() |
| 45 | + .addModule(new tools.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationModule() |
| 46 | + .setNameUsedForXmlValue(null)) |
| 47 | + .build(); |
| 48 | + |
| 49 | + Reproducer510 obj = new Reproducer510(new BigDecimal("1.123"), "myNode"); |
| 50 | + String xml = mapper.writeValueAsString(obj); |
| 51 | + // Expect: <Reproducer node="myNode">1.123</Reproducer> |
| 52 | + assertEquals("<Reproducer node=\"myNode\">1.123</Reproducer>", xml); |
| 53 | + } |
| 54 | + |
| 55 | + // [dataformat-xml#510]: Deserialization |
| 56 | + @Test |
| 57 | + public void testDeserializationWithJaxbAnnotations510() throws Exception { |
| 58 | + final XmlMapper mapper = XmlMapper.builder() |
| 59 | + .addModule(new tools.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationModule() |
| 60 | + .setNameUsedForXmlValue(null)) |
| 61 | + .build(); |
| 62 | + |
| 63 | + String xml = "<Reproducer node=\"myNode\">1.123</Reproducer>"; |
| 64 | + Reproducer510 obj = mapper.readValue(xml, Reproducer510.class); |
| 65 | + assertEquals(new BigDecimal("1.123"), obj.getValue()); |
| 66 | + assertEquals("myNode", obj.getNode()); |
| 67 | + } |
| 68 | + |
| 69 | + // Also verify with manual introspector pair setup (common pattern) |
| 70 | + @Test |
| 71 | + public void testWithIntrospectorPair510() throws Exception { |
| 72 | + JakartaXmlBindAnnotationIntrospector jaxbIntr = |
| 73 | + new JakartaXmlBindAnnotationIntrospector(); |
| 74 | + jaxbIntr.setNameUsedForXmlValue(null); |
| 75 | + AnnotationIntrospector intr = XmlAnnotationIntrospector.Pair.instance( |
| 76 | + jaxbIntr, |
| 77 | + new JacksonAnnotationIntrospector()); |
| 78 | + XmlMapper mapper = XmlMapper.builder() |
| 79 | + .annotationIntrospector(intr) |
| 80 | + .build(); |
| 81 | + |
| 82 | + Reproducer510 obj = new Reproducer510(new BigDecimal("1.123"), "myNode"); |
| 83 | + String xml = mapper.writeValueAsString(obj); |
| 84 | + assertEquals("<Reproducer node=\"myNode\">1.123</Reproducer>", xml); |
| 85 | + |
| 86 | + // Round-trip |
| 87 | + Reproducer510 back = mapper.readValue(xml, Reproducer510.class); |
| 88 | + assertEquals(new BigDecimal("1.123"), back.getValue()); |
| 89 | + assertEquals("myNode", back.getNode()); |
| 90 | + } |
| 91 | +} |
0 commit comments