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

Serializes a property annotated with JsonProperty starting with a lower followed by a upper case letter (xY) multiple times #701

Closed Dudeplayz closed 1 year ago

Dudeplayz commented 1 year ago

Describe the bug Trying to serialize a property annotated with JsonProperty, which starts with a lower case letter, directly followed by a upper case letter, results in a doubled incorrect serialization. E.g. @field:JsonProperty("E_Test") eTest is serialized as E_Test and etest. Only tested in Kotlin with kotlin-module.

To Reproduce

import com.fasterxml.jackson.annotation.JsonProperty

data class Test(
        @field:JsonProperty("E_Test1")
        var eTest1: String? = null,
        @field:JsonProperty("E_Test2")
        var etest2: String? = null,
        @field:JsonProperty("E_Test3")
        var eetest3: String? = null,
)

//Execute
jacksonObjectMapper().writeValueAsString(Test("val1", "val2", "val3"))

Result:

{
    "E_Test1": "val1",
    "E_Test2": "val2",
    "E_Test3": "val3",
    "etest1": "val1"
}

Expected result

{
    "E_Test1": "val1",
    "E_Test2": "val2",
    "E_Test3": "val3"
}

Versions Kotlin: 1.9.0 Jackson-module-kotlin: 2.15.2 Jackson-databind: 2.15.2

Additional context

data class Test(
        var eTest1: String? = null,
        var etest2: String? = null,
        var eetest3: String? = null,
)

results in

{
    "etest2": "val2",
    "eetest3": "val3",
    "etest1": "val1"
}

notable, because the property order is changed and etest1 is at the end.

k163377 commented 1 year ago

This problem is caused by a difference between the name that jackson analogizes from the getter name generated by Kotlin and the actual field name.

A workaround option for these issues will be released in 2.16. Please see the issue below for details. https://github.com/FasterXML/jackson-module-kotlin/issues/630

This issue is closed as a duplicate.