Skip to content

Commit 6789218

Browse files
authored
Mark #248 as fixed; add regression test (#823)
1 parent 70c8bad commit 6789218

4 files changed

Lines changed: 121 additions & 0 deletions

File tree

release-notes/CREDITS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ Christopher McVay (@mcvayc)
3939
* Fixed #306: Can not use `@JacksonXmlText` for Creator property (creator parameter)
4040
(3.2.0)
4141

42+
Tobias M (@lightbringer)
43+
* Reported #248: `@JacksonXmlProperty` with attributes raises an exception
44+
when used with `@JsonIdentityInfo`
45+
(3.2.0)
46+
4247
Eduard Wirch (@ewirch)
4348
* Reported #306: Can not use `@JacksonXmlText` for Creator property (creator parameter)
4449
(3.2.0)

release-notes/VERSION

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Version: 3.x (for earlier see VERSION-2.x)
1818
#192: Two wrapped lists with items of same name conflict
1919
(reported by @TeemuStenhammar)
2020
(fix by @cowtowncoder, w/ Claude code)
21+
#248: `@JacksonXmlProperty` with attributes raises an exception when used
22+
with `@JsonIdentityInfo`
23+
(reported by Tobias M)
24+
(fix by @cowtowncoder, w/ Claude code)
2125
#299: `@JsonUnwrapped` with a `@JacksonXmlElementWrapper(useWrapping=false)`
2226
on a collection fails reading from XML
2327
(reported by @jmsjr)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package tools.jackson.dataformat.xml.lists;
2+
3+
import java.util.List;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import tools.jackson.dataformat.xml.XmlMapper;
8+
import tools.jackson.dataformat.xml.XmlTestUtil;
9+
import tools.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
10+
import tools.jackson.dataformat.xml.annotation.JacksonXmlProperty;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
14+
// [dataformat-xml#192]: Two wrapped lists with items of same name
15+
public class WrappedListsWithSameName192Test extends XmlTestUtil
16+
{
17+
static class TwoLists {
18+
@JacksonXmlElementWrapper(localName = "ones")
19+
@JacksonXmlProperty(localName = "name")
20+
public List<String> ones;
21+
22+
@JacksonXmlElementWrapper(localName = "twos")
23+
@JacksonXmlProperty(localName = "name")
24+
public List<String> twos;
25+
}
26+
27+
private final XmlMapper MAPPER = newMapper();
28+
29+
@Test
30+
public void testTwoWrappedListsWithSameItemName() throws Exception
31+
{
32+
final String xml =
33+
"<TwoLists>"
34+
+ "<ones><name>one1</name><name>one2</name></ones>"
35+
+ "<twos><name>two1</name><name>two2</name></twos>"
36+
+ "</TwoLists>";
37+
TwoLists result = MAPPER.readValue(xml, TwoLists.class);
38+
assertNotNull(result);
39+
assertEquals(2, result.ones.size());
40+
assertEquals("one1", result.ones.get(0));
41+
assertEquals("one2", result.ones.get(1));
42+
assertEquals(2, result.twos.size());
43+
assertEquals("two1", result.twos.get(0));
44+
assertEquals("two2", result.twos.get(1));
45+
}
46+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package tools.jackson.dataformat.xml.misc;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
6+
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
7+
8+
import tools.jackson.dataformat.xml.XmlMapper;
9+
import tools.jackson.dataformat.xml.XmlTestUtil;
10+
import tools.jackson.dataformat.xml.annotation.JacksonXmlProperty;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
14+
// [dataformat-xml#248]: @JacksonXmlProperty(isAttribute=true) with @JsonIdentityInfo
15+
// throws "Trying to write an attribute when there is no open start element"
16+
public class ObjectIdWithAttribute248Test extends XmlTestUtil
17+
{
18+
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "name")
19+
static class NodeA {
20+
@JacksonXmlProperty(isAttribute = true)
21+
public String name;
22+
23+
public NodeB ref;
24+
25+
public NodeA() { }
26+
public NodeA(String name) { this.name = name; }
27+
}
28+
29+
static class NodeB {
30+
public NodeA ref;
31+
32+
public NodeB() { }
33+
public NodeB(NodeA a) { this.ref = a; }
34+
}
35+
36+
private final XmlMapper MAPPER = newMapper();
37+
38+
@Test
39+
public void testSerializeAttributeWithIdentityInfo() throws Exception
40+
{
41+
NodeA a = new NodeA("test");
42+
NodeB b = new NodeB(a);
43+
a.ref = b;
44+
45+
String xml = MAPPER.writeValueAsString(a);
46+
assertNotNull(xml);
47+
assertTrue(xml.contains("name=\"test\""),
48+
"Expected 'name' as XML attribute, got: " + xml);
49+
}
50+
51+
@Test
52+
public void testRoundTripAttributeWithIdentityInfo() throws Exception
53+
{
54+
NodeA a = new NodeA("test");
55+
NodeB b = new NodeB(a);
56+
a.ref = b;
57+
58+
String xml = MAPPER.writeValueAsString(a);
59+
NodeA result = MAPPER.readValue(xml, NodeA.class);
60+
61+
assertNotNull(result);
62+
assertEquals("test", result.name);
63+
assertNotNull(result.ref);
64+
assertSame(result, result.ref.ref);
65+
}
66+
}

0 commit comments

Comments
 (0)