jackson-module-kotlin: Missing empty constructor issue: deserialization breaking change from 2.17 to 2.18

Search before asking

  • I searched in the issues and found nothing similar.
  • I have confirmed that the same problem is not reproduced if I exclude the KotlinModule.
  • I searched in the issues of databind and other modules used and found nothing similar.
  • I have confirmed that the problem does not reproduce in Java and only occurs when using Kotlin and KotlinModule.

Describe the bug

It appears that 2.18 introduced a change to the constructor detection, causing existing use cases to fail. I observed it in a case where a class extending a Map without an empty constructor can no longer be instantiated. See the test case example.

To Reproduce

With jackson 2.18.0 onboard:

    @Test
    fun test() {
        assertThrows<MissingKotlinParameterException> {
            jacksonObjectMapper().readValue<Old>("""{ "key":"value" }""")
        }

        assertDoesNotThrow {
            jacksonObjectMapper().readValue<New>("""{ "key":"value" }""")
        }
    }

    // what was working prior to 2.18
    class Old : TreeMap<String, String> {
        constructor(map: Map<String, String>) : super(map)
    }

    // what has to be changed to work with 2.18
    class New : TreeMap<String, String> {
        constructor() : super()
        constructor(map: Map<String, String>) : super(map)
    }

Expected behavior

Changes should be backward-compatible per the versioning standard (minor version changed).

Versions

Kotlin: Jackson-module-kotlin: 2.18.0 Jackson-databind: 2.18.0

Additional context

I’m not 100% sure if this is a Kotlin module issue or not, as I operate in Kotlin codebase only and not invested enough to test it in plane java. Generally, the fix to the issue is simple; the questionable part is that, if this is intentional, this is technically a breaking change in a minor version change.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 13
  • Comments: 21 (12 by maintainers)

Most upvoted comments

I can confirm the bug with these versions of jackson-module-kotlin:

I also tested with version 2.17.2 and the behavior to assure that the MissingKotlinParameterException is not thrown with the older version, as described.

This seems to be breaking for @JsonSubTypes classes too, specifically where there is a private variable value which is of a function type and it has @JsonIgnore added to it, it seems like @JsonIgnore is not ignoring that property for deserialization.

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonTypeInfo
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "type",
)
@JsonSubTypes(
    JsonSubTypes.Type(value = Banana::class, name = "banana"),
    JsonSubTypes.Type(value = Apple::class, name = "apple"),
)
sealed class Fruit(
    @JsonIgnore
    private val accessor: (Map<String, String>) -> String,
) {
    fun accessValue(input: Map<String, String>): String? = accessor.invoke(input)
}

object Banana : Fruit({ it.getOrDefault("color", "") })

object Apple : Fruit({ it.getOrDefault("color", "") })


class ExampleTest : FunSpec({
    context("test example") {
        val objectMapper = ObjectMapper().registerKotlinModule()
        val jsonString = """{ "type": "banana" }""".trimIndent()
        val deserializedKey = objectMapper.readValue(jsonString, Fruit::class.java)
        deserializedKey::class shouldBe Banana::class // <== expect to deserialize into correct subtype class
    }
})

The above test passes with jackson 2.17.2 but fails with the following error in 2.18.3:

