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] DocumentReference in startAfterDocument snapshot causes crash (invalid encode argument) #2044

Closed
creativecreatorormaybenot opened this issue Feb 22, 2020 · 17 comments · Fixed by #2105
Assignees
Labels
type: bug Something isn't working

Comments

@creativecreatorormaybenot
Copy link
Contributor

creativecreatorormaybenot commented Feb 22, 2020

@amrfarid140 @ditman I think I have found another one of the .valueEncode issues as discussed here. See also: #1986.

The error I get is Invalid argument: Instance of 'DocumentReference'.

Full stack trace
E/flutter (14093): #0      StandardMessageCodec.writeValue (package:flutter/src/services/message_codecs.dart:392:7)
E/flutter (14093): #1      FirestoreMessageCodec.writeValue (package:cloud_firestore_platform_interface/src/method_channel/utils/firestore_message_codec.dart:83:13)
E/flutter (14093): #2      StandardMessageCodec.writeValue.<anonymous closure> (package:flutter/src/services/message_codecs.dart:389:9)
E/flutter (14093): #3      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8)
E/flutter (14093): #4      StandardMessageCodec.writeValue (package:flutter/src/services/message_codecs.dart:387:13)
E/flutter (14093): #5      FirestoreMessageCodec.writeValue (package:cloud_firestore_platform_interface/src/method_channel/utils/firestore_message_codec.dart:83:13)
E/flutter (14093): #6      StandardMessageCodec.writeValue.<anonymous closure> (package:flutter/src/services/message_codecs.dart:389:9)
E/flutter (14093): #7      _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8)
E/flutter (14093): #8      StandardMessageCodec.writeValue (package:flutter/src/services/message_codecs.dart:387:13)
E/flutter (14093): #9      FirestoreMessageCodec.writeValue (package:cloud_firestore_platform_interface/src/method_channel/utils/firestore_message_codec.dart:83:13)
E/flutter (14093): #10     StandardMessageCodec.writeValue.<anonymous closure> (package:flutter/src/services/message_codecs.dart:389:9)
E/flutter (14093): #11     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8)
E/flutter (14093): #12     MapView.forEach (dart:collection/maps.dart:337:10)
E/flutter (14093): #13     StandardMessageCodec.writeValue (package:flutter/src/services/message_codecs.dart:387:13)
E/flutter (14093): #14     FirestoreMessageCodec.writeValue (package:cloud_firestore_platform_interface/src/method_channel/utils/firestore_message_codec.dart:83:13)
E/flutter (14093): #15     StandardMessageCodec.writeValue.<anonymous closure> (package:flutter/src/services/message_codecs.dart:389:9)
E/flutter (14093): #16     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:379:8)
E/flutter (14093): #17     StandardMessageCodec.writeValue (package:flutter/src/services/message_codecs.dart:387:13)
E/flutter (14093): #18     FirestoreMessageCodec.writeValue (package:cloud_firestore_platform_interface/src/method_channel/utils/firestore_message_codec.dart:83:13)
E/flutter (14093): #19     StandardMethodCodec.encodeMethodCall (package:flutter/src/services/message_codecs.dart:524:18)
E/flutter (14093): #20     MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:148:13)
E/flutter (14093): #21     MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:329:12)
E/flutter (14093): #22     MethodChannel.invokeMapMethod (package:flutter/src/services/platform_channel.dart:356:48)
E/flutter (14093): #23     MethodChannelQuery.getDocuments (package:cloud_firestore_platform_interface/src/method_channel/method_channel_query.dart:87:46)
E/flutter (14093): #24     Query.getDocuments (package:cloud_firestore/src/query.dart:37:34)

Setup

The error happens every time a document has a DocumentReference as a value for a field and the snapshot is passed to startAfterDocument with this line (the getDocuments call causes it):

query.startAfterDocument(documentSnapshot).limit(8).getDocuments()

This was recently introduced with 0.13.0. I am currently using cloud_firestore version 0.13.3.

@creativecreatorormaybenot creativecreatorormaybenot added the type: bug Something isn't working label Feb 22, 2020
@ditman
Copy link
Contributor

ditman commented Feb 24, 2020

Hey @creativecreatorormaybenot, can you share what your query and documentSnapshot look like?

@creativecreatorormaybenot
Copy link
Contributor Author

creativecreatorormaybenot commented Feb 24, 2020

@ditman I have this debug log, which should be the exact same query (apart from the limit, but that value varies). Let me know if I should provide it in a different format.

Regarding the snapshot, I guess the most useful input here would be sharing the data types. The documents that are queried consist of:

Field types
  • int
  • float
  • list (of strings)
  • map (string: int)
  • timestamp
  • document reference

Some occur multiple times, e.g. multiple int fields.

This is for sure due to startAfterDocument and the document reference as a field of the document snapshot I pass. I am probably responsible for most of the code for this, but the refactoring seems to have broken it.

@ditman
Copy link
Contributor

ditman commented Feb 24, 2020

Yes, this is probably related to the document passed to startAfterDocument containing a DocumentReference. I'm surprised you say this only happens sometimes and not always, though!

@creativecreatorormaybenot
Copy link
Contributor Author

creativecreatorormaybenot commented Feb 24, 2020

@ditman Oh, I know why. Sorry for not thinking carefully.

The reason why it happens only sometimes is that the documents in the query mostly have the document reference field as null. So the error only occurs when a document reference is actually in Cloud Firestore. When the field is null, it does not happen (obviously).


Every time a document has a DocumentReference as a value for a field and the snapshot is passed to startAfterDocument, the error occurs.

@ditman
Copy link
Contributor

ditman commented Feb 24, 2020

Ah, cool, that makes sense! I'll take a look ASAP, if @amrfarid140 doesn't get to this before I do!

