Skip to content

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

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

ik-make wants to merge 1 commit 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. The macOS CodecUtility.swift is a symlink to the iOS source, so both platforms get the change.

Fixes #18982.

Related Issues

Fixes #18982.

Test plan

The plugin package cannot host an SPM test target without the generated firebase_core path dependency, so the regression test compiles CodecUtility.swift directly:

cd packages/cloud_functions/cloud_functions/ios/cloud_functions
swiftc -o codec-check \
  Sources/cloud_functions/CodecUtility.swift \
  Tests/CodecUtilityEncodingTests.swift
./codec-check

That test passed locally. It covers NSNumber 0, 1, 0.0, 1.0, -1.0, and 5.5, Swift 0 and 1.0, real true/false, and a dictionary containing both timezoneOffset: 1.0 and a boolean.

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.

The new encoder test passed. melos run analyze was not run; this change is Swift-only. The Google CLA still needs to be signed by the commit author if the bot requests it.

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.

Made with Cursor

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

Co-authored-by: Cursor <cursoragent@cursor.com>
@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.

Comment on lines +56 to +69
private func encode(
_ number: NSNumber,
into container: inout SingleValueEncodingContainer
) throws {
if CFGetTypeID(number) == CFBooleanGetTypeID() {
try container.encode(number.boolValue)
return
}
if CFNumberIsFloatType(number) {
try container.encode(number.doubleValue)
return
}
try container.encode(number.int64Value)
}

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

The previous implementation of AnyEncodable explicitly supported UInt64 values via case let uint64Value as UInt64:. In the new implementation, all non-boolean and non-floating-point numeric types are cast to NSNumber and encoded using number.int64Value.

However, if a UInt64 value greater than Int64.max (9223372036854775807) is passed, casting it to NSNumber and calling int64Value will cause a silent integer overflow/wrap-around (e.g., UInt64.max becomes -1).

To prevent this regression and preserve correct encoding for large unsigned 64-bit integers, we can inspect the Objective-C type encoding (objCType) of the NSNumber. If it is "Q" (unsigned long long) or "L" (unsigned long), we can safely encode it using uint64Value.

Suggested change
private func encode(
_ number: NSNumber,
into container: inout SingleValueEncodingContainer
) throws {
if CFGetTypeID(number) == CFBooleanGetTypeID() {
try container.encode(number.boolValue)
return
}
if CFNumberIsFloatType(number) {
try container.encode(number.doubleValue)
return
}
try container.encode(number.int64Value)
}
private func encode(
_ number: NSNumber,
into container: inout SingleValueEncodingContainer
) throws {
if CFGetTypeID(number) == CFBooleanGetTypeID() {
try container.encode(number.boolValue)
return
}
let objcType = String(cString: number.objCType)
if objcType == "Q" || objcType == "L" {
try container.encode(number.uint64Value)
return
}
if CFNumberIsFloatType(number) {
try container.encode(number.doubleValue)
return
}
try container.encode(number.int64Value)
}

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

1 participant