FasterXML / jackson-future-ideas

Repository for SOLE PURPOSE of issue tracker and Wiki for NEW IDEAS. Please: NO BUG REPORTS.
18 stars 6 forks source link

nullIsNameAsDefault does not work when inheritance. #56

Closed hohoins closed 3 years ago

hohoins commented 3 years ago

nullIsNameAsDefault does not work when inheritance.

In general, nullIsNameAsDefault works well. However, if you receive inheritance, it will not work. In the example below, responseC generates an error. Did I do something wrong?

Thank you very much.

Environment: android with retrofit2

` import com.fasterxml.jackson.databind.DeserializationFeature import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.module.kotlin.KotlinModule

object JacksonTest { // android with retrofit2 // implementation "com.squareup.retrofit2:converter-jackson:2.9.0" // implementation "com.fasterxml.jackson.module:jackson-module-kotlin:2.12.2"

fun test() {
    val objectMapper = ObjectMapper()
    objectMapper.registerModule(KotlinModule(nullIsSameAsDefault = true))
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

    val responseA = "{\"baseData\":\"data1\",\"myData\":\"data2\"}"
    val responseB = "{\"baseData\":\"data1\",\"myData\":null}"
    val responseC = "{\"baseData\":null,\"myData\":\"data2\"}"

    val successObjA = objectMapper.readValue(responseA, MyClass::class.java)
    val successObjB = objectMapper.readValue(responseB, MyClass::class.java)
    val failObjC = objectMapper.readValue(responseC, MyClass::class.java)
    // Caused by: com.fasterxml.jackson.databind.JsonMappingException: Parameter specified as non-null is null:
    // method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter <set-?>
}

}

open class BaseClass(var baseData: String = "baseDataDefault")

class MyClass(val myData: String = "myDataDefault") : BaseClass() `