question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

enum deserialization of missing values to null

See original GitHub issue

My domain class has one field as an enum.

Recently I decided to change the enum values for a better readeability. But know when trying to load users from firebase i get the exception.

Giveaway giv = ds.getValue(Giveaway.class);

Process: com.tomatedigital.instagramgiveawaywinner, PID: 21593
       com.google.firebase.database.DatabaseException: Could not find enum value of com.tomatedigital.giveawaymaster.domain.CommentListType for value "COMMON_NAMES"
           at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToEnum(com.google.firebase:firebase-database@@19.2.1:314)
           at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.2.1:229)
           at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(com.google.firebase:firebase-database@@19.2.1:178)
           at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(com.google.firebase:firebase-database@@19.2.1:47)
           at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@19.2.1:592)
           at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@19.2.1:562)
           at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@19.2.1:432)
           at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.2.1:231)
           at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@19.2.1:79)
           at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@19.2.1:203)
           at com.tomatedigital.giveawaymaster.firabase.LoginValueEventListener$3.onDataChange(LoginValueEventListener.java:178)
           at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@19.2.1:179)
           at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.1:75)
           at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.1:63)
           at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.1:55)
           at android.os.Handler.handleCallback(Handler.java:883)
           at android.os.Handler.dispatchMessage(Handler.java:100)
           at android.os.Looper.loop(Looper.java:214)
           at android.app.ActivityThread.main(ActivityThread.java:7356)
           at java.lang.reflect.Method.invoke(Native Method)
           at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

COMMON_NAMES was changed to something else.

I completely understand the reason of this exception… my question is “are there any workaround?”…

I wish i could tell firebase to map unexisting enum values to null or any value, so then i could manually deal with them…

Is a very complicated restriction to throw a fatal exception like this on deserialization for a missing value… it means that ALL APPS RUNNING FIREBASE WONT BE ALLOWED TO REMOVE ENUM VALUES FOREVER…

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
dconeybecommented, Apr 12, 2022

@risalfajar That is a great idea. If we ever implement this feature then we will likely do just that for Kotlin if it’s technically possible.

0reactions
risalfajarcommented, Apr 8, 2022

I am using Kotlin and I’ve experienced this unknown enum problem. However, in my case, it’s a little bit different since I’m using Kotlin.

I have a simple data class that contains an enum

data class User(
    val authority: Authority = Authority.USER // default value provided
)

enum class Authority {
    USER, SUPER_ADMIN
}

Let’s say that in the DB there’s a User with an authority of GENERAL_ADMIN, it will lead to a crash since it’s unknown to the app.

I think instead of throwing an exception and causing a crash, the serializer should use the default value provided instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deserialization null value to enum with Jackson - Stack Overflow
I tried to create some method annotated by @JsonCreator , creating own enum deserializer, using @JsonEnumDefaultValue but none of these ...
Read more >
Json Enum deserialization breakes kotlin null-safety - Android
I use Kotlin data classes and GSON to deserialize JSON schemas, and implement default values to protect against null-objects in JSON.
Read more >
DeserializationFeature (jackson-databind 2.6.0 API) - FasterXML
public enum DeserializationFeature extends Enum<DeserializationFeature> implements ... Feature that allows unknown Enum values to be parsed as null values.
Read more >
Language Guide (proto3) | Protocol Buffers - Google Developers
During deserialization, unrecognized enum values will be preserved in the message, though how this is represented when the message is deserialized is language- ......
Read more >
How To Serialize and Deserialize Enums with Jackson
We can use the same annotation for deserialization as well. This is possible because Enum values are constants. First, let's use @JsonValue with ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found