Skip to content

fix(cloud_functions): keep Apple stream numbers 0 and 1 as JSON numbers - #18983

Open
ik-make wants to merge 4 commits into
firebase:mainfrom
ik-make:fix/apple-stream-numeric-encoding
Open

ik-make wants to merge 4 commits into
firebase:mainfrom
ik-make:fix/apple-stream-numeric-encoding

Conversation

@ik-make

@ik-make ik-make commented Nov 18, 2026 •

Copy link
Copy Markdown

Description

httpsCallable().stream() on Apple encodes numeric 0 and 1 as JSON false and true.

AnyEncodable.encode matched as Bool before any numeric cast. Flutter delivers both booleans and numbers as NSNumber, and Swift's as Bool succeeds for NSNumber 0 and 1. A streamed payload such as {"timezoneOffset": 1.0} therefore arrives as {"timezoneOffset": true}. Other numbers, including 2, -1, and 5.5, were already preserved. Non-streaming call() does not use this encoder.

The encoder now treats a value as a boolean only when its Core Foundation type is CFBoolean. Other NSNumber values encode as JSON numbers:

  • objCType Q (unsigned values above Int64.max) encodes with uint64Value, so it does not wrap.
  • objCType f (Float) encodes with floatValue, so Float(0.1) stays 0.1.
  • Other floating-point values encode as Double, and other integers as Int64.

This keeps the UInt64 and Float behavior of the previous switch. The macOS CodecUtility.swift is a symlink to the iOS source, so both platforms get the change.

Related Issues

Fixes #18982.

Test plan

E2E (example/integration_test/e2e_test.dart, HttpsCallable Stream group), using the existing testFunctionDefaultRegion emulator function:

  • accepts number values 0 and 1 streams 0, 1, 0.0, and 1.0 and expects the function to see a number.
  • keeps number values 0 and 1 in a [Map] and [List] streams a rawData map with 0, 1, 0.0, 1.0, true, false, and a mixed list, and expects it to round-trip unchanged.

Run locally on an iPhone 16 simulator (iOS 18.5) with the CI steps (generate-dummy-firebase-configs.dart --functions-native, firebase emulators:exec):

  • With the previous encoder, both new tests fail: 0 reaches the function as boolean, and zero comes back as false.
  • With this change, the full cloud_functions e2e suite passes (34 passed, 4 web-only skipped).

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]).
This will ensure a smooth and quick review process. Updating the pubspec.yaml and changelogs is not required.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • 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 (melos run analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Tests were run locally on iOS; Android, macOS, and web e2e runs are pending CI. flutter analyze and dart format are clean for the e2e file, and the analyze CI job passed on the first commit.

Breaking Change

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

  • Yes, this is a breaking change.
  • No, this is not a breaking change.

AnyEncodable matched NSNumber 0 and 1 as Bool before Double, so
httpsCallable().stream() sent those values as false and true.
@google-cla

google-cla Bot commented Nov 18, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@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 refactors the encoding logic in CodecUtility.swift by introducing helper methods to simplify the serialization of NSNumber and other method-channel values, and adds a comprehensive test suite in CodecUtilityEncodingTests.swift. Feedback highlights a potential regression where large UInt64 values exceeding Int64.max could suffer from silent integer overflow when encoded as int64Value. A code suggestion is provided to inspect the Objective-C type encoding (objCType) to safely handle and encode these large unsigned integers.

…coding

NSNumber values above Int64.max report objCType Q and wrapped through int64Value. Float values gained digits through doubleValue. Both now encode with their own width, as the previous switch did.

@SelaseKay SelaseKay 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.

Hi @ik-make, thanks for the PR. Can we add e2e tests for this?

@ik-make

ik-make commented Nov 18, 2026

Copy link
Copy Markdown
Author

@SelaseKay Thanks, added in c1fdf45. The HttpsCallable Stream group in example/integration_test/e2e_test.dart now has two tests:

  • accepts number values 0 and 1 streams 0, 1, 0.0, and 1.0 and expects testFunctionDefaultRegion to see a number.
  • keeps number values 0 and 1 in a [Map] and [List] streams a rawData map with 0, 1, 1.0, true, false, and a mixed list, and expects it back unchanged.

They use the existing emulator function, so no function changes are needed. On an iOS 18.5 simulator both tests fail with the previous encoder (0 arrives as boolean, and zero comes back as false). With this change the full cloud_functions e2e suite passes.

@ik-make

ik-make commented Nov 18, 2026

Copy link
Copy Markdown
Author

@googlebot I signed it!

@SelaseKay the e2e tests are in c1fdf45, and this is ready for another look when you have a moment. The CI runs for the latest commit are waiting for workflow approval.

@ik-make
ik-make force-pushed the fix/apple-stream-numeric-encoding branch from c1fdf45 to 836b424 Compare November 18, 2026 20:46
@ik-make

ik-make commented Nov 18, 2026

Copy link
Copy Markdown
Author

I force-pushed to drop an automatic tool co-author trailer that the CLA check could not match. Code is unchanged; each new commit has the same tree as the old one:

@ik-make
ik-make requested a review from SelaseKay November 18, 2026 20:57
@ik-make
ik-make requested a review from SelaseKay November 19, 2026 14:46

This branch has not been deployed

No deployments
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.

[cloud_functions] Apple httpsCallable stream encodes numeric 0 and 1 as booleans

2 participants