From d60caebffce249d19fec338713b69a34184afa06 Mon Sep 17 00:00:00 2001 From: Tom Andersen Date: Mon, 30 Oct 2023 11:19:20 -0400 Subject: [PATCH 1/3] Support converter that returns undefined. --- packages/firestore-compat/src/api/database.ts | 15 ++++++++---- .../firestore-compat/test/database.test.ts | 24 +++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/packages/firestore-compat/src/api/database.ts b/packages/firestore-compat/src/api/database.ts index f4e63c09d77..04e0d87b334 100644 --- a/packages/firestore-compat/src/api/database.ts +++ b/packages/firestore-compat/src/api/database.ts @@ -972,11 +972,16 @@ export class QueryDocumentSnapshot { 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; + } } } diff --git a/packages/firestore-compat/test/database.test.ts b/packages/firestore-compat/test/database.test.ts index 69237d74897..fd004754880 100644 --- a/packages/firestore-compat/test/database.test.ts +++ b/packages/firestore-compat/test/database.test.ts @@ -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. From 6b69abb6114f6443098b1116429095ced0aa3d2a Mon Sep 17 00:00:00 2001 From: Tom Andersen Date: Mon, 30 Oct 2023 12:00:11 -0400 Subject: [PATCH 2/3] Changeset --- .changeset/strong-coins-repeat.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/strong-coins-repeat.md diff --git a/.changeset/strong-coins-repeat.md b/.changeset/strong-coins-repeat.md new file mode 100644 index 00000000000..f9a52b827d5 --- /dev/null +++ b/.changeset/strong-coins-repeat.md @@ -0,0 +1,5 @@ +--- +'@firebase/firestore-compat': patch +--- + +Allow converter return value of undefined. From 24138787f0ea77a199f1d73d11628729c5d7b31b Mon Sep 17 00:00:00 2001 From: Tom Andersen Date: Mon, 30 Oct 2023 12:05:39 -0400 Subject: [PATCH 3/3] Format --- packages/firestore-compat/src/api/database.ts | 4 +-- .../firestore-compat/test/database.test.ts | 26 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/packages/firestore-compat/src/api/database.ts b/packages/firestore-compat/src/api/database.ts index 04e0d87b334..e7cbe8196d6 100644 --- a/packages/firestore-compat/src/api/database.ts +++ b/packages/firestore-compat/src/api/database.ts @@ -977,8 +977,8 @@ export class QueryDocumentSnapshot return data as T; } else { _debugAssert( - data !== undefined, - 'Document in a QueryDocumentSnapshot should exist' + data !== undefined, + 'Document in a QueryDocumentSnapshot should exist' ); return data; } diff --git a/packages/firestore-compat/test/database.test.ts b/packages/firestore-compat/test/database.test.ts index fd004754880..00612972c0c 100644 --- a/packages/firestore-compat/test/database.test.ts +++ b/packages/firestore-compat/test/database.test.ts @@ -1651,19 +1651,19 @@ apiDescribe('Database', (persistence: boolean) => { 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; - } - }); + .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();