FasterXML / jackson-module-kotlin

Module that adds support for serialization/deserialization of Kotlin (http://kotlinlang.org) classes and data classes.
Apache License 2.0
1.12k stars 175 forks source link

Jackson 2.12.3 and later fails to collapse XML list using @JsonAlias #721

Open henrik242 opened 10 months ago

henrik242 commented 10 months ago

As described in this blog post it has been possible to deserialize this:

    <LedgerTransactionDetails>
      <LedgerActivityDetailObject>
        <Amount>1056.38</Amount>
        <TransactionType>Simple</TransactionType>
      </LedgerActivityDetailObject>
    </LedgerTransactionDetails>

... into a collaped list like this:

data class LedgerActivityDetail(
        @set:JsonProperty("TransactionType")
        var loanType: String? = null,

        @set:JsonProperty("Amount")
        var amount: String? = null
)

@JsonRootName("LedgerActivityObject")
data class LedgerActivity(

        @set:JsonProperty("LedgerTransactionDate")
        var LedgerTransactionDate: String? = null,

        @set:JsonProperty("Amount")
        var amount: String? = null,

        // HERE IS WHERE THE MAGIC HAPPENS!!!
        @set:JsonAlias("LedgerTransactionDetails", "LedgerActivityDetailObject")
        var LedgerActivityDetails: List<LedgerActivityDetail> = ArrayList()
)

This worked until jackson 2.12.2. In 2.12.3 and onwards (including 2.16.0-rc1) the inner values of LedgerActivityDetail are null

Here's a test project: https://github.com/henrik242/jackson-xml-problem/blob/list-wrappers/src/test/kotlin/jackson/xml/ListWrappersTest.kt

henrik242 commented 10 months ago

@cowtowncoder Any ideas if this should be possible somehow in jackson 2.12.3+? I'm not familiar with how this is supposed to work in newer versions.

cowtowncoder commented 10 months ago

From little reading this does not seem like a reliable way to work around problems, i.e. might appear to work by accident more than intentionally.

But as usual, one important part in de-tangling problems is trying to figure out what part is due to Kotlin (annotations being associated with accessors as needed), and which XML format module (and possibly even jackson-databind occasionally). So pure-Java reproduction would be useful to show something is not Kotlin-specific (if so).