-
Notifications
You must be signed in to change notification settings - Fork 620
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
serializer<KType>() throws SerializationException when used with sealed class on JS #1116
Comments
More info: it is definitely related to resolving the serializer. Using SerializersModule {
polymorphic(Trait::class, Trait.serializer())
} doesn't help, it causes Full stack trace: "IllegalArgumentException: Serializer for Trait can't be registered as a subclass for polymorphic serialization because its kind SEALED is not concrete. To work with multiple hierarchies, register it as a base class.
at checkKind_0 (http://localhost:8080/static/spellbook.js:147781:13)
at PolymorphismValidator.polymorphic_1 (http://localhost:8080/static/spellbook.js:147759:5)
at SerialModuleImpl.dumpTo_0 (http://localhost:8080/static/spellbook.js:145927:19)
at validateConfiguration (http://localhost:8080/static/spellbook.js:146289:40)
at new JsonImpl (http://localhost:8080/static/spellbook.js:146263:5)
at Json_1 (http://localhost:8080/static/spellbook.js:146195:12)
at Json$default (http://localhost:8080/static/spellbook.js:146199:12)
at main (http://localhost:8080/static/spellbook.js:174473:18)
at Object.<anonymous> (http://localhost:8080/static/spellbook.js:181357:3)
at http://localhost:8080/static/spellbook.js:122208:37
at Object.<anonymous> (http://localhost:8080/static/spellbook.js:122211:2)
at Object.H:\Google Drive\My Stuff\spellbook\build\js\packages\spellbook\kotlin\spellbook.js (http://localhost:8080/static/spellbook.js:181361:30)
at __webpack_require__ (http://localhost:8080/static/spellbook.js:30:30)
at Object.0 (http://localhost:8080/static/spellbook.js:106:18)
at __webpack_require__ (http://localhost:8080/static/spellbook.js:30:30)
at http://localhost:8080/static/spellbook.js:94:18
at http://localhost:8080/static/spellbook.js:97:10
at webpackUniversalModuleDefinition (http://localhost:8080/static/spellbook.js:9:23)
at http://localhost:8080/static/spellbook.js:10:3" Specifying the subclasses explicitly without a base serializer does work. E.g.: polymorphic(Trait::class){
subclass(Trait.Attack::class, Trait.Attack.serializer())
subclass(Trait.Other::class, Trait.Other.serializer())
} Although note that this only fixes serialization/deserialization of the class, |
Yes, that's unfortunately a current limitation on Kotlin/JS and Kotlin/Native — |
Is this something that's planned to be fixed by 1.0 or soonish, or is it a longer term issue? |
It is likely won't be fixed in 1.0 but we have it on our radar for future releases |
@sandwwraith my kotlin/js serializer just hangs: @Serializable(with = LonjeDateSerializer::class)
actual class LonjeDate {
actual val format: String
get() {
return "format"
}
}
@Serializer(LonjeDate::class)
actual object LonjeDateSerializer : KSerializer<LonjeDate> {
override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("kotlinx.serialization.LonjeDateSerializer", PrimitiveKind.STRING)
override fun serialize(encoder: Encoder, value: LonjeDate) {
}
override fun deserialize(decoder: Decoder): LonjeDate {
return LonjeDate()
}
} is it known issue? |
As a workaround, it seems to work as long as the sealed class has type parameters. |
for sealed and abstract serializable classes. Fixes Kotlin/kotlinx.serialization#1116 Fixes Kotlin/kotlinx.serialization#1078
Is this issue fixed in 1.2.0? |
Nope, it will likely be fixed in Kotlin 1.5.20 |
for sealed and abstract serializable classes. Fixes Kotlin/kotlinx.serialization#1116 Fixes Kotlin/kotlinx.serialization#1078
for sealed and abstract serializable classes. Fixes Kotlin/kotlinx.serialization#1116 Fixes Kotlin/kotlinx.serialization#1078 (cherry picked from commit 66f39ca)
What version will this be fixed in for Kotlin/JS? I'm seeing it right now in Kotlin 1.5.21 and Kotlinx.Serialization 1.2.1. |
@nschulzke Do you use JS IR? |
Yes, I'm using IR. |
Can you post your reproducer, please? |
Ah, wait, I thought this issue would resolve the error "Serializer for ___ can't be registered as a subclass for polymorphic serialization because its kind SEALED is not concrete." It looks like that error was related, but not changed here. I'll ask a question on the Kotlin Slack and maybe file a different issue. Sorry for the confusion! |
I'm encountering the same issue with the IR compiler. If I switch to LEGACY, it works fine. Doing the test via JVM is also fine. build.gradle.kts:
AccessibleRegionsRequest.kt:
AccessibleRegionsTest.kt:
Test Output:
|
Any update about this? I have the same issue with IR backend, on Legacy it works fine |
I think it'll need to be re-opened as a new issue. |
I have a sealed class
Trait
in common code:I want to get it's serializer via
serializer<Trait>()
. On JVM this works fine and I get aSealedClassSerializer
. On JS (IR) I get:This is with
1.0.0-RC2
.The text was updated successfully, but these errors were encountered: