Skip to content

STRICT_DUPLICATE_DETECTION and FAIL_ON_READING_DUP_TREE_KEY allow for duplications, when they come from child-nodes #779

@bjuergens

Description

@bjuergens

Search before asking

  • I searched in the issues and found nothing similar.

(#237 describes FAIL_ON_READING_DUP_TREE_KEY )

Describe the bug

When mapping to a non-list, like a string, I expect to get an exception, when there is a duplicate value. As it is, the first value just disappears, with no possibility to even know there was something wrong to begin with. As it stands, "FAIL_ON_READING_DUP_TREE_KEY" only picks up on duplicate Attributes, which is already broken XML to begin with.

example:

<myXml>
    <value>aaa</value>
    <value>bbb</value>
</myXml> ```

with this class 
```kotlin
data class MyXml(val value: String)

here MyXml.value receives a duplicate value, which FAIL_ON_READING_DUP_TREE_KEY will not detect.

I think the solution would either to add this to the "known limitation" section in the readme, or to fix the bug.

Is there a workaround for this bug, so that I can find out, when multiple values gets mapped to the same property? (And no, I don't want to turn every single property into a list, and then manually check if size<=1. But maybe there is something like a global flag, that adds a custom setter to all properties, that checks if there was already a value? Or maybe there is some way to add a custom Feature to add this check?)

Version Information

2.18.2

Reproduction

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.dataformat.xml.XmlMapper
import com.fasterxml.jackson.module.kotlin.kotlinModule
import org.testng.annotations.Test
import kotlin.test.assertEquals
data class MyXml(val value: String)

class DuplicateTest {

    @Test(expectedExceptions = [com.fasterxml.jackson.core.JsonParseException::class])
    fun duplicateNodeTestWorksWithAttributes() {
        val mapper = XmlMapper.builder().addModule(kotlinModule()).enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY).enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION).build()
        mapper.readValue("""<myXml value="aaa" value="bbb" />""", MyXml::class.java)
    }
    
    @Test(expectedExceptions = [com.fasterxml.jackson.core.JsonParseException::class]) 
    fun duplicateNodeTestDoesntWorkWithChildren() {
        val mapper = XmlMapper.builder().addModule(kotlinModule()).enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY).enable(JsonParser.Feature.STRICT_DUPLICATE_DETECTION).build()
        val result = mapper.readValue("""<myXml>
                    <value>aaa</value>
                    <value>bbb</value>
                </myXml> """, MyXml::class.java)

        assertEquals("bbb", result.value)
        assertEquals("aaa", result.value)
    }
}

Expected behavior

I expect "STRICT_DUPLICATE_DETECTION" to give me an exception, whenever an existing value gets overwritten, and if the target-property is no list.

Additional context

I am no jackson-XML professional, so odds are, that I overlooked something obvious. I that is the case, I am very sorry.

(edit: formatting)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions