Skip to content

Commit baf6543

Browse files
authored
fix(firestore,windows): fix an issue that could happen when querying by DocumentReference value (#18053)
* fix(firestore,windows): fix an issue that could happen when querying by DocumentReference value * format
1 parent c13040e commit baf6543

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

packages/cloud_firestore/cloud_firestore/example/integration_test/document_reference_e2e.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,5 +629,39 @@ void runDocumentReferenceTests() {
629629
timeout: const Timeout.factor(3),
630630
);
631631
});
632+
633+
group('DocumentReference as field value', () {
634+
// Regression test for https://github.com/firebase/flutterfire/issues/18028
635+
test('can store and read a DocumentReference as a field value', () async {
636+
final doc = await initializeTest('doc-ref-field');
637+
final targetDoc = firestore.doc('flutter-tests/target-doc');
638+
639+
await doc.set({'ref': targetDoc});
640+
641+
final snapshot = await doc.get();
642+
final refValue = snapshot.data()!['ref'];
643+
expect(refValue, isA<DocumentReference>());
644+
expect((refValue as DocumentReference).path, targetDoc.path);
645+
});
646+
647+
test('can query by DocumentReference value', () async {
648+
final collection =
649+
firestore.collection('flutter-tests/doc-ref-query/items');
650+
final targetDoc = firestore.doc('flutter-tests/target-doc');
651+
652+
// Clean up
653+
final existing = await collection.get();
654+
for (final doc in existing.docs) {
655+
await doc.reference.delete();
656+
}
657+
658+
await collection.add({'ref': targetDoc, 'name': 'test'});
659+
660+
final querySnapshot =
661+
await collection.where('ref', isEqualTo: targetDoc).get();
662+
expect(querySnapshot.docs, hasLength(1));
663+
expect(querySnapshot.docs.first.data()['name'], 'test');
664+
});
665+
});
632666
});
633667
}

packages/cloud_firestore/cloud_firestore/windows/firestore_codec.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,23 @@ cloud_firestore_windows::FirestoreCodec::ReadValueOfType(
212212
std::get<CustomEncodableValue>(
213213
FirestoreCodec::ReadValue(stream)));
214214

215-
if (CloudFirestorePlugin::firestoreInstances_.find(appName) !=
215+
// Use composite key matching GetFirestoreFromPigeon to avoid
216+
// creating a duplicate unique_ptr for the same Firestore instance.
217+
// See https://github.com/firebase/flutterfire/issues/18028
218+
std::string cacheKey = appName + "-" + databaseUrl;
219+
220+
if (CloudFirestorePlugin::firestoreInstances_.find(cacheKey) !=
216221
CloudFirestorePlugin::firestoreInstances_.end()) {
217222
return CustomEncodableValue(
218-
CloudFirestorePlugin::firestoreInstances_[appName].get());
223+
CloudFirestorePlugin::firestoreInstances_[cacheKey].get());
219224
}
220225

221226
firebase::App* app = firebase::App::GetInstance(appName.c_str());
222227

223-
Firestore* firestore = Firestore::GetInstance(app);
228+
Firestore* firestore = Firestore::GetInstance(app, databaseUrl.c_str());
224229
firestore->set_settings(settings);
225230

226-
CloudFirestorePlugin::firestoreInstances_[appName] =
231+
CloudFirestorePlugin::firestoreInstances_[cacheKey] =
227232
std::unique_ptr<firebase::firestore::Firestore>(firestore);
228233

229234
return CustomEncodableValue(firestore);

0 commit comments

Comments
 (0)