|
| 1 | +package tools.jackson.dataformat.xml.ser; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.annotation.*; |
| 8 | + |
| 9 | +import tools.jackson.dataformat.xml.XmlMapper; |
| 10 | +import tools.jackson.dataformat.xml.XmlTestUtil; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.*; |
| 13 | + |
| 14 | +// [dataformat-xml#629] @JsonAnySetter mangles nested XML elements and attributes |
| 15 | +public class AnySetterNestedXml629Test extends XmlTestUtil |
| 16 | +{ |
| 17 | + static class PojoWith629 { |
| 18 | + @JsonProperty("id") |
| 19 | + public String id; |
| 20 | + |
| 21 | + @JsonAnySetter |
| 22 | + @JsonAnyGetter |
| 23 | + public Map<String, Object> others = new LinkedHashMap<>(); |
| 24 | + } |
| 25 | + |
| 26 | + private final XmlMapper MAPPER = newMapper(); |
| 27 | + |
| 28 | + // Simple unmapped elements round-trip correctly |
| 29 | + @Test |
| 30 | + public void testAnySetterSimpleRoundTrip() throws Exception |
| 31 | + { |
| 32 | + String input = "<PojoWith629><id>123</id><simple>hello</simple></PojoWith629>"; |
| 33 | + PojoWith629 pojo = MAPPER.readValue(input, PojoWith629.class); |
| 34 | + assertEquals("123", pojo.id); |
| 35 | + assertEquals("hello", pojo.others.get("simple")); |
| 36 | + |
| 37 | + String xml = MAPPER.writeValueAsString(pojo); |
| 38 | + assertTrue(xml.contains("<simple>hello</simple>"), "Got: " + xml); |
| 39 | + } |
| 40 | + |
| 41 | + // Nested elements without attributes round-trip correctly |
| 42 | + @Test |
| 43 | + public void testAnySetterNestedElementsRoundTrip() throws Exception |
| 44 | + { |
| 45 | + String input = |
| 46 | + "<PojoWith629>" + |
| 47 | + "<id>123</id>" + |
| 48 | + "<unmapped>" + |
| 49 | + "<a>1</a>" + |
| 50 | + "<b>2</b>" + |
| 51 | + "</unmapped>" + |
| 52 | + "</PojoWith629>"; |
| 53 | + |
| 54 | + PojoWith629 pojo = MAPPER.readValue(input, PojoWith629.class); |
| 55 | + assertEquals("123", pojo.id); |
| 56 | + |
| 57 | + Object unmapped = pojo.others.get("unmapped"); |
| 58 | + assertNotNull(unmapped, "unmapped should be captured by @JsonAnySetter"); |
| 59 | + assertTrue(unmapped instanceof Map, "Expected Map but got: " + unmapped.getClass()); |
| 60 | + @SuppressWarnings("unchecked") |
| 61 | + Map<String, Object> nested = (Map<String, Object>) unmapped; |
| 62 | + assertEquals("1", nested.get("a")); |
| 63 | + assertEquals("2", nested.get("b")); |
| 64 | + |
| 65 | + String xml = MAPPER.writeValueAsString(pojo); |
| 66 | + assertFalse(xml.contains("<>"), "Output contains empty tags: " + xml); |
| 67 | + assertFalse(xml.contains("</>"), "Output contains empty closing tags: " + xml); |
| 68 | + } |
| 69 | + |
| 70 | + // [dataformat-xml#629]: Elements with attributes should not produce empty tags |
| 71 | + @Test |
| 72 | + public void testAnySetterAttributeElementRoundTrip() throws Exception |
| 73 | + { |
| 74 | + String input = |
| 75 | + "<PojoWith629>" + |
| 76 | + "<id>123</id>" + |
| 77 | + "<elem uid=\"1\">text</elem>" + |
| 78 | + "</PojoWith629>"; |
| 79 | + |
| 80 | + PojoWith629 pojo = MAPPER.readValue(input, PojoWith629.class); |
| 81 | + assertEquals("123", pojo.id); |
| 82 | + |
| 83 | + String xml = MAPPER.writeValueAsString(pojo); |
| 84 | + assertFalse(xml.contains("<>"), "Output contains empty tags: " + xml); |
| 85 | + assertFalse(xml.contains("</>"), "Output contains empty closing tags: " + xml); |
| 86 | + // text content should be present (even if attribute-ness is lost) |
| 87 | + assertTrue(xml.contains("text"), "Got: " + xml); |
| 88 | + } |
| 89 | + |
| 90 | + // [dataformat-xml#629]: The reporter's original case |
| 91 | + @Test |
| 92 | + public void testAnySetterNestedElementsWithAttributesRoundTrip() throws Exception |
| 93 | + { |
| 94 | + String input = |
| 95 | + "<PojoWith629>" + |
| 96 | + "<id>123</id>" + |
| 97 | + "<unmapped-element>" + |
| 98 | + "<e uid=\"1\">one</e>" + |
| 99 | + "<e uid=\"2\">TWO</e>" + |
| 100 | + "<e uid=\"3\">3</e>" + |
| 101 | + "</unmapped-element>" + |
| 102 | + "</PojoWith629>"; |
| 103 | + |
| 104 | + PojoWith629 pojo = MAPPER.readValue(input, PojoWith629.class); |
| 105 | + assertEquals("123", pojo.id); |
| 106 | + |
| 107 | + String xml = MAPPER.writeValueAsString(pojo); |
| 108 | + assertFalse(xml.contains("<>"), "Output contains empty tags: " + xml); |
| 109 | + assertFalse(xml.contains("</>"), "Output contains empty closing tags: " + xml); |
| 110 | + } |
| 111 | + |
| 112 | + // [dataformat-xml#629]: Verify text-element key works when it appears first |
| 113 | + // in the Map (edge case: _nextName could be null at that point) |
| 114 | + @Test |
| 115 | + public void testAnySetterTextKeyFirstInMap() throws Exception |
| 116 | + { |
| 117 | + PojoWith629 pojo = new PojoWith629(); |
| 118 | + pojo.id = "1"; |
| 119 | + // Construct a Map where the empty key (text element) appears first |
| 120 | + LinkedHashMap<String, Object> inner = new LinkedHashMap<>(); |
| 121 | + inner.put("", "textval"); |
| 122 | + inner.put("attr", "aval"); |
| 123 | + pojo.others.put("elem", inner); |
| 124 | + |
| 125 | + String xml = MAPPER.writeValueAsString(pojo); |
| 126 | + assertFalse(xml.contains("<>"), "Output contains empty tags: " + xml); |
| 127 | + assertFalse(xml.contains("</>"), "Output contains empty closing tags: " + xml); |
| 128 | + assertTrue(xml.contains("textval"), "Text content missing: " + xml); |
| 129 | + assertTrue(xml.contains("<attr>aval</attr>"), "Attribute-turned-element missing: " + xml); |
| 130 | + } |
| 131 | +} |
0 commit comments