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

Value class incompatible with Jackson default typing #608

Closed
christophejan opened this issue Dec 7, 2022 · 1 comment
Closed

Value class incompatible with Jackson default typing #608

christophejan opened this issue Dec 7, 2022 · 1 comment
Labels

Comments

@christophejan
Copy link

Describe the bug

Value class throw exception when Jackson default typing is activated

To Reproduce

data class MyDataClass(val myValue: Any)

@JvmInline
value class MyValueClass(val myValue: String)

val objectMapper = ObjectMapper().apply {
    activateDefaultTyping(polymorphicTypeValidator, DefaultTyping.NON_FINAL, As.PROPERTY)
    registerKotlinModule()
}

objectMapper.writeValueAsString(MyDataClass(MyValueClass("my value")))

Output :

Type id handling not implemented for type java.lang.Object (by serializer of type com.fasterxml.jackson.module.kotlin.ValueClassUnboxSerializer) (through reference chain: fr.maif.epa.backmobile.commons.infrastructure.cache.CacheServiceTest$MyDataClass["myValue"])
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Type id handling not implemented for type java.lang.Object (by serializer of type com.fasterxml.jackson.module.kotlin.ValueClassUnboxSerializer) (through reference chain: fr.maif.epa.backmobile.commons.infrastructure.cache.CacheServiceTest$MyDataClass["myValue"])
	at app//com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)

Expected behavior

Jackson module Kotlin should be able de serialize/deserialize this kind of value with Jackson default typing

Versions

Kotlin: 1.7.0
Jackson-module-kotlin: 2.13.3
Jackson-databind: 2.13.3

Additional context

Model used to reproduce is a simplified one, just to put in light the current behavior (remove sealed class, complex type in value class etc...).

@k163377
Copy link
Contributor

k163377 commented Mar 17, 2023

This is not a problem caused by jackson-module-kotlin or value class.
It is possible to reproduce this problem even with code that can be written only in Java, for example

import com.fasterxml.jackson.annotation.JsonTypeInfo
import com.fasterxml.jackson.databind.ObjectMapper
import java.time.LocalDateTime

data class MyDataClass(val myValue: Any)

fun main() {
    val objectMapper = ObjectMapper().apply {
        activateDefaultTyping(polymorphicTypeValidator, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY)
    }
    println(objectMapper.writeValueAsString(MyDataClass(LocalDateTime.now())))
}

The cause of this problem is that StdSerializer does not take ObjectIdWriter into account (it only works correctly when serialized with BeanSerializer).
If you need a new feature, please submit an issue to jackson-databind.

This issue will be closed.

As a side note, deserialization of value class is not yet supported (#650).

@k163377 k163377 closed this as completed Mar 17, 2023
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