After upgrading to Jackson 2.12.3, weirdly named Kotlin properties don't seem to be supported anymore:
Caused by: java.lang.IllegalArgumentException:
Conflicting getter definitions for property "fSRI": com.example.ModelRow#getFSRI-SECTION() vs com.example.ModelRow#getFSRI-ID()
Full test case:
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 {
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)
}
}
It worked fine with 2.11.3.
You need to enable matching by fields not by getters
. You can define this for the ObjectMapper
:
private fun objectMapper(): ObjectMapper {
val mapper = ObjectMapper()
.registerModule(KotlinModule(nullToEmptyCollection = true, nullIsSameAsDefault = true))
.registerModule(JavaTimeModule())
.registerModule(Jdk8Module())
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false)
return mapper.setVisibility(mapper.visibilityChecker
.withGetterVisibility(JsonAutoDetect.Visibility.NONE)
.withFieldVisibility(JsonAutoDetect.Visibility.ANY))
}
Or annotate only given class:
@JsonAutoDetect(getterVisibility = NONE, fieldVisibility = ANY)
data class MyModel(
In both cases it should work and print:
MyModel(FSRI-ID=1234, FSRI-SECTION=1)
See also: