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

Deserialization of generic class with nullable values failes #917

Open
3 of 4 tasks
Janik7777 opened this issue Feb 13, 2025 · 0 comments
Open
3 of 4 tasks

Deserialization of generic class with nullable values failes #917

Janik7777 opened this issue Feb 13, 2025 · 0 comments
Labels

Comments

@Janik7777
Copy link

Janik7777 commented Feb 13, 2025

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

Error occurs during deserialization of my generic class:

com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of [simple type, class SerializationTest$test_deserialization$Example<java.lang.String>] value failed for JSON property data due to missing (therefore NULL) value for creator parameter data which is a non-nullable type
 at [Source: (StringReader); line: 3, column: 1] (through reference chain: SerializationTest$test_deserialization$Example["data"])
	at com.fasterxml.jackson.module.kotlin.KotlinValueInstantiator.createFromObjectWith(KotlinValueInstantiator.kt:97)
	at com.fasterxml.jackson.databind.deser.impl.PropertyBasedCreator.build(PropertyBasedCreator.java:214)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:541)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1497)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:348)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:185)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4917)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3860)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3843)
	at SerializationTest.test_deserialization(SerializationTest.kt:36)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

This is my data class

@JsonIgnoreProperties(ignoreUnknown = true)
data class Example<T> @JsonCreator constructor(
    @JsonProperty("data") var data: T,
    @JsonProperty("number") var number : Int,
)

I want use it for non null and nullable data types for T. One example usage would be:

val value = Example<String?>(null, 10)

We want our YAML file only to include non-null fields, so we use the option setSerializationInclusion(JsonInclude.Include.NON_NULL) for the mapper.

But now the deserialization fails.

To Reproduce

import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import org.junit.jupiter.api.Test

class SerializationTest {
    @Test
    fun test_deserialization () {
        @JsonIgnoreProperties(ignoreUnknown = true)
        data class Example<T> @JsonCreator constructor(
            @JsonProperty("data") var data: T,
            @JsonProperty("number") var number : Int,
        )
        val yamlMapper = ObjectMapper(
            YAMLFactory()
        )
            .registerKotlinModule()
            .setSerializationInclusion(JsonInclude.Include.NON_NULL)

        val value = Example<String?>(null, 10)
        val yamlString = yamlMapper.writeValueAsString(value)
        val deserializedValue = yamlMapper.readValue<Example<String?>>(yamlString)
    }
}

Expected behavior

deserializedValue == Example<String?>(null, 10)

Versions

Kotlin:
Jackson-module-kotlin: 2.18.2
Jackson-databind: 2.18.2

Additional context

No response

@Janik7777 Janik7777 added the bug label Feb 13, 2025
@Janik7777 Janik7777 changed the title Deserialization fo generic class with nullable values failes Deserialization of generic class with nullable values failes Feb 14, 2025
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

1 participant