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

Fix FirestoreDataConverter.fromFirestore() being called with QueryDocumentSnapshot from exp #4284

Merged
merged 12 commits into from
Jan 19, 2021

Conversation

dconeybe
Copy link
Contributor

@dconeybe dconeybe commented Jan 13, 2021

FirestoreDataConverter.fromFirestore() was being invoked with an instance of QueryDocumentSnapshot from the experimental SDK; however, it was expected to be invoked with an instance of QueryDocumentSnapshot from the Classic SDK. This worked okay, except that the object was lacking attributes like ref, which are only provided by the Classic SDK.

The root cause is that the FirestoreDataConverter provided by the user was registered directly with the QueryDocumentSnapshot from the experimental SDK, and therefore was invoked with types from the experimental SDK. The fix is to instead register a "wrapper" data converter with the QueryDocumentSnapshot that calls the underlying data converter with the equivalent objects from the Classic SDK.

There was, however, a side effect of this change that caused isEqual() methods (e.g. DocumentReference.isEqual()) to incorrectly return false if compared with another DocumentReference created with the same converter instance. This is because the implementation of isEqual() compares the converters using the === operator. The wrapper instances that wrapped the same converters were distinct objects, and thus did not compare equal using ===. The fix was to store each converter in a WeakMap mapping it to its wrapper instance so that the same wrapper instance can be used for each given converter, causing isEqual() to compare correctly.

Fixes #4278

@changeset-bot
Copy link

changeset-bot bot commented Jan 13, 2021

🦋 Changeset detected

Latest commit: 4aa48fe

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 8 packages
Name Type
@firebase/firestore Patch
firebase Patch
@firebase/rules-unit-testing Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Jan 13, 2021

Size Analysis Report

Affected Products

No changes between base commit (da7f7ff) and head commit (24c169f).

@dconeybe dconeybe changed the title WIP Fix FirestoreDataConverter.fromFirestore() being called with QueryDocumentSnapshot from exp Fix FirestoreDataConverter.fromFirestore() being called with QueryDocumentSnapshot from exp Jan 15, 2021
@dconeybe dconeybe marked this pull request as ready for review January 15, 2021 18:17
Copy link
Contributor

@schmidt-sebastian schmidt-sebastian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this fix! I have a couple of lame suggestions for you, but I am impressed. Especially with the PR description :)

packages/firestore/src/api/database.ts Outdated Show resolved Hide resolved
packages/firestore/src/api/database.ts Outdated Show resolved Hide resolved
packages/firestore/src/api/database.ts Outdated Show resolved Hide resolved
packages/firestore/test/integration/api/database.test.ts Outdated Show resolved Hide resolved
packages/firestore/test/integration/api/database.test.ts Outdated Show resolved Hide resolved
packages/firestore/test/integration/api/database.test.ts Outdated Show resolved Hide resolved
packages/firestore/test/integration/api/database.test.ts Outdated Show resolved Hide resolved
packages/firestore/src/api/database.ts Outdated Show resolved Hide resolved
packages/firestore/src/api/database.ts Outdated Show resolved Hide resolved
@dconeybe dconeybe merged commit 6ac66ba into master Jan 19, 2021
@dconeybe dconeybe deleted the dconeybe/WithConverterExpFix branch January 19, 2021 18:15
@google-oss-bot google-oss-bot mentioned this pull request Jan 19, 2021
@firebase firebase locked and limited conversation to collaborators Feb 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DocumentReference from QueryDocumentSnapshot in model converter not typed correctly.
3 participants