jackson-module-kotlin icon indicating copy to clipboard operation
jackson-module-kotlin copied to clipboard

Failed deserialization of fields where only the first character is lowercase

Open junkdog opened this issue 2 years ago • 1 comments

Describe the bug

Serialization of fields starting with a single lowercase character followed by an uppercase character are serialized as all lowercase. This causes deserialization to fail as jackson tries to deserialize according to the "original"/camelCased field name. E.g. pId is serialized as pid but expects pId during deserialization.

To Reproduce

data class FooBar(
    val id: String,
    val nId: String, // expected name: nId, actual: nid
)

fun main() {
    val fooBar = FooBar("bar", "foo")
    val json = jacksonObjectMapper().writeValueAsString(fooBar)
    assert(fooBar == jacksonObjectMapper().readValue<FooBar>(json))
}

Expected behavior

Serialized fields names should match the original field name.

Versions Kotlin: 1.5.30 Jackson-module-kotlin: 2.12.5 Jackson-databind: 2.12.5

Additional context Add any other context about the problem here.

junkdog avatar Sep 24 '21 12:09 junkdog

+1 Hint where to find the bug: com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector#collectAll com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector#_addMethods com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector#_addGetterMethod com.fasterxml.jackson.databind.ser.BeanSerializerFactory#removeIgnorableTypes

This is because pId has a getPId and setPId format accessor. Jackson extracts the PId from here and makes the letters lowercase until it encounters a lowercase letter (PId -> pid). Quckfix for it - add @JsonProperty("pId") on accessor for Java. Or @get:JsonProperty("pId") / @param:JsonProperty("pId") for kotlin

AndChelp avatar Sep 28 '21 14:09 AndChelp

This issue will be fixed by https://github.com/FasterXML/jackson-module-kotlin/issues/630 and therefore closed as a duplicate.

k163377 avatar Mar 17 '23 05:03 k163377