kotlinx.serialization: serializer() throws SerializationException when used with sealed class on JS

I have a sealed class Trait in common code:

@Serializable
sealed class Trait() {
    abstract val name: String
    ...
}

I want to get it’s serializer via serializer<Trait>(). On JVM this works fine and I get a SealedClassSerializer. On JS (IR) I get:

"SerializationException: Serializer for class 'Trait' is not found.
Mark the class as @Serializable or provide the serializer explicitly.
On Kotlin/JS explicitly declared serializer should be used for interfaces and enums without @Serializable annotation
    at platformSpecificSerializerNotRegistered (http://localhost:8080/static/spellbook.js:159984:11)
    at serializer (http://localhost:8080/static/spellbook.js:155641:7)
    at main (http://localhost:8080/static/spellbook.js:188469:25)
    at Object.<anonymous> (http://localhost:8080/static/spellbook.js:195282:3)
    at http://localhost:8080/static/spellbook.js:136257:37
    at Object.<anonymous> (http://localhost:8080/static/spellbook.js:136260:2)
    at Object.H:\Google Drive\My Stuff\spellbook\build\js\packages\spellbook\kotlin\spellbook.js (http://localhost:8080/static/spellbook.js:195286: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"

This is with 1.0.0-RC2.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 16 (5 by maintainers)

Commits related to this issue

Most upvoted comments

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:

plugins {
    kotlin("multiplatform") version "1.7.+"
    kotlin("plugin.serialization") version "1.7.+"
    application
}

AccessibleRegionsRequest.kt:

import kotlinx.serialization.Serializable
@Serializable
data class AccessibleRegionsRequest(
    val worldId: String,
)

AccessibleRegionsTest.kt:

class AccessibleRegionsTest {
    @Test
    fun testFakeCall() {
        val req = AccessibleRegionsRequest(
            worldId = "foo-bar",
        )
        val reqs = Json.encodeToString(req)
        assertEquals("""{"worldId":"foo-bar"}""", reqs)
        val req2 = Json.decodeFromString<AccessibleRegionsRequest>(reqs)
        assertEquals(req, req2)
    }
}

Test Output:

SerializationException: Serializer for class 'AccessibleRegionsRequest' is not found.
Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.
On Kotlin/JS explicitly declared serializer should be used for interfaces and enums without @Serializable annotation
SerializationException: Serializer for class 'AccessibleRegionsRequest' is not found.
Please ensure that class is marked as '@Serializable' and that the serialization compiler plugin is applied.
On Kotlin/JS explicitly declared serializer should be used for interfaces and enums without @Serializable annotation
	at <global>.platformSpecificSerializerNotRegistered(C:/Users/bcwhite/AppData/Local/Temp/_karma_webpack_539458/commons.js:40973)
	at <global>.serializer(C:/Users/bcwhite/AppData/Local/Temp/_karma_webpack_539458/commons.js:35040)
	at com.riverworth.deity.common.api.AccessibleRegionsTest.testFakeCall_flu7nw(C:/Users/bcwhite/AppData/Local/Temp/_karma_webpack_539458/commons.js:5455)
	at <global>.test_fun$AccessibleRegionsTest_test_fun$testFakeCall_test_fun_9hdub3(C:/Users/bcwhite/AppData/Local/Temp/_karma_webpack_539458/commons.js:5486)
	at Context.<anonymous>(C:/Users/bcwhite/AppData/Local/Temp/_karma_webpack_539458/commons.js:47604)