Skip to content

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

@chengyiuchun

Description

@chengyiuchun

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>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions