Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support converter that returns undefined. #7738

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading