-
Notifications
You must be signed in to change notification settings - Fork 9
Description
This is not a bug per-se, but more of an attempt from me to ask you about your thought about how to best work with enums in destinations when also having to work with deep links.
After looking at the implementation and debugging some code, it turns out that enums are serialized simply by their ordinal. In my case I got a destination with a nullable enum class as a parameter. For the deep link, I can add it to the uriPattern as ...?paramName={paramName}
, and then when I create an actual deep link for it, I need to do ...?paramName=0
or whatever index of that enum over there.
This means that if for example we do a follow-up release with a new enum entry, if I try to send that deep link to people out in the wild, if they still got an old version of the app, trying to open the deep link will simply crash their app, as kotlinx.serialization will try to decode that by running this line https://github.com/Kotlin/kotlinx.serialization/blob/1116f5f13a957feecda47d5e08b0aa335fc010fa/core/commonMain/src/kotlinx/serialization/internal/Enums.kt#L140 and since the index is not there it crashes the entire application.
My crash for example is kotlinx.serialization.SerializationException: 5 is not among valid com.android.MyClass enum values, values size is 5
What do you think is the best approach to make this process a bit more robust and forwards compatible? I could have the type in the destination itself be a String? for example, and then have to do in my own code a check to find any possible matches, or default to my own entry if not, something like:
val input = destinationInstance.deepLinkPopulatedField
val resolvedEnum = MyEnum.entries.firstOrNull {
it.name == input
} ?: MyEnum.Default
Am I missing some smarter approach here? Have you encountered this before perhaps, and if yes what did you end up doing?