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

DocumentReference serialization not supported?

Open levictorsmith opened this issue 3 years ago • 2 comments
trafficstars

If you have a reference as a child field of a document, how would one go about serializing/deserializing that reference?

It seems that directly using DocumentReference in your object structure won't work since it is not a @Serializable, and using the referenced document type throws an exception

e.g. in android it throws

java.lang.ClassCastException: com.google.firebase.firestore.DocumentReference cannot be cast to java.util.Map

Trying to figure this one out and any help would be appreciated. I was also looking at creating a potential PR, but hesitated because I'm not confident in the other platforms 🤷

levictorsmith avatar Jun 16 '22 01:06 levictorsmith

Here's an example problem:

db.collection("things").document("thing").get().data(Thing.serializer())
// ...
@Serializable
data class Thing(
    val someRef: DocumentReference // Complains that DocumentReference is not serializable, and can't do @Contextual
)
// or
@Serializable
data class Thing(
    val someRef: String // Does not fail, but serializes the reference to the default kotlin .toString() method
)
// or
@Serializable
data class Thing(
    val someRef: OtherCollectionDocument // Fails with the error mentioned above
)

levictorsmith avatar Jun 17 '22 01:06 levictorsmith

we'll need to add specific support for reference fields in the serialization support - how do they work with the standard serialization for android or js, aka what is the expected behavior?

nbransby avatar Oct 29 '22 13:10 nbransby