Skip to content

KotlinModule fails to deserialize with readerWithView for classes from external JAR #829

Closed
@cooperlyt

Description

@cooperlyt

Search before asking

  • I searched in the issues and found nothing similar.
  • I searched in the issues of databind and other modules used and found nothing similar.
  • I have confirmed that the problem only occurs when using Kotlin.

Describe the bug

When deserializing a Kotlin data class from an external JAR using objectMapper.readerWithView(), KotlinModule fails with the following error:
Caused by: com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of [simple type, class io.github.cooperlyt.erp.api.data.stock.StockFreezeStatusMessage] value failed for JSON property tenant due to missing (therefore NULL) value for creator parameter tenant which is a non-nullable type at [Source: (String)"{"businessId":1336493025009664,"tenant":"first","type":"LEASE","success":false}"; line: 1, column: 79] (through reference chain: io.github.cooperlyt.erp.api.data.stock.StockFreezeStatusMessage["tenant"])

However, using objectMapper.readValue() works perfectly for the same class and JSON. This issue only occurs when the class is in an external JAR and when using readerWithView() in combination with forType().

To Reproduce

open class StockFreezeStatusMessage(
    val businessId: Long,
    val tenant: String,
    val type: StockRequestType,
    val success: Boolean,
)
  1. Create a Kotlin data class inside a separate project, package it into a JAR, and include it as a dependency in the main project.
  2. Register KotlinModule in the main project using:
val objectMapper = ObjectMapper().registerModule(KotlinModule())
  1. Use readValue() for deserialization (this works fine):
val json = """{"businessId":1336504106360835,"tenant":"first","type":"LEASE","success":false}"""
val value = objectMapper.readValue(json, StockFreezeStatusMessage::class.java)
  1. Now use readerWithView() for deserialization (this throws an exception):
val view: Class<*> = StockFreezeStatusMessage::class.java
val javaType: JavaType = objectMapper.constructType(object : TypeReference<StockFreezeStatusMessage>() {})

val value = objectMapper.readerWithView(view).forType(javaType).readValue<Any>(json)
  1. The second deserialization fails with MissingKotlinParameterException indicating that a non-nullable field (tenant) is missing, even though the JSON contains the correct value.
  2. Moving the data class into the main project (instead of keeping it in the external JAR) this works fine (only Jackson-module-kotlin: 2.18.0-rc1).

Expected behavior

Both readValue() and readerWithView() should correctly deserialize the JSON into the Kotlin data class regardless of whether the class is in the main project or in an external JAR.

Versions

Kotlin:
Jackson-module-kotlin: 2.15.4
Jackson-databind: 2.15.4

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions