Repository navigation
Conversation
…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.
There was a problem hiding this comment.
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"]), |
There was a problem hiding this comment.
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().
| data=_decode(_json.loads(request.data)["data"]), | |
| data=_decode(request.get_json()["data"]), |
IzaakGough
left a comment
There was a problem hiding this comment.
@0z5a Thank you for the contribution! I've left some comments below
| if isinstance(data, dict): | ||
| if data.get("@type") in (_LONG_TYPE, _UNSIGNED_LONG_TYPE): | ||
| try: | ||
| return int(data["value"]) |
There was a problem hiding this comment.
int() also accepts a non-string value, so some malformed wrappers decode to a wrong number instead of returning 400:
value |
Result |
|---|---|
42.9 |
200, handler gets 42 |
true |
200, handler gets 1 |
"abc" |
400 |
Node's decode returns 42.9 for the first and rejects true. Checking isinstance(data["value"], str) before int() would reject both. It would be good to add these two cases to the rejection test too, since it currently only covers "abc".
| context: CallableRequest = CallableRequest( | ||
| raw_request=request, | ||
| data=_json.loads(request.data)["data"], | ||
| data=_decode(_json.loads(request.data)["data"]), |
There was a problem hiding this comment.
Because _decode runs here, before on_call_check_tokens, a request with an invalid ID token and a malformed wrapper gets 400 INVALID_ARGUMENT instead of 401 UNAUTHENTICATED:
Authorization: Bearer garbage, data {"n": {"@type": "...Int64Value", "value": "abc"}} -> 400
Authorization: Bearer garbage, data {"n": 1} -> 401
Node decodes after the token checks, so unauthenticated callers always get 401 there. Could the decode move to just before the handler is called?
| Maps with any other ``@type`` are left as maps. | ||
| """ | ||
| if isinstance(data, list): | ||
| return [_decode(item) for item in data] |
There was a problem hiding this comment.
_decode recurses once per nesting level, so deeply nested data that json.loads parses without error now hits RecursionError, and the handler turns that into a 500. On Python 3.14, nested lists 1000 deep return 200 on main and 500 INTERNAL with this change. Real payloads are unlikely to nest that deep, but it changes an existing success into a 500. An iterative walk, or catching RecursionError and returning 400, would avoid it.
| _UNSIGNED_LONG_TYPE = "type.googleapis.com/google.protobuf.UInt64Value" | ||
|
|
||
|
|
||
| def _decode(data: _typing.Any) -> _typing.Any: |
There was a problem hiding this comment.
tasks_fn._on_call_handler still passes the raw data, so a task enqueued with an Int64Value wrapper reaches the handler as {'@type': '...Int64Value', 'value': '42'}. Node calls decode in both onCall and onTaskDispatched. Moving _decode into private/util.py and calling it from both handlers would keep them consistent.
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/JavaLongthis way, and the iOS SDK sends everyNSNumberwhose type islong long, which includes every SwiftInt. The Node.js SDK runsdecode()onreq.body.dataand the handler receives a plain number.on_callin this SDK passes the request'sdatathrough 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 doingreq.data["count"] + 1raisesTypeError, and the client gets a 500INTERNALerror. The same handler returns 43 when the client sends a plain42.This change adds a small recursive
_decode()that_on_call_handlerapplies todata:Int64ValueandUInt64Valuewrappers become Pythonint. Usingintinstead offloat, which is what Node gets fromparseFloat, keeps values above 2^53 exact, matching how the Android and iOS SDKs decode these wrappers.@typestay as maps, as the callable protocol reference specifies ("If a map with an unknown@typefield is deserialized, it is left as a map"). Node throws in this case. Not throwing means data that works today keeps working.valueis not an integer string returns 400INVALID_ARGUMENTinstead of an unhandled 500.on_task_dispatchedis 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 afterTesting: added
test_on_call_decodes_wrapped_longstotests/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 --checkandmypyare clean.This change was drafted with Claude Code (claude-opus-5-5); the test results above are from the runs described.