Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 of 3 tasks
cooperlyt opened this issue Sep 17, 2024 · 3 comments
Open
1 of 3 tasks
Labels

Comments

@cooperlyt
Copy link

cooperlyt commented Sep 17, 2024

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

@cooperlyt cooperlyt added the bug label Sep 17, 2024
@cowtowncoder
Copy link
Member

Please verify that this still occurs with 2.17.2 (or, if possible, 2.18.0-rc1) just to rule out possibility of later fixes.

@cooperlyt
Copy link
Author

cooperlyt commented Sep 19, 2024

2.17.2 and 2.18.0-rc1: Moving the data class into the main project (instead of keeping it in the external JAR) works fine
but not works with JAR

@cowtowncoder
Copy link
Member

Thank you for verifying behavior is still the same, @cooperlyt .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants