Skip to content

NullPointerException when decoding sealed class list #366

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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!

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

No branches or pull requests

2 participants