firestoreodm-flutter icon indicating copy to clipboard operation
firestoreodm-flutter copied to clipboard

batchSet and transactionSet should rely on castedReference like set

Open gmarizy opened this issue 1 year ago • 5 comments

Fixes #37

In generated code, relying on batch.set(reference, json, options); for batchSet implementation is wrong because we pass a DocumentReference<T> with T a mapping class as a reference with a json data. Cloud_firestore then try to apply the converter but data is already in json. See write_batch.dart from cloud_firestore, line 56.

batch.set(reference, json, options);

Should be replaced with :

    final castedReference = reference.withConverter<Map<String, dynamic>>(
      fromFirestore: (snapshot, options) => throw UnimplementedError(),
      toFirestore: (value, options) => value,
    );

    batch.set(castedReference, json, options);

Like it's done for set method. Same for transaction.

  • [ ] Tests pass
  • [ ] Appropriate changes to README are included in PR

gmarizy avatar Dec 10 '24 00:12 gmarizy

Thanks! Could you add tests for those?

rrousselGit avatar Dec 10 '24 12:12 rrousselGit

Batch and transaction set are already covered in example/integration_test/document_reference_test.dart. I wanted to run those tests before and after fix for validation, but didn't succeed running them.

gmarizy avatar Dec 10 '24 13:12 gmarizy

I should have mentioned that if I couldn't run the test suit, the proposed fix work fine on real projects.

gmarizy avatar Dec 12 '24 17:12 gmarizy

Any progress on this? Having this exact issue now where I wanted to use batchSet and it wouldn't work.

andreasmpet avatar Feb 27 '25 12:02 andreasmpet

Still waiting review

gmarizy avatar Feb 27 '25 19:02 gmarizy