We use polymorphic types extensively in our Androi...
# serialization
s
We use polymorphic types extensively in our Android app, and I find myself writing a
SerializerModule
with a
defaultDeserializer
pretty much every time:
Copy code
@Serializable
sealed interface RewardsCapabilityDto {
    @Serializable
    @SerialName("Available")
    data class Available(val level: RewardsLevel) : RewardsCapabilityDto

    @Serializable
    @SerialName("Unavailable")
    data object Unavailable : RewardsCapabilityDto

    // Fallback in case we get a `"type"` that we don't recognize
    @Serializable
    data object Unknown : RewardsCapabilityDto
}

val MembershipDtoJsonSerializersModule = SerializersModule {
    polymorphic(RewardsCapabilityDto::class) {
        defaultDeserializer { RewardsCapabilityDto.Unknown.serializer() }
    }
}
Is there an annotation or something we could put on the sealed interface instead? I know I could write my own polymorphic serializer, but that’s also not what I want. Something akin to this is what I’m thinking:
Copy code
@Serializable
@DefaultDeserializer(Unknown) // probably can't reference .serializer() here?
sealed interface RewardsCapabilityDto {
    // ...
}
e
I had impression it just happens automatically. Maybe
Unknown
is making need to write module by hand
s
What happens automatically? I mean specifically to have a fallback if we get a type that we haven't covered in our subclasses.
e
I meant, that I don’t have many polymorphic cases but I don’t write modules for sealed hierarchies. But we also don’t have unknown since we consider it is bad API and every enum change (even addition) requires API version bump.
s
We do it mostly for compatibility reasons, in case we introduce new types in the future.
e
Understand and it is reasonable. We just though oke, client gets something it doesn’t understand - is it reasonable to have uknown? What UX will do with it? Filter out? Report broken? And I would say it is probably one sided. If we go this approach at least we can possibility to have some strategy for unknown (not sure which) and we have more flexibility to decide if we need to work on this sooner or later.
Sorry for the offtopic
s
Case-by-case how the client deals with it, but filtering them out can be an option. Custom error message can be another. 👍
e
Now you have to remember all these case by cases. Maybe document it 🙂
s
I'd have to case-by-case the whole thing if it threw exceptions instead, though, which would be the default behaviour? Or maybe I'm not following?
Following up on this, if the sealed type was instead an enum, I could use a default parameter value:
Copy code
data class UserDto(
  val rewardsCapability: RewardsCapabilityDto = RewardsCapabilityDto.Unknown
)

enum class RewardsCapabilityDto {
    Available,
    Unavailable,
    Unknown,
}
With this, if I were to pass any unrecognized value, it’d fall back to
Unknown
. However, the same doesn’t seem to work for sealed types.
I filed github.com/Kotlin/kotlinx.serialization/issues/3320, in case anyone wants to follow this further.
✅ 1