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

DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT does not work for Kotlin nullable types

Open gaborkr opened this issue 8 years ago • 1 comments

Given

val mapper = ObjectMapper()
    .findAndRegisterModules()
    .configure(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT, true)

Test case 1:

data class A(@JsonFormat(shape = JsonFormat.Shape.ARRAY) val p: Pair<Double, Double>?)

val a: A = mapper.readValue("{\"p\": []}")

throws

com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of [simple type, class kotlin.Pair<java.lang.Double,java.lang.Double>] value failed for JSON property first due to missing (therefore NULL) value for creator parameter first which is a non-nullable type

Test case 2:

    @JsonFormat(shape = JsonFormat.Shape.ARRAY)
    data class Coordinate(val latitude: Double, val longitude: Double)

    data class B(val p:Coordinate?)

    val b: B = mapper.readValue("{\"p\": []}")

returns Coordinate(latitude=0.0, longitude=0.0)

Both classes should be a valid representation of {p: []}. I would expect to receive A(p=null) and B(p=null) objects.

gaborkr avatar Jun 02 '17 17:06 gaborkr

@haspok sorry for such a long answer, but ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT works as expected in this module:

class Github73 {

    data class A(@JsonFormat(shape = JsonFormat.Shape.ARRAY) val p: Pair<Double, Double>?)

    @Test
    fun testAcceptEmptyArrayAsNullObject() {
        val mapper = jacksonObjectMapper().enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT)
        val result = mapper.readValue<A>("[]")
        assertNull(result)
    }

}

I think the main problem is confusing name for this feature. You can see a discussion about it here: https://github.com/FasterXML/jackson-databind/issues/1323. There is an opened issue for the feature you are looking for: https://github.com/FasterXML/jackson-databind/issues/2124

viartemev avatar May 11 '20 08:05 viartemev

It will be closed because it appears to have been answered.

k163377 avatar Jul 15 '23 16:07 k163377