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

Sealed Interfaces with named companion objects do not generate serializers #2163

Open
Shengaero opened this issue Jan 21, 2023 · 1 comment

Comments

@Shengaero
Copy link

Describe the bug
Named companion objects of sealed interfaces do not auto-generate serializers when tagged with @Serializable.

Note that this only occurs when the companion object is named, if unnamed this error will not occur.

To Reproduce

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
sealed interface MyInterface {
    companion object NamedCompanionObject {
        const val Example = "testing"
    }
}

@Serializable
@SerialName("FooBar")
data class FooBar(val value: String): MyInterface

fun main() {
    // error will be thrown here, reporting that MyInterface is not tagged with @Serializable
    val text = Json.encodeToString<MyInterface>(FooBar(MyInterface.Example))
    println(text)
}

Expected behavior
This should not throw an error, or at least I don't believe it should.
Nothing in the documentation I could find implies this is expected behavior.

Environment

  • Kotlin version: 1.8.0
  • Library version: 1.4.1
  • Kotlin platforms: JVM
  • Gradle version: 7.4.2
  • IDE version: Unsure if related but IntelliJ IDEA 2022.2.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants