firebase / firebase-tools

The Firebase Command Line Tools
MIT License
3.97k stars 915 forks source link

Emulator in datastore mode doesn't return "endCursor" while Datastore emulator returns for the same query #7352

Open onurdialpad opened 1 week ago

onurdialpad commented 1 week ago

[REQUIRED] Environment info

firebase-tools: 7.16.1

Platform: macOS

[REQUIRED] Test case

import requests

emulator_url = 'http://127.0.0.1:50579/v1/projects/test-app:runQuery'

payload = b'{"partitionId": {"projectId": "test-app", "namespaceId": ""}, "query": {"kind": [{"name": "Profile"}], "limit": 1}, "readOptions": {"readConsistency": "EVENTUAL"}}'

headers = {'Content-Length': '169', 'Content-Type': 'application/json'}

# POST request to the emulator
resp = requests.post(emulator_url, headers=headers, data=payload)

# check response's data
data = resp.json()
print(data)
# prints {'batch': {'entityResultType': 'FULL', 'moreResults': 'MORE_RESULTS_AFTER_LIMIT', 'readTime': '2024-06-21T20:20:20.928406Z'}} no "endCursor" within the data.

[REQUIRED] Steps to reproduce

Explained above

[REQUIRED] Expected behavior

You will see something like {'entityResultType': 'FULL', 'moreResults': 'MORE_RESULTS_AFTER_LIMIT', 'readTime': '2024-06-20T21:40:28.228019Z'}

[REQUIRED] Actual behavior

Datastore emulator returns {'endCursor': 'CgA=', 'entityResultType': 'FULL', 'moreResults': 'MORE_RESULTS_AFTER_LIMIT'} which is close to the production env

aalej commented 1 week ago

Hey @onurdialpad, thanks for the report. Could you try updating to the latest version of firebase-tools, which is currently v13.11.3, to see if you would still encounter the issue?

Also, I’m having some issues with running the code snippet you provided. Could you let me know if I may be missing anything here, what I have so far is:

from google.appengine.ext import db
import os

os.environ["APPLICATION_ID"] = "demo-testproj"
os.environ["DATASTORE_EMULATOR_HOST"] = "127.0.0.1:8081"

class Person(db.Model):
   first_name = db.StringProperty(required=True)

result = Person.all().get()

print(result)
onurdialpad commented 1 week ago

Hey @aalej thanks for your quick response, I upgraded firebase-tools but it didn't help.

Regarding with the reproduce steps, sorry for the confusion, I updated it please retry and let me know if you still have issues to reproduce.

aalej commented 4 days ago

Thanks for updating the test case @onurdialpad. It is highly appreciated! I was able to reproduce the behavior you mentioned and I created this mcve using the information provided.

Just to make sure we’re on the same page, could you verify if the expected behavior and actual behavior you shared is correct?

When running gcloud --project=test-app beta emulators datastore start --use-firestore-in-datastore-mode --host-port=127.0.0.1:5057, running the code sample provided prints:

{'batch': {'entityResultType': 'FULL', 'endCursor': 'CgA=', 'moreResults': 'MORE_RESULTS_AFTER_LIMIT'}}

Which has the endCursor, which is I think the behavior you expected.

However, when running gcloud emulators firestore start --host-port=127.0.0.1:50579 --database-mode=datastore-mode, and running the code sample provided prints:

{'batch': {'entityResultType': 'FULL', 'moreResults': 'MORE_RESULTS_AFTER_LIMIT', 'readTime': '2024-06-25T10:30:18.578242Z'}}

Which doesn’t have the endCursor, which is I think the actual(unexpected) behavior.

harshyyy21 commented 4 days ago

Hi @onurdialpad, from some quick testing, it looks like this is an issue when the query doesn't return any results. Can you verify whether or not the endCursor is populated in the case where the query does return results?

onurdialpad commented 4 days ago

Hi @harshyyy21 yes that's correct, when there is no result, datastore emulator returns endCursor while firestore emulator doesn't return it and I think according to the reference page I mentioned, it is supposed to return endCursor. If there is result, then endCursor exists.

Hey @aalej yes that's exactly what I meant, thanks!