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

After upgrading from 2.12.3 to 2.13.0, conversion fails in Kotlin

Open MrBuddyCasino opened this issue 3 years ago • 3 comments

Describe the bug A conversion that worked before doesn't anymore: Conflicting getter definitions for property "fSRI": com.example.ModelRow#getFSRI-SECTION() vs com.example.ModelRow#getFSRI-ID()

Version information upgrade from 2.12.3 to 2.13.0

To Reproduce

import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.MapperFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.fasterxml.jackson.module.kotlin.convertValue
import org.junit.jupiter.api.Test

class JacksonTest {

    @Test
    fun doTest() {
        val map = mapOf( "FSRI-SECTION" to "1", "FSRI-ID" to "1234")
        objectMapper().convertValue<MyModel>(map)
    }

    data class MyModel(
        val `FSRI-ID`: String?,
        val `FSRI-SECTION`: Int?
    )

    private fun objectMapper(): ObjectMapper {
        // defaults largely as specified in Jackson2ObjectMapperBuilder
        return ObjectMapper()
            .registerModule(KotlinModule(nullToEmptyCollection = true, nullIsSameAsDefault = true))
            .registerModule(JavaTimeModule())
            .registerModule(Jdk8Module())
            .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
            .configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false)
    }

}

Expected behavior Conversion should succeed.

Additional context Kotlin version: 1.5.21

MrBuddyCasino avatar Aug 14 '21 11:08 MrBuddyCasino

After some debugging, Jackson seems to pick up a 3rd fSRI property via findImplicitPropertyName() called in _addGetterMethod(), which then leads to the exception. I'll check if I can disable that behaviour.

MrBuddyCasino avatar Aug 14 '21 12:08 MrBuddyCasino

It seems this is fixed by configuring the mapper with: .withGetterVisibility(JsonAutoDetect.Visibility.NONE). Maybe an upgrade notice could be added?

MrBuddyCasino avatar Aug 14 '21 12:08 MrBuddyCasino

(transferred since this appears to be Kotlin-related, wrt naming)

cowtowncoder avatar Sep 28 '21 02:09 cowtowncoder

This issue will be fixed by #630 and therefore closed as a duplicate.

k163377 avatar Mar 04 '23 00:03 k163377