Unknown origin of public abstract operator fun invoke(p1: P1): R defined in kotlin.Function1[FunctionInvokeDescriptor@78007060] (class kotlin.reflect.jvm.internal.impl.builtins.functions.FunctionInvokeDescriptor)
kotlin.reflect.jvm.internal.KotlinReflectionInternalError: Unknown origin of public abstract operator fun invoke(p1: P1): R defined in kotlin.Function1[FunctionInvokeDescriptor@78007060] (class kotlin.reflect.jvm.internal.impl.builtins.functions.FunctionInvokeDescriptor)
	at kotlin.reflect.jvm.internal.RuntimeTypeMapper.mapSignature(RuntimeTypeMapper.kt:226)
	at kotlin.reflect.jvm.internal.KFunctionImpl.<init>(KFunctionImpl.kt:55)
	at kotlin.reflect.jvm.internal.CreateKCallableVisitor.visitFunctionDescriptor(util.kt:314)
	at kotlin.reflect.jvm.internal.CreateKCallableVisitor.visitFunctionDescriptor(util.kt:291)
	at kotlin.reflect.jvm.internal.impl.descriptors.impl.FunctionDescriptorImpl.accept(FunctionDescriptorImpl.java:826)
	at kotlin.reflect.jvm.internal.KDeclarationContainerImpl.getMembers(KDeclarationContainerImpl.kt:62)
	at kotlin.reflect.jvm.internal.KClassImpl$Data.declaredNonStaticMembers_delegate$lambda$22(KClassImpl.kt:173)
	at kotlin.reflect.jvm.internal.KClassImpl$Data.accessor$KClassImpl$Data$lambda10(KClassImpl.kt)
	at kotlin.reflect.jvm.internal.KClassImpl$Data$$Lambda$10.invoke(Unknown Source)
	at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.invoke(ReflectProperties.java:70)
	at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(ReflectProperties.java:32)
	at kotlin.reflect.jvm.internal.KClassImpl$Data.getDeclaredNonStaticMembers(KClassImpl.kt:173)
	at kotlin.reflect.jvm.internal.KClassImpl$Data.allNonStaticMembers_delegate$lambda$26(KClassImpl.kt:182)
	at kotlin.reflect.jvm.internal.KClassImpl$Data.accessor$KClassImpl$Data$lambda14(KClassImpl.kt)
	at kotlin.reflect.jvm.internal.KClassImpl$Data$$Lambda$14.invoke(Unknown Source)
	at kotlin.reflect.jvm.internal.ReflectProperties$LazySoftVal.invoke(ReflectProperties.java:70)
	at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(ReflectProperties.java:32)
	at kotlin.reflect.jvm.internal.KClassImpl$Data.getAllNonStaticMembers(KClassImpl.kt:182)
	at kotlin.reflect.full.KClasses.getMemberProperties(KClasses.kt:148)
	at com.fasterxml.jackson.module.kotlin.KotlinNamesAnnotationIntrospector.findDefaultCreator(KotlinNamesAnnotationIntrospector.kt:99)

To workaround this, I had to add a custom noop deserializer and use it as the custom deserializer for this ignored property. This passes with both jackson 2.17.2 and 2.18.3:

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.annotation.JsonSubTypes
import com.fasterxml.jackson.annotation.JsonTypeInfo
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "type",
)
@JsonSubTypes(
    JsonSubTypes.Type(value = Banana::class, name = "banana"),
    JsonSubTypes.Type(value = Apple::class, name = "apple"),
)
sealed class Fruit(
    @JsonIgnore
    @JsonDeserialize(using = AccessorDeserializer::class) // <== This is the custom no-op deserializer
    private val accessor: (Map<String, String>) -> String,
) {
    fun accessValue(input: Map<String, String>): String? = accessor.invoke(input)
}

object Banana : Fruit({ it.getOrDefault("color", "") })

object Apple : Fruit({ it.getOrDefault("color", "") })


class ExampleTest : FunSpec({
    context("test example") {
        val objectMapper = ObjectMapper().registerKotlinModule()
        val jsonString = """{ "type": "banana" }""".trimIndent()
        val deserializedKey = objectMapper.readValue(jsonString, Fruit::class.java)
        deserializedKey::class shouldBe Banana::class // <== expect to deserialize into correct subtype class
    }
})

class AccessorDeserializer : JsonDeserializer<(Map<String, String>) -> String>() {  // <== custom no-op deserializer
    override fun deserialize(
        p: JsonParser,
        ctxt: DeserializationContext,
    ): (Map<String, String>) -> String = { "" }
}

Ah. I only saw the email notification and replied without reading the context.

As to fix, it is not clear behavior can or will be changed.

But instead of adding an empty constructor (which really shouldn’t be needed) please considering adding annotation – something I have mentioned multiple times in this thread:

@JsonCreator(mode = JsonCreator.Mode.DELEGATING)

on constructor. That will indicate that the incoming value should match the full argument value of 1-arg constructor, and not 1 property of JSON Object with name matching that argument.

I will also file an issue on jackson-databind to see if there is a way to create Java-only reproduction, and if so if the behavior could be changed.