Why doesn't kotlinx.datetime.LocalTime XML deserialization with Jackson use default kotlin values?

41 Views Asked by At

I was trying to de-serialize an XML payload with a nullable kotlinx.datetime.LocalTime field. However, I encountered an error suggesting that my field was in fact not declared as nullable even though it is.

The following code:

class NullableKotlinxLocalTimeTest {
    @XmlRootElement
    data class NullableKotlinxLocalTime(
        val time: kotlinx.datetime.LocalTime? = null,
    )

    @XmlRootElement
    data class NullableJavaLocalTime(
        val time: LocalTime? = null,
    )

    @Test
    fun nullableKotlinxDateTimeException() {
        val mapper =
            XmlMapper(
                JacksonXmlModule().apply {
                    setDefaultUseWrapper(false)
                },
            )
                .registerModule(JavaTimeModule())
                .registerKotlinModule() as XmlMapper

        val xml =
            """
            <root>
                <time></time>
            </root>
            """.trimIndent()

        // Works as expected
        mapper.readValue(
            xml,
            NullableJavaLocalTime::class.java,
        )

        // Throws "Parameter specified as non-null is null"
        mapper.readValue(
            xml,
            NullableKotlinxLocalTime::class.java,
        )
    }
}

Throws the following exception:

com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of `kotlinx.datetime.LocalTime`, problem: Parameter specified as non-null is null: method kotlinx.datetime.LocalTime.<init>, parameter value
 at [Source: (StringReader); line: 2, column: 11] (through reference chain: NullableKotlinxLocalTimeTest$NullableKotlinxLocalTime["time"])

Does anyone know why this doesn't work for the kotlinx.datetime.LocalTime? I've also tried changing the data class to use val time: String? = null,. When doing so the time value is correctly assigned the default value null.

0

There are 0 best solutions below