|
| 1 | +package tools.jackson.dataformat.xml.tofix; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 8 | + |
| 9 | +import tools.jackson.dataformat.xml.XmlTestUtil; |
| 10 | +import tools.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; |
| 11 | +import tools.jackson.dataformat.xml.annotation.JacksonXmlProperty; |
| 12 | +import tools.jackson.dataformat.xml.testutil.failure.JacksonTestFailureExpected; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 15 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 16 | + |
| 17 | +// [dataformat-xml#795] Element wrapper not applied with creator-based deserialization |
| 18 | +public class ElementWrapperWithCreator795Test extends XmlTestUtil |
| 19 | +{ |
| 20 | + static class HttpHeader { |
| 21 | + private String name; |
| 22 | + private String value; |
| 23 | + |
| 24 | + protected HttpHeader() { } |
| 25 | + public HttpHeader(String name, String value) { |
| 26 | + this.name = name; |
| 27 | + this.value = value; |
| 28 | + } |
| 29 | + |
| 30 | + public String getName() { return name; } |
| 31 | + public void setName(String n) { name = n; } |
| 32 | + public String getValue() { return value; } |
| 33 | + public void setValue(String v) { value = v; } |
| 34 | + } |
| 35 | + |
| 36 | + // Creator-based (immutable) class: wrapper + creator triggers name mismatch |
| 37 | + static class Config { |
| 38 | + @JacksonXmlElementWrapper(localName = "httpHeaders") |
| 39 | + @JacksonXmlProperty(localName = "property") |
| 40 | + final List<HttpHeader> httpHeaders; |
| 41 | + |
| 42 | + @JsonCreator(mode = JsonCreator.Mode.PROPERTIES) |
| 43 | + public Config( |
| 44 | + @JacksonXmlElementWrapper(localName = "httpHeaders") |
| 45 | + @JacksonXmlProperty(localName = "property") |
| 46 | + List<HttpHeader> httpHeaders) |
| 47 | + { |
| 48 | + this.httpHeaders = httpHeaders; |
| 49 | + } |
| 50 | + |
| 51 | + public List<HttpHeader> getHttpHeaders() { return httpHeaders; } |
| 52 | + } |
| 53 | + |
| 54 | + private static final String XML = |
| 55 | + "<Config>" |
| 56 | + + "<httpHeaders>" |
| 57 | + + "<property>" |
| 58 | + + "<name>X-JFrog-Art-Api</name>" |
| 59 | + + "<value>myApiToken</value>" |
| 60 | + + "</property>" |
| 61 | + + "</httpHeaders>" |
| 62 | + + "</Config>"; |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testSerializeWithWrapper() throws Exception |
| 66 | + { |
| 67 | + Config config = new Config(List.of(new HttpHeader("X-JFrog-Art-Api", "myApiToken"))); |
| 68 | + String xml = newMapper().writeValueAsString(config); |
| 69 | + assertEquals(XML, xml); |
| 70 | + } |
| 71 | + |
| 72 | + // Deserialization fails: XmlBeanDeserializerModifier renames property to wrapper name |
| 73 | + // ("httpHeaders") but creator property retains original name ("property"), causing mismatch. |
| 74 | + @JacksonTestFailureExpected |
| 75 | + @Test |
| 76 | + public void testDeserializeWithWrapper() throws Exception |
| 77 | + { |
| 78 | + Config result = newMapper().readValue(XML, Config.class); |
| 79 | + assertNotNull(result.getHttpHeaders()); |
| 80 | + assertEquals(1, result.getHttpHeaders().size()); |
| 81 | + assertEquals("X-JFrog-Art-Api", result.getHttpHeaders().get(0).getName()); |
| 82 | + assertEquals("myApiToken", result.getHttpHeaders().get(0).getValue()); |
| 83 | + } |
| 84 | +} |
0 commit comments