|
| 1 | +package tools.jackson.dataformat.xml.tofix; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import jakarta.xml.bind.annotation.*; |
| 6 | + |
| 7 | +import tools.jackson.databind.AnnotationIntrospector; |
| 8 | +import tools.jackson.databind.introspect.JacksonAnnotationIntrospector; |
| 9 | +import tools.jackson.dataformat.xml.*; |
| 10 | +import tools.jackson.dataformat.xml.testutil.failure.JacksonTestFailureExpected; |
| 11 | +import tools.jackson.module.jakarta.xmlbind.JakartaXmlBindAnnotationIntrospector; |
| 12 | + |
| 13 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 14 | + |
| 15 | +// [dataformat-xml#559] JAXB @XmlValue deserializing not working with records |
| 16 | +// |
| 17 | +// Root cause: @XmlValue targets only FIELD and METHOD, not PARAMETER. |
| 18 | +// For records, Java doesn't propagate the annotation to the constructor parameter, |
| 19 | +// so Jackson can't match the property-based creator param to the @XmlValue property. |
| 20 | +// Additionally, the JAXB introspector assigns implicit name "value" to @XmlValue |
| 21 | +// properties, causing a property definition split (constructor param keeps "name", |
| 22 | +// while field/getter get renamed to "value"). |
| 23 | +public class JaxbXmlValueRecord559Test extends XmlTestUtil |
| 24 | +{ |
| 25 | + @XmlRootElement(name = "TestObject") |
| 26 | + record TestObject( |
| 27 | + @XmlValue String name, |
| 28 | + @XmlAttribute int age) {} |
| 29 | + |
| 30 | + // POJO equivalent — works fine because field/getter/setter all merge |
| 31 | + // under the JAXB-assigned "value" name (no constructor param involved) |
| 32 | + @XmlRootElement(name = "TestObject") |
| 33 | + static class TestPojo { |
| 34 | + @XmlValue |
| 35 | + public String name; |
| 36 | + @XmlAttribute |
| 37 | + public int age; |
| 38 | + } |
| 39 | + |
| 40 | + private final XmlMapper MAPPER; |
| 41 | + { |
| 42 | + JakartaXmlBindAnnotationIntrospector jaxbIntr = new JakartaXmlBindAnnotationIntrospector(); |
| 43 | + AnnotationIntrospector intr = XmlAnnotationIntrospector.Pair.instance( |
| 44 | + jaxbIntr, |
| 45 | + new JacksonAnnotationIntrospector()); |
| 46 | + MAPPER = XmlMapper.builder() |
| 47 | + .annotationIntrospector(intr) |
| 48 | + .build(); |
| 49 | + } |
| 50 | + |
| 51 | + // POJO: works |
| 52 | + @Test |
| 53 | + public void testDeserializePojo559() throws Exception { |
| 54 | + String xml = "<TestObject age=\"12\">foo</TestObject>"; |
| 55 | + TestPojo obj = MAPPER.readValue(xml, TestPojo.class); |
| 56 | + assertEquals("foo", obj.name); |
| 57 | + assertEquals(12, obj.age); |
| 58 | + } |
| 59 | + |
| 60 | + // Record: fails |
| 61 | + @JacksonTestFailureExpected |
| 62 | + @Test |
| 63 | + public void testDeserializeRecord559() throws Exception { |
| 64 | + String xml = "<TestObject age=\"12\">foo</TestObject>"; |
| 65 | + TestObject obj = MAPPER.readValue(xml, TestObject.class); |
| 66 | + assertEquals("foo", obj.name()); |
| 67 | + assertEquals(12, obj.age()); |
| 68 | + } |
| 69 | +} |
0 commit comments