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

Mismatch in deserialized/serialized serial name used for sealed value class. #2288

Closed
christiandeange opened this issue Apr 30, 2023 · 2 comments · Fixed by #2601
Closed
Assignees
Labels

Comments

@christiandeange
Copy link

christiandeange commented Apr 30, 2023

Describe the bug

When serializing a sealed value class, the serial name of the inner class is embedded into the resulting JSON string. However, the produced string cannot be deserialized, as it is the outer class's serial name (ie: the sealed value class itself) that should be used here.

To Reproduce

@Serializable
sealed interface Sealed

@JvmInline
@Serializable
@SerialName("outer")
value class SealedOuter(val inner: Inner) : Sealed

@Serializable
@SerialName("inner")
data class Inner(val n: Int)

val sealed: Sealed = SealedOuter(Inner(10))

Json.encodeToString(sealed)                                     // {"type":"inner","n":10}
Json.decodeFromString<Sealed>("""{"type":"inner","n":10}""")    // ERROR: Polymorphic serializer was not found for class discriminator 'inner'
Json.decodeFromString<Sealed>("""{"type":"outer","n":10}""")    // SealedOuter(inner=Inner(n=10))

Expected behavior

Json.encodeToString(sealed) should probably return {"type":"outer","n":10} instead, since that string would be able to be deserialized back into an equivalent instance as the original.

I used @SerialName annotations to make the example more succinct, but the same issue can be seen if using omitting that and using the fully-qualified class names by default instead.

Environment

  • Kotlin version: 1.8.20
  • Library version: 1.5.0
  • Kotlin platforms: JVM
  • Gradle version: 8.0.2
@sandwwraith sandwwraith added the bug label May 8, 2023
@sandwwraith
Copy link
Member

Related: #2159, #2164, #2049

@christiandeange
Copy link
Author

@sandwwraith any updates on this issue?

tadfisher added a commit to tadfisher/kotlinx.serialization that referenced this issue Mar 14, 2024
tadfisher added a commit to tadfisher/kotlinx.serialization that referenced this issue Mar 14, 2024
tadfisher added a commit to tadfisher/kotlinx.serialization that referenced this issue Mar 21, 2024
tadfisher added a commit to tadfisher/kotlinx.serialization that referenced this issue Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants