jackson-module-kotlin: Serialization doesn't work correctly for objects parametrized by inline class

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule

inline class A(val value: Int = 0)
inline class B(val value: Int = 0)
inline class C(val value: Int = 0)
class D(val inlineField: A = A())

class Pojo(
    val works: A,
    val alsoWorks: B,
    val worksToo: Collection<D>,
    val broken: Collection<C>
)

val om = ObjectMapper().registerModule(KotlinModule())
println(om.writeValueAsString(Pojo(A(), B(), listOf(D(), D()), listOf(C(), C()))))

Output:

{
  "works": 0,
  "alsoWorks": 0,
  "worksToo": [ {"inlineField":0}, {"inlineField":0} ],
  "broken": [ {"value":0}, {"value":0} ]
}

Jackson version 2.12

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 25 (21 by maintainers)

Commits related to this issue

Most upvoted comments

Value classes will ultimately support multiple fields and compile to primitive classes on the JVM, when they are introduced. Value classes with multiple fields will not support @JvmInline, so ideally we should check for that annotation before auto-unboxing to avoid backward compatibility issues.

I haven’t heard back from JB yet but someone else brought up a good point. It would be impossible to use a @JvmInline class with multiple fields as a return type because the fields could not be inlined into the single return type required by the JVM. I think based on this, it is safe to assume that @JvmInline classes will only ever have one field.

Yes, I agree with everything you said. After reading a second time, I agree that the language in the specification is not clear enough to completely rule out @JvmInline classes with multiple fields, so I asked the question in the Kotlin Slack channel. We will see if anyone from JetBrains gets back to me.