@creativecreatorormaybenot creativecreatorormaybenot changed the title [cloud_firestore] getDocuments: Invalid argument: Instance of 'DocumentReference' [cloud_firestore] DocumentReference in startAfterDocument snapshot causes crash Feb 24, 2020
@creativecreatorormaybenot creativecreatorormaybenot changed the title [cloud_firestore] DocumentReference in startAfterDocument snapshot causes crash [cloud_firestore] DocumentReference in startAfterDocument snapshot causes crash (invalid encode argument) Feb 24, 2020
ditman added a commit to ditman/firebase-flutterfire that referenced this issue Feb 28, 2020
```
I/flutter ( 4164): 00:07 +9: Firestore pagination with DocumentReference (firebase#2044) [E]
I/flutter ( 4164):   Invalid argument: Instance of 'DocumentReference'
I/flutter ( 4164):   package:flutter/src/services/message_codecs.dart 392:7                                                                                 StandardMessageCodec.writeValue
I/flutter ( 4164):   package:cloud_firestore_platform_interface/src/method_channel/utils/firestore_message_codec.dart 83:13                                 FirestoreMessageCodec.writeValue
I/flutter ( 4164):   package:flutter/src/services/message_codecs.dart 389:9                                                                                 StandardMessageCodec.writeValue.<fn>
I/flutter ( 4164):   dart:collection-patch/compact_hash.dart 379:8                                                                                          _LinkedHashMapMixin.forEach
I/flutter ( 4164):   package:flutter/src/services/message_codecs.dart 387:13                                                                                StandardMessageCodec.writeValue
I/flutter ( 4164):   package:cloud_firestore_platform_interface/src/method_channel/utils/firestore_message_codec.dart 83:13                                 FirestoreMessageCodec.writeValue
```
@ditman ditman self-assigned this Feb 28, 2020
@ditman
Copy link
Contributor

ditman commented Feb 28, 2020

The issue seems to originate in _PlatformUtils::toPlatformDocumentSnapshot.

It's using the data getter from DocumentSnapshot, which internally converts Platform to Plugin types. This is for convenience for end-users (who only care about Plugin types), but it's a problem when we try to funnel that back into the plugin for further processing.

The fix is to either pass documentSnapshot._delegate.data (skips the getter), or to _CodecUtility.replaceValueWithDelegatesInMap(documentSnapshot.data).

I'm leaning towards the latter, which may waste a few cycles (and on big documents it might be a bunch) converting back and forth the data of the document snapshot, but it doesn't access private fields (which makes it harder to refactor the library as import/export based later).

@ditman
Copy link
Contributor

ditman commented Feb 28, 2020

The "fix" seems to break tests in web, so maybe it's not a good fix though:

Nope. The Firestore pagination integ test is broken in web in master already (phew):

00:05 +7: Firestore pagination [E]
Invalid argument (dartObject): Could not convert: Instance of 'Timestamp'
package:dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 196:49      throw_
  package:firebase/src/utils.dart 119:3                                                     jsify
  package:dart-sdk/lib/internal/iterable.dart 417:29                                        elementAt
  package:dart-sdk/lib/internal/iterable.dart 221:19                                        toList
  package:firebase/src/firestore.dart 552:22                                                [_wrapPaginatingFunctionCall]
  package:firebase/src/firestore.dart 523:11                                                startAt
  package:cloud_firestore_web/src/query_web.dart 180:17                                     startAtDocument
  package:cloud_firestore/src/query.dart 130:17                                             startAtDocument
  package:firestore_example/main.dart 192:48                                                <fn>

@ditman
Copy link
Contributor

ditman commented Feb 28, 2020

My new test requires an index to be created. I'm waiting to be added to the Integ test project so I can create said index and continue on this.

@creativecreatorormaybenot
Copy link
Contributor Author

creativecreatorormaybenot commented Feb 28, 2020

@ditman How come that this was not caught in CI?
I realize that this is a Timestamp object and thus not completely related.

I could write tests to catch the error described in this issue if that helps.

@rbluethl
Copy link

rbluethl commented Mar 2, 2020

I'm facing the exact same issue. Downgrading to cloud_firestore: ^0.12.11 works for now.

@ditman
Copy link
Contributor

ditman commented Mar 2, 2020

I'm going to push a fix for most of the issues I've found while investigating this (and fixing tests) today. I'll investigate the Timestamp issue next.

ditman added a commit that referenced this issue Mar 6, 2020
…tSnapshotPlatform and FieldValueFactoryWeb (#2105)

* [cloud_firestore_web] Ensure FieldValueFactoryWeb encodes params.
* [cloud_firestore] Ensure `DocumentSnapshotPlatform` is well encoded.

Also tweak some integ tests so everything passes now, in Android.

Fixes #2044
@ditman
Copy link
Contributor

ditman commented Mar 6, 2020

Tagged and published:

DocumentReferences with DocumentReferences in startAfterDocument should work as expected now! Please clear your pub caches and grab the latest versions, and let us know how it goes!

@creativecreatorormaybenot
Copy link
Contributor Author

creativecreatorormaybenot commented Mar 7, 2020

@ditman Works 🙂 Thank you so much 👍

@ditman
Copy link
Contributor

ditman commented Mar 9, 2020

Sweet, thanks for reporting @creativecreatorormaybenot!

@kushvatsa
Copy link
Contributor

not working, timestamp issue - error-Invalid argument (dartObject): Could not convert: Instance of 'Timestamp'

@ditman
Copy link
Contributor

ditman commented Mar 9, 2020

@kushvatsa can you cut a separate issue for that? Thanks!

@kushvatsa
Copy link
Contributor

@ditman , #2153

@firebase firebase locked and limited conversation to collaborators Aug 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: bug Something isn't working
Projects
None yet
4 participants