Repository navigation
Conversation
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>
|
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. |
There was a problem hiding this comment.
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.
| 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) | ||
| } |
There was a problem hiding this comment.
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.
| 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) | |
| } |
Description
httpsCallable().stream()on Apple encodes numeric0and1as JSONfalseandtrue.AnyEncodable.encodematchedas Boolbefore any numeric cast. Flutter delivers both booleans and numbers asNSNumber, and Swift'sas Boolsucceeds forNSNumber0and1. A streamed payload such as{"timezoneOffset": 1.0}therefore arrives as{"timezoneOffset": true}. Other numbers, including2,-1, and5.5, were already preserved. Non-streamingcall()does not use this encoder.The encoder now treats a value as a boolean only when its Core Foundation type is
CFBoolean. OtherNSNumbervalues encode as JSON numbers. The macOSCodecUtility.swiftis 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_corepath dependency, so the regression test compilesCodecUtility.swiftdirectly:cd packages/cloud_functions/cloud_functions/ios/cloud_functions swiftc -o codec-check \ Sources/cloud_functions/CodecUtility.swift \ Tests/CodecUtilityEncodingTests.swift ./codec-checkThat test passed locally. It covers
NSNumber0,1,0.0,1.0,-1.0, and5.5, Swift0and1.0, realtrue/false, and a dictionary containing bothtimezoneOffset: 1.0and 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.yamland changelogs is not required.///).melos run analyze) does not report any problems on my PR.The new encoder test passed.
melos run analyzewas 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?
Made with Cursor