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

NullPointerException when decoding sealed class list #366

Closed
brahyam opened this issue Apr 7, 2023 · 1 comment
Closed

NullPointerException when decoding sealed class list #366

brahyam opened this issue Apr 7, 2023 · 1 comment

Comments

@brahyam
Copy link
Contributor

brahyam commented Apr 7, 2023

Hi!,

Thanks a lot for the huge effort of making available this library.

While using the SDK to store a list of sealed classes I ran into the following error on decoding.
NullPointerException: null cannot be cast to non-null type kotlin.collections.Map<*, *>

I managed to track it to firebase-common/androidMain/_decoders.kt FirebaseDecoder.structureDecoder function. (and also its iOS equivalent)

It happens because the descriptor is expecting a value property that was not encoded. I managed to fix it with the following

StructureKind.CLASS, StructureKind.OBJECT, PolymorphicKind.SEALED -> (value as Map<*, *>).let { map ->
        FirebaseClassDecoder(decodeDouble, map.size, { map.containsKey(it) }) { desc, index ->
            val elementName = desc.getElementName(index)
            if (desc.kind is PolymorphicKind && elementName == "value") {
                map
            } else {
                map[desc.getElementName(index)]
            }
        }
    }

And similar for the iOS but maybe there is a more elegant way?

I created a PR with failing tests that points out the problem.
#365

@nbransby
Copy link
Member

PR has been merged, thanks for your contribution!

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

No branches or pull requests

2 participants