-
Notifications
You must be signed in to change notification settings - Fork 635
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
Polymorphic Serialization Mistakenly Triggered for Generic Property with Directly Serializable Type #2953
Comments
Your English is good enough. Unfortunately you have hit a fundamental limitation with sealed interfaces/classes. In this case the parent There is a case to be made to disallow this case entirely (with a good error message that explains the problem with this), but the current solution also has merit. It chooses to use a The way to solve it is to have the base type have a type parameter. If you have subtypes that don't need the parameter and have |
I think this may have been fixed in 2.2, cc @shanshin |
emmm, but it is a type parameter named |
You are right, and as @sandwwraith said, this is hopefully fixed with the Kotlin 2.2 release. |
Recent Kotlin develop and runtime
|
I discovered a new error, and I'm unsure if I should open a new issue: import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json
@Serializable
sealed interface Test<T>
{
val x: T
}
@Serializable
data class Test1<Y>(override val x: Y): Test<Y>
@Serializable
data class Test2<Y>(val x: Test<Y>)
fun main()
{
println(Json.encodeToString(Test2(Test1(1)))) //error
} This code produces the same error in both versions 2.1.10 and 2.2.0-dev:
I observed that the plugin-generated |
This error is very similar to the one above, with the only difference being that there is an additional class of wrapping around |
…ealed class Problem described in Kotlin/kotlinx.serialization#2953 (comment) Fixes Kotlin/kotlinx.serialization#2953
Describe the bug
When serializing a sealed interface's generic property with a directly serializable type (e.g., Int or another @serializable class), the Kotlinx Serialization library incorrectly attempts polymorphic serialization, leading to a SerializationException.
To Reproduce
error:
Expected behavior
no error and successful serialization
Environment
My English is not good, and some of the above content is from AI translation. If it is inaccurate, please forgive me.
The text was updated successfully, but these errors were encountered: