Skip to content

Commit

Permalink
Support converter that returns undefined. (#7738)
Browse files Browse the repository at this point in the history
* Support converter that returns undefined.

* Changeset

* Format
  • Loading branch information
tom-andersen authored Oct 30, 2023
1 parent 12ad9f1 commit 67c5a90
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-coins-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/firestore-compat': patch
---

Allow converter return value of undefined.
15 changes: 10 additions & 5 deletions packages/firestore-compat/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -972,11 +972,16 @@ export class QueryDocumentSnapshot<T = PublicDocumentData>
{
data(options?: PublicSnapshotOptions): T {
const data = this._delegate.data(options);
_debugAssert(
data !== undefined,
'Document in a QueryDocumentSnapshot should exist'
);
return data;
if (this._delegate._converter) {
// Undefined is a possible valid value from converter.
return data as T;
} else {
_debugAssert(
data !== undefined,
'Document in a QueryDocumentSnapshot should exist'
);
return data;
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions packages/firestore-compat/test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1647,6 +1647,30 @@ apiDescribe('Database', (persistence: boolean) => {
expect(untypedDocRef.isEqual(ref)).to.be.true;
});
});

it('for DocumentReference.withConverter() that returns undefined', () => {
return withTestDb(persistence, async db => {
const docRef = db
.collection('posts')
.doc()
.withConverter({
toFirestore(post: Post): firestore.DocumentData {
return { title: post.title, author: post.author };
},
fromFirestore(
snapshot: firestore.QueryDocumentSnapshot,
options: firestore.SnapshotOptions
): Post | undefined {
return undefined;
}
});

await docRef.set(new Post('post', 'author'));
const postData = await docRef.get();
const post = postData.data();
expect(post).to.equal(undefined);
});
});
});

// TODO(b/196858864): This test regularly times out on CI.
Expand Down

0 comments on commit 67c5a90

Please sign in to comment.