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

Can't serialize Kotlin Value class that has map inside #873

Open
4 tasks done
kkocel opened this issue Dec 20, 2024 · 2 comments
Open
4 tasks done

Can't serialize Kotlin Value class that has map inside #873

kkocel opened this issue Dec 20, 2024 · 2 comments

Comments

@kkocel
Copy link

kkocel commented Dec 20, 2024

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

I tried to serialize Kotlin value class, and failed with:

Cannot invoke "com.fasterxml.jackson.databind.JsonSerializer.serialize(Object, com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.databind.SerializerProvider)" because "keySerializer" is null (through reference chain: com.paramount.snapshooter.pitstop.SerializationValueClassTest$TimestampedPerson["person"]->java.util.LinkedHashMap["id"])
com.fasterxml.jackson.databind.JsonMappingException: Cannot invoke "com.fasterxml.jackson.databind.JsonSerializer.serialize(Object, com.fasterxml.jackson.core.JsonGenerator, com.fasterxml.jackson.databind.SerializerProvider)" because "keySerializer" is null (through reference chain: com.paramount.snapshooter.pitstop.SerializationValueClassTest$TimestampedPerson["person"]->java.util.LinkedHashMap["id"])

To Reproduce

import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import org.junit.jupiter.api.Test

class SerializationValueClassTest() {

    @Test
    fun `should serialize value class`() {

        val person = Person(
            mapOf(
                "id" to "123",
                "updated" to "2023-11-22 12:11:23",
                "login" to "2024-01-15",
            ),
        )

        val serialized2 = jacksonObjectMapper().writeValueAsString(
            TimestampedPerson(
                123L,
                Person(person.properties),
            )
        )

        val deserialized2 = jacksonObjectMapper().readValue<TimestampedPerson>(serialized2)

        assert(
            deserialized2 == TimestampedPerson(
                123L,
                Person(person.properties),
            )
        )
    }

    @JvmInline
    value class Person(
        val properties: Map<String, Any>,
    )

    data class TimestampedPerson(
        val timestamp: Long,
        val person: Person,
    )
}

Expected behavior

Value class shoud be serialized

Versions

Kotlin: 2.10
Jackson-module-kotlin: 2.17.1
Jackson-databind: 2.18.1

Additional context

No response

@kkocel kkocel added the bug label Dec 20, 2024
@kkocel kkocel changed the title Can't serialize kotlin value class that has map inside in 2.17 Can't serialize Kotlin value class that has map inside in 2.17 Dec 20, 2024
@kkocel kkocel changed the title Can't serialize Kotlin value class that has map inside in 2.17 Can't serialize Kotlin Value class that has map inside in 2.17 Dec 20, 2024
@cowtowncoder
Copy link
Member

Quick note on this:

Jackson-module-kotlin: 2.17.1
Jackson-databind: 2.18.1

The general rule is that minor versions should match to avoid any incompatibilities. Although in this particular case order of dependencies is such that this should work (kotlin can depend on later databind versions; but not vice versa).

Still, I'd suggest trying this out with both Kotlin module and databind with latest published version, 2.18.2, to rule out possibility of issue being fixed since versions you use.

@kkocel kkocel changed the title Can't serialize Kotlin Value class that has map inside in 2.17 Can't serialize Kotlin Value class that has map inside Jan 2, 2025
@kkocel
Copy link
Author

kkocel commented Jan 2, 2025

@cowtowncoder I checked it with 2.18.2 and created https://github.com/kkocel/jackson-module-kotlin-873 with reproduction

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

No branches or pull requests

3 participants