Skip to content

Type discriminator leaks from serialized value class to the next property #3155

@huzvanec

Description

@huzvanec

Describe the bug
When a @JvmInline value class implements a sealed interface, the polymorphic serialization does not correctly update its state after encoding the value class as a primitive. This causes the "type" discriminator to be injected into the next serializable property in the class.

To Reproduce

@Serializable
sealed interface Component

@Serializable
@JvmInline
value class TextComponent(val value: String) : Component

@Serializable
data class Behavior(val description: String)


@Serializable
data class Dog(
    val name: Component,    // value class implementing a sealed interface
    val behavior: Behavior  // the next object that incorrectly gets assigned the type discriminator during serialization
)

fun main(args: Array<String>) {
    val json = Json { prettyPrint = true }
    println(
        json.encodeToString(
            Dog(
                name = TextComponent("Daisy"),
                behavior = Behavior(description = "cuddly")
            )
        )
    )
}

outputs:

{
    "name": "Daisy",
    "behavior": {
        "type": "com.example.TextComponent",
        "description": "cuddly"
    }
}

Expected behavior
The name should either include its type or be a raw string, and behavior should not contain the type of the previous property.

Environment

  • Kotlin version: 2.3.10
  • Library version: 1.10.0
  • Kotlin platforms: JVM
  • Gradle version: 9.3.1
  • IDE version: IntelliJ IDEA Ultimate 2025.3.3
  • OS: Debian GNU/Linux 13 (trixie) x86_64
  • JRE version: Temurin 21.0.10

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions