https://kotlinlang.org logo
Title
p

Paul Woitaschek

04/26/2023, 1:40 PM
Why doesn’t kotlin serialization complain about the interface not being serializable here?
Code for those who can’t see images:
@Serializable
data class SerializableSomething(
  val i: SomeInterface,
  val b: SomeClass, <- error here
)

interface SomeInterface

class SomeClass
Background: we just had a crash where a sealed interface was not marked as Serializable and relied upon that kotlinx usually fails the compilation if a type is not serializable
a

Adam S

04/26/2023, 2:20 PM
https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/polymorphism.md#serializing-interfaces
all interfaces are considered to be implicitly serializable with the PolymorphicSerializer strategy
here’s a relevant bit from the docs, but I’m not sure if it might help!
p

Paul Woitaschek

04/26/2023, 2:22 PM
Uh dang. That takes a big portion of the compiler time safety away
I really want to disable that behavior
a

Adam S

04/26/2023, 2:23 PM
if it was disabled, what serializer would you use for
SomeInterface
?
p

Paul Woitaschek

04/26/2023, 3:31 PM
Either the generated serializer if it's marked with Serializable or the one specified in Serializable(with)