Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jackson XML reading mixed sibling nodes "overwriting" issue #712

Open
chengyiuchun opened this issue Jan 26, 2025 · 3 comments
Open

Jackson XML reading mixed sibling nodes "overwriting" issue #712

chengyiuchun opened this issue Jan 26, 2025 · 3 comments

Comments

@chengyiuchun
Copy link

Jackson seems to be overwriting the older sibling nodes if the order is mixed. See below test case. Seems to be a bug. Thanks.

@JacksonXmlRootElement
data class RootNode(
    @JacksonXmlElementWrapper(useWrapping = false)
    @JacksonXmlProperty(localName = "NodeA")
    val nodeA: List<NodeA>,
    @JacksonXmlProperty(localName = "NodeB")
    val nodeB: NodeB
)

data class NodeA(
    @JacksonXmlProperty(localName = "attr", isAttribute = true)
    val attr: String
)

data class NodeB(
    @JacksonXmlProperty(localName = "attr", isAttribute = true)
    val attr: String
)

class ReadXMLTest {
    @Test
    fun canRead() {
        val xml = """
            <RootNode>
                <NodeA attr="123"/>
                <NodeB attr="ABC"/>
                <NodeA attr="456"/>
            </RootNode>
        """.trimIndent()

        val xmlMapper = XmlMapper()
        xmlMapper.registerModule(kotlinModule())

        assertEquals(
            RootNode(
                listOf(NodeA("123"), NodeA("456")),
                NodeB("ABC")
            ),
            xmlMapper.readValue<RootNode>(xml)
        )
    }
}

This fails with

Expected :RootNode(nodeA=[NodeA(attr=123), NodeA(attr=456)], nodeB=NodeB(attr=ABC))
Actual   :RootNode(nodeA=[NodeA(attr=456)], nodeB=NodeB(attr=ABC))

If the XML is like below, then it passes.

<RootNode>
    <NodeA attr="123"/>
    <NodeA attr="456"/>
    <NodeB attr="ABC"/>
</RootNode>
@cowtowncoder
Copy link
Member

This is the way things work unfortunately: there is no merging of dis-joint sequences of elements.
It is unlikely to be resolved in near future.

But there is a work-around: if you add a setter method you can merge Lists on setter.

@chengyiuchun
Copy link
Author

Thanks. Due to other requirements we decided to write a custom deserializer for this.

@cowtowncoder
Copy link
Member

Ok best of luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants