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

Default value for unknown Enum value #90

Closed
cosminstefanxp opened this issue Feb 22, 2018 · 11 comments
Closed

Default value for unknown Enum value #90

cosminstefanxp opened this issue Feb 22, 2018 · 11 comments
Labels

Comments

@cosminstefanxp
Copy link

cosminstefanxp commented Feb 22, 2018

In order to be forward-compatible and support certain scenarios, it would be useful to have a way to specify a default value for a deserialized Enum field, if an unknown value is encountered. That way, if a new type is defined and being returned by remote APIs, apps already in production will work properly in the intermediate period before migration.

Example:

enum class Status { ACTIVE, INACTIVE, UNKNOWN }
class Test(val status: Status = Status.UNKNOWN)

would return a Test(UNKNOWN) when provided with the following json: {'status': 'PENDING'}

@cosminstefanxp cosminstefanxp changed the title Default value for unkown Enum value Default value for unknown Enum value Feb 22, 2018
@simonovvasiliy
Copy link

Good use case, Jackson can do it and we rely its feature

@avently
Copy link

avently commented Feb 23, 2019

@sandwwraith
A year is gone. Any news related to enums?

@sandwwraith
Copy link
Member

@avently Not yet

@Larsmyrup
Copy link

This would be really useful. Does anyone know if this will be implemented?

@santoshastagi
Copy link

This is important for us too. Any other ways to do this?

@ManuelDCR
Copy link

This would be a really appreciated feature!
Any news @sandwwraith ?

@arberg
Copy link

arberg commented Apr 7, 2020

This is just to help others who need to solve this issue here and now: Bolinfest showed us how to do create a custom serializer here on issue #31

@igrmk
Copy link

igrmk commented May 3, 2020

@azabost
Copy link

azabost commented Aug 17, 2020

If you are interested in even better forward compatibility of enums, I would recommend using a wrapper like this: https://github.com/bright/codified. I have been using it for a quite long time and it works beautifully. Feel free to let me know what you think about it 😊

@tagy
Copy link

tagy commented May 25, 2021

Enabling coerceInputValues and setting default enum values in you class was easiest way to solve this at the moment I think.

@arberg
Copy link

arberg commented Mar 31, 2022

For those who need a solution here is a complete answer. https://github.com/igrmk/smsq/blob/532e3468266c8fc319c8ecca11be76f7026949a7/android/app/src/main/java/com/github/igrmk/smsq/entities/DeliveryResult.kt

I updated this example by @igrmk, and made it generic: https://gist.github.com/arberg/e20db05e018c61f37f1d274a254657c3

The code above annotates the enum-class with something like @Serializable(with = TestEnumSerializer::class), and it has the advantage that classes that use the enum does not have to do anything special. This is great, because when reviewing code for correctness we only have to review the enum-class and its annotation, not every single file that uses it now (or in the future).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests