Skip to content

fix(https_fn): decode Int64Value/UInt64Value wrappers in callable request data - #338

Open
0z5a wants to merge 1 commit into
firebase:mainfrom
0z5a:fix/on-call-decode-longs
Open

0z5a wants to merge 1 commit into
firebase:mainfrom
0z5a:fix/on-call-decode-longs

Conversation

@0z5a

@0z5a 0z5a commented Nov 16, 2026

Copy link
Copy Markdown

The callable protocol encodes 64-bit integers as typed proto3 wrappers, for example {"@type": "type.googleapis.com/google.protobuf.Int64Value", "value": "42"}. The Android SDK sends every Kotlin/Java Long this way, and the iOS SDK sends every NSNumber whose type is long long, which includes every Swift Int. The Node.js SDK runs decode() on req.body.data and the handler receives a plain number. on_call in this SDK passes the request's data through unchanged, so the handler receives the wrapper dict instead.

Example: an iOS app calls functions.httpsCallable("add").call(["count": 42]). The request body is {"data": {"count": {"@type": "type.googleapis.com/google.protobuf.Int64Value", "value": "42"}}}. A handler doing req.data["count"] + 1 raises TypeError, and the client gets a 500 INTERNAL error. The same handler returns 43 when the client sends a plain 42.

This change adds a small recursive _decode() that _on_call_handler applies to data:

  • Int64Value and UInt64Value wrappers become Python int. Using int instead of float, which is what Node gets from parseFloat, keeps values above 2^53 exact, matching how the Android and iOS SDKs decode these wrappers.
  • Maps with any other @type stay as maps, as the callable protocol reference specifies ("If a map with an unknown @type field is deserialized, it is left as a map"). Node throws in this case. Not throwing means data that works today keeps working.
  • A wrapper whose value is not an integer string returns 400 INVALID_ARGUMENT instead of an unhandled 500.

on_task_dispatched is unchanged. The Node SDK decodes task payloads too, but I have no case where a task enqueuer sends these wrappers.

Before and after, using a Flask test request against on_call:

  • {"count": 42}: 200 / 43 before, 200 / 43 after
  • {"count": <Int64Value "42">}: 500 INTERNAL before, 200 / 43 after
  • {"count": <Int64Value "9007199254740993">}: 500 INTERNAL before, 200 / 9007199254740994 after

Testing: added test_on_call_decodes_wrapped_longs to tests/test_https_fn.py. Without the fix it fails (4 failures, including 500 != 200). With the fix it passes, and the full suite passes (175 passed, macOS arm64, Python 3.11). ruff check, ruff format --check and mypy are clean.

This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.

…uest data

The Android and iOS client SDKs send 64-bit integers in callable request
data as typed proto3 wrappers. on_call passed them through as dicts, so
a handler doing arithmetic on such a value failed with a 500. Decode the
wrappers to int, as the Node.js SDK decodes them, and leave maps with any
other @type unchanged.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces decoding logic for wrapped 64-bit integers (Int64Value and UInt64Value) sent by client SDKs in callable HTTPS functions, converting them into native Python integers. It also includes comprehensive unit tests for this behavior. The review feedback suggests optimizing performance by using Flask's cached request.get_json() instead of redundantly parsing the raw request data with _json.loads(request.data).

context: CallableRequest = CallableRequest(
raw_request=request,
data=_json.loads(request.data)["data"],
data=_decode(_json.loads(request.data)["data"]),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

Flask parses and caches the JSON body of the request when request.get_json() is called (which already happens during validation in _util.valid_on_call_request). Using _json.loads(request.data) parses the raw request body string a second time, which is redundant and inefficient. We can directly use the cached parsed JSON via request.get_json().

Suggested change
data=_decode(_json.loads(request.data)["data"]),
data=_decode(request.get_json()["data"]),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant