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

[cloud_firestore] Fix DocumentReference comparison bug. #2524

Closed
wants to merge 1 commit into from

Conversation

slightfoot
Copy link
Contributor

Description

I was just caught out by this.

void main() {
  final first = Firestore.instance.collection('a').document('b');
  final second = Firestore.instance.collection('a').document('b');
  print(first == second); // prints false
}

You expect two DocumentReferences to be equal. https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_firestore/cloud_firestore/lib/src/document_reference.dart#L25 shows this to be the case if the firestore instance is the same and the instance getter above does not return a singleton instead it creates a new instance each time.

This fix corrects this by firstly treating the Firestore.instance field as a singleton, and second adding a equality method that now compares the underlying app. Which now means even if you have multiple instances of the same Firestore class they will be considered equal.

Tests

  • cloud_firestore package does not contain tests.

Checklist

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • If the pull request affects only one plugin, the PR title starts with the name of the plugin in brackets (e.g. [cloud_firestore])
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See [Contributor Guide]).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (flutter analyze) does not report any problems on my PR.
  • I read and followed the [Flutter Style Guide].
  • I updated pubspec.yaml with an appropriate new version according to the [pub versioning philosophy].
  • I updated CHANGELOG.md to add a description of the change.
  • I signed the [CLA].
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change (please indicate a breaking change in CHANGELOG.md and increment major revision).
  • No, this is not a breaking change.

@slightfoot
Copy link
Contributor Author

/cc @bparrishMines @collinjackson not sure who I should reference for this simple ticket. Can you have a look and tell me where the tests should go?

@slightfoot slightfoot mentioned this pull request May 20, 2020
@bparrishMines bparrishMines self-requested a review May 20, 2020 17:33
Copy link
Contributor

@bparrishMines bparrishMines left a comment

Choose a reason for hiding this comment

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

@slightfoot The code looks good to me. It only lacks lacks a test and updating the pubspec.yaml and CHANGELOG. It looks like tests were placed in flutterfire/packages/cloud_firestore/cloud_firestore/example/test_driver/ for this plugin.

/// Gets the instance of Firestore for the default Firebase app.
static Firestore get instance => Firestore();
static Firestore get instance => _instance ??= Firestore();
Copy link
Contributor

Choose a reason for hiding this comment

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

@@ -98,4 +101,14 @@ class Firestore {
host: host,
sslEnabled: sslEnabled,
cacheSizeBytes: cacheSizeBytes);

@override
int get hashCode => _delegate.app.name.hashCode;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
int get hashCode => _delegate.app.name.hashCode;
int get hashCode => _delegate.app.hashCode;

@slightfoot

The hashCode of FirebaseApp is implemented appropriately, so .name seems redundant.

@duttaoindril
Copy link

Glad this is finally getting attention! i've literally had to wrap DocumentReference in a wrapper DocumentReferenceKey class I've created to use DocumentReferences as keys in all my query result maps.

@Salakar Salakar requested a review from Ehesp July 10, 2020 16:52
@Ehesp Ehesp closed this in #2942 Jul 13, 2020
@firebase firebase locked and limited conversation to collaborators Aug 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants