1

I know this is a very specific ask but I've got a situation where it would be very nice to have my custom class deserialized and have the collections parent ID set to a specific field. I know with the @DocumentId annotation we can do this for the documents own ID, is there some SDK method to extend this for my use case?

public class MyCustomClass {
    @DocumentId public documentID;
    ...

    @<insert magic here> public documentsCollectionsParentID

So for example, something like /users/<uid>/docs/<docid>, I already have the functionality for documentID to be set automatically to docID, but I'd like documentsCollectionsParentID set to uid. Is this in any way possible at the moment? My current alternative is to deserialize, and the the object afterword:

MyCustomClass thing = (MyCustomClass)doc.toObject(MyCustomClass.class);
thing.setDocumentsCollectionsParentID(doc.getReference().getParent().getParent().getId())
Mattwmaster58
  • 2,266
  • 3
  • 23
  • 36
  • I came across similar [question](https://stackoverflow.com/questions/56219469/firestore-get-the-parent-document-of-a-subcollection), let me know if it's helpful in your case. – mdobrucki Mar 16 '22 at 10:05
  • @mdobrucki Unfortunately, not much help as I already am able to do this manually, but seek a method to do this automatically – Mattwmaster58 Mar 16 '22 at 10:08

1 Answers1

0

Unfortunately, there is no method in Firebase that does that automatically. This is Java limitation and you are doing it correctly by deserializing the object.

mdobrucki
  • 462
  • 1
  • 7
  • 1
    "This is Java limitation" I'm not convinced it a limitation of Java itself, I could maybe see the SDK. After all if it can set a document ID field to be a certain property of the response, there's nothing fundamentally different between that and my use case. – Mattwmaster58 Mar 17 '22 at 14:53