firebase-kotlin-sdk icon indicating copy to clipboard operation
firebase-kotlin-sdk copied to clipboard

function returning an array of objects

Open hohteri opened this issue 4 years ago • 1 comments
trafficstars

I'm not sure if this is a bug or not but I have a simple function returning an array of objects:

exports.leaderboard = functions.https.onCall((data, context) => {
    return 
        [
            {rank: 5, playerId: "xxx", score: 500.0},
            {rank: 6, playerId: context.auth.uid, score: 521.0},
            {rank: 7, playerId: "yyy", score: 534.0}
        ]
})

I try to retrieve the document like this:

@Serializable
    data class LeaderboardEntry(val rank: Int, val playerId: String, val score: Double)

    suspend fun leaderBoard() : List<LeaderboardEntry> {
        val f = Firebase.functions.httpsCallable("leaderboard")
        return f().data<List<LeaderboardEntry>>()
    }

In iOS, the app crashes with: Uncaught Kotlin exception: kotlin.ClassCastException: null cannot be cast to kotlin.collections.List

I can wrap the list into an object in the function and deserialise with:

@Serializable
    data class Leaderboard(val entries: List<LeaderboardEntry>)

in which case it works as expected.

hohteri avatar Sep 27 '21 06:09 hohteri

Would it be possible for you to add a failing test for this as a PR? Does it work on android?

nbransby avatar Oct 13 '21 21:10 nbransby