Skip to content

Commit

Permalink
Return Firestore Classic types from Transaction API (#4233)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidt-sebastian committed Dec 23, 2020
1 parent dec74aa commit ba59a0f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/slimy-mugs-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/firestore": patch
---

Fixes an issue in the Transaction API that caused the SDK to return invalid DocumentReferences through `DocumentSnapshot.data()` calls.
18 changes: 17 additions & 1 deletion packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,14 @@ export function setLogLevel(level: PublicLogLevel): void {
export class Transaction
extends Compat<ExpTransaction>
implements PublicTransaction {
private _userDataWriter: UserDataWriter;

constructor(
private readonly _firestore: Firestore,
delegate: ExpTransaction
) {
super(delegate);
this._userDataWriter = new UserDataWriter(_firestore);
}

get<T>(
Expand All @@ -419,7 +422,20 @@ export class Transaction
const ref = castReference(documentRef);
return this._delegate
.get(ref)
.then(result => new DocumentSnapshot(this._firestore, result));
.then(
result =>
new DocumentSnapshot(
this._firestore,
new ExpDocumentSnapshot<T>(
this._firestore._delegate,
this._userDataWriter,
result._key,
result._document,
result.metadata,
ref._converter
)
)
);
}

set<T>(
Expand Down
6 changes: 6 additions & 0 deletions packages/firestore/test/integration/api/type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ apiDescribe('Firestore', (persistence: boolean) => {
let docSnapshot = await doc.get();
expect(docSnapshot.data()).to.deep.equal(data);

// Validate that the transaction API returns the same types
await db.runTransaction(async transaction => {
docSnapshot = await transaction.get(doc);
expect(docSnapshot.data()).to.deep.equal(data);
});

if (validateSnapshots) {
let querySnapshot = await collection.get();
docSnapshot = querySnapshot.docs[0];
Expand Down

0 comments on commit ba59a0f

Please sign in to comment.