|
| 1 | +package tools.jackson.dataformat.xml.deser; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 8 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 9 | + |
| 10 | +import tools.jackson.dataformat.xml.XmlTestUtil; |
| 11 | +import tools.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; |
| 12 | +import tools.jackson.dataformat.xml.annotation.JacksonXmlProperty; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 16 | + |
| 17 | +// [dataformat-xml#853] InvalidDefinitionException on classes using |
| 18 | +// @JacksonXmlElementWrapper and @JacksonXmlProperty on a field when class |
| 19 | +// has a matching args-constructor (e.g., record). |
| 20 | +public class ElementWrapperCreator853Test extends XmlTestUtil |
| 21 | +{ |
| 22 | + // Class version: field-annotated only, no @JsonCreator, single args ctor |
| 23 | + static class GroupDto { |
| 24 | + @JacksonXmlElementWrapper(localName = "users") |
| 25 | + @JacksonXmlProperty(localName = "user") |
| 26 | + private List<String> users; |
| 27 | + |
| 28 | + private String name; |
| 29 | + |
| 30 | + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) |
| 31 | + public GroupDto(@JsonProperty("users") List<String> users, |
| 32 | + @JsonProperty("name") String name) { |
| 33 | + this.users = users; |
| 34 | + this.name = name; |
| 35 | + } |
| 36 | + |
| 37 | + public List<String> getUsers() { return users; } |
| 38 | + public String getName() { return name; } |
| 39 | + } |
| 40 | + |
| 41 | + // Record version: most direct form |
| 42 | + public record GroupRecord( |
| 43 | + @JacksonXmlElementWrapper(localName = "users") |
| 44 | + @JacksonXmlProperty(localName = "user") |
| 45 | + List<String> users, |
| 46 | + String name |
| 47 | + ) {} |
| 48 | + |
| 49 | + private static final String XML = |
| 50 | + "<GroupDto>" |
| 51 | + + "<users>" |
| 52 | + + "<user>A</user>" |
| 53 | + + "<user>B</user>" |
| 54 | + + "</users>" |
| 55 | + + "<name>g1</name>" |
| 56 | + + "</GroupDto>"; |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testDeserializeClass() throws Exception |
| 60 | + { |
| 61 | + GroupDto result = newMapper().readValue(XML, GroupDto.class); |
| 62 | + assertNotNull(result.getUsers()); |
| 63 | + assertEquals(2, result.getUsers().size()); |
| 64 | + assertEquals("A", result.getUsers().get(0)); |
| 65 | + assertEquals("B", result.getUsers().get(1)); |
| 66 | + assertEquals("g1", result.getName()); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testDeserializeRecord() throws Exception |
| 71 | + { |
| 72 | + String xml = XML.replace("GroupDto", "GroupRecord"); |
| 73 | + GroupRecord result = newMapper().readValue(xml, GroupRecord.class); |
| 74 | + assertNotNull(result.users()); |
| 75 | + assertEquals(2, result.users().size()); |
| 76 | + assertEquals("A", result.users().get(0)); |
| 77 | + assertEquals("B", result.users().get(1)); |
| 78 | + assertEquals("g1", result.name()); |
| 79 | + } |
| 80 | +} |
0 commit comments