From ef469b424e977c29461e54b2de9d58de94330354 Mon Sep 17 00:00:00 2001 From: "Igor K." Date: Wed, 18 Nov 2026 15:20:40 +0100 Subject: [PATCH 1/4] fix(cloud_functions): keep Apple stream numbers 0 and 1 as JSON numbers AnyEncodable matched NSNumber 0 and 1 as Bool before Double, so httpsCallable().stream() sent those values as false and true. --- .../cloud_functions/CodecUtility.swift | 65 +++++----- .../Tests/CodecUtilityEncodingTests.swift | 112 ++++++++++++++++++ 2 files changed, 149 insertions(+), 28 deletions(-) create mode 100644 packages/cloud_functions/cloud_functions/ios/cloud_functions/Tests/CodecUtilityEncodingTests.swift diff --git a/packages/cloud_functions/cloud_functions/ios/cloud_functions/Sources/cloud_functions/CodecUtility.swift b/packages/cloud_functions/cloud_functions/ios/cloud_functions/Sources/cloud_functions/CodecUtility.swift index 1b8b7faed39a..2d2e37d600e9 100644 --- a/packages/cloud_functions/cloud_functions/ios/cloud_functions/Sources/cloud_functions/CodecUtility.swift +++ b/packages/cloud_functions/cloud_functions/ios/cloud_functions/Sources/cloud_functions/CodecUtility.swift @@ -12,38 +12,32 @@ public struct AnyEncodable: Encodable { public func encode(to encoder: Encoder) throws { var container = encoder.singleValueContainer() + try encodeAny(value, into: &container, encoder: encoder) + } - switch value { - case is NSNull: + /// Encodes a method-channel value. + /// + /// Flutter delivers numbers and booleans as `NSNumber`. Swift's `as Bool` + /// succeeds for numeric `0` and `1`, so a boolean is recognized only when + /// the value is a `CFBoolean`. Numeric `0` and `1` stay JSON numbers. + private func encodeAny( + _ value: Any?, + into container: inout SingleValueEncodingContainer, + encoder: Encoder + ) throws { + if value == nil || value is NSNull { try container.encodeNil() + return + } + + if let number = value as? NSNumber { + try encode(number, into: &container) + return + } + + switch value { case let stringValue as String: try container.encode(stringValue) - case let boolValue as Bool: - try container.encode(boolValue) - case let intValue as Int: - try container.encode(intValue) - case let int8Value as Int8: - try container.encode(int8Value) - case let int16Value as Int16: - try container.encode(int16Value) - case let int32Value as Int32: - try container.encode(int32Value) - case let int64Value as Int64: - try container.encode(int64Value) - case let uintValue as UInt: - try container.encode(uintValue) - case let uint8Value as UInt8: - try container.encode(uint8Value) - case let uint16Value as UInt16: - try container.encode(uint16Value) - case let uint32Value as UInt32: - try container.encode(uint32Value) - case let uint64Value as UInt64: - try container.encode(uint64Value) - case let doubleValue as Double: - try container.encode(doubleValue) - case let floatValue as Float: - try container.encode(floatValue) case let arrayValue as [Any]: try container.encode(arrayValue.map { AnyEncodable($0) }) case let dictionaryValue as [String: Any]: @@ -58,6 +52,21 @@ public struct AnyEncodable: Encodable { ) } } + + 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) + } } public struct AnyDecodable: Decodable { diff --git a/packages/cloud_functions/cloud_functions/ios/cloud_functions/Tests/CodecUtilityEncodingTests.swift b/packages/cloud_functions/cloud_functions/ios/cloud_functions/Tests/CodecUtilityEncodingTests.swift new file mode 100644 index 000000000000..81ddeca7d7b7 --- /dev/null +++ b/packages/cloud_functions/cloud_functions/ios/cloud_functions/Tests/CodecUtilityEncodingTests.swift @@ -0,0 +1,112 @@ +// Copyright 2026 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// AnyEncodable is compiled with this file. The macOS CodecUtility.swift is a +// symlink to the iOS source, so this covers both platforms. +// +// swiftc -o codec-check \ +// Sources/cloud_functions/CodecUtility.swift \ +// Tests/CodecUtilityEncodingTests.swift \ +// && ./codec-check + +import Foundation + +func encodedJSON(_ value: Any) throws -> String { + let data = try JSONEncoder().encode(AnyEncodable(value)) + guard let json = String(data: data, encoding: .utf8) else { + throw TestFailure("encoded data is not UTF-8") + } + return json +} + +struct TestFailure: Error, CustomStringConvertible { + let description: String + init(_ description: String) { self.description = description } +} + +func expect(_ condition: Bool, _ message: String) throws { + if !condition { + throw TestFailure(message) + } +} + +func expectJSON(_ value: Any, equals expected: String, _ label: String) throws { + let actual = try encodedJSON(value) + try expect(actual == expected, "\(label): expected \(expected), got \(actual)") +} + +func expectNumber(_ value: Any, equals expected: Double, _ label: String) throws { + let json = try encodedJSON(value) + try expect(json != "true" && json != "false", "\(label) encoded as a boolean: \(json)") + let data = Data(json.utf8) + let parsed = try JSONSerialization.jsonObject(with: data, options: [.fragmentsAllowed]) + guard let number = parsed as? NSNumber else { + throw TestFailure("\(label) did not decode as a number: \(json)") + } + try expect( + CFGetTypeID(number) != CFBooleanGetTypeID(), + "\(label) decoded as CFBoolean: \(json)" + ) + try expect(number.doubleValue == expected, "\(label): expected \(expected), got \(number)") +} + +func runTests() throws { + let cases: [(String, Any, Double)] = [ + ("double 0", NSNumber(value: 0.0), 0), + ("double 1", NSNumber(value: 1.0), 1), + ("double -1", NSNumber(value: -1.0), -1), + ("double 5.5", NSNumber(value: 5.5), 5.5), + ("int 0", NSNumber(value: Int(0)), 0), + ("int 1", NSNumber(value: Int(1)), 1), + ("swift double 1", 1.0, 1), + ("swift int 0", 0, 0), + ] + + for (label, value, expected) in cases { + try expectNumber(value, equals: expected, label) + } + + try expectJSON(NSNumber(value: true), equals: "true", "NSNumber true") + try expectJSON(NSNumber(value: false), equals: "false", "NSNumber false") + try expectJSON(true, equals: "true", "Swift true") + try expectJSON(false, equals: "false", "Swift false") + + let payload: [String: Any] = [ + "timezoneOffset": NSNumber(value: 1.0), + "enabled": NSNumber(value: true), + "message": "hello", + ] + let payloadData = try JSONEncoder().encode(AnyEncodable(payload)) + let payloadObject = try JSONSerialization.jsonObject(with: payloadData) as? [String: Any] + guard let payloadObject else { + throw TestFailure("payload did not encode as an object") + } + guard let offset = payloadObject["timezoneOffset"] as? NSNumber else { + throw TestFailure("timezoneOffset missing") + } + try expect(CFGetTypeID(offset) != CFBooleanGetTypeID(), "timezoneOffset became a boolean") + try expect(offset.doubleValue == 1, "timezoneOffset was \(offset)") + guard let enabled = payloadObject["enabled"] as? NSNumber else { + throw TestFailure("enabled missing") + } + try expect( + CFGetTypeID(enabled) == CFBooleanGetTypeID() && enabled.boolValue, + "enabled was not true" + ) + try expect(payloadObject["message"] as? String == "hello", "message changed") + + print("CodecUtility encoding tests passed") +} + +@main +struct CodecUtilityEncodingTests { + static func main() { + do { + try runTests() + } catch { + fputs("\(error)\n", stderr) + exit(EXIT_FAILURE) + } + } +} From 318f27d918e4c34acd9145201e50b09ea8fd3a42 Mon Sep 17 00:00:00 2001 From: "Igor K." Date: Wed, 18 Nov 2026 16:08:19 +0100 Subject: [PATCH 2/4] fix(cloud_functions): keep UInt64 and Float widths in Apple stream encoding 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. --- .../Sources/cloud_functions/CodecUtility.swift | 17 +++++++++++++---- .../Tests/CodecUtilityEncodingTests.swift | 7 +++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/cloud_functions/cloud_functions/ios/cloud_functions/Sources/cloud_functions/CodecUtility.swift b/packages/cloud_functions/cloud_functions/ios/cloud_functions/Sources/cloud_functions/CodecUtility.swift index 2d2e37d600e9..06f6f597ab2f 100644 --- a/packages/cloud_functions/cloud_functions/ios/cloud_functions/Sources/cloud_functions/CodecUtility.swift +++ b/packages/cloud_functions/cloud_functions/ios/cloud_functions/Sources/cloud_functions/CodecUtility.swift @@ -53,6 +53,8 @@ public struct AnyEncodable: Encodable { } } + /// `NSNumber` reports `Q` for unsigned values above `Int64.max` and `f` for + /// `Float`. Those keep their own width so they do not wrap or gain digits. private func encode( _ number: NSNumber, into container: inout SingleValueEncodingContainer @@ -61,11 +63,18 @@ public struct AnyEncodable: Encodable { try container.encode(number.boolValue) return } - if CFNumberIsFloatType(number) { - try container.encode(number.doubleValue) - return + switch String(cString: number.objCType) { + case "Q": + try container.encode(number.uint64Value) + case "f": + try container.encode(number.floatValue) + default: + if CFNumberIsFloatType(number) { + try container.encode(number.doubleValue) + } else { + try container.encode(number.int64Value) + } } - try container.encode(number.int64Value) } } diff --git a/packages/cloud_functions/cloud_functions/ios/cloud_functions/Tests/CodecUtilityEncodingTests.swift b/packages/cloud_functions/cloud_functions/ios/cloud_functions/Tests/CodecUtilityEncodingTests.swift index 81ddeca7d7b7..8ba00dc68ec6 100644 --- a/packages/cloud_functions/cloud_functions/ios/cloud_functions/Tests/CodecUtilityEncodingTests.swift +++ b/packages/cloud_functions/cloud_functions/ios/cloud_functions/Tests/CodecUtilityEncodingTests.swift @@ -72,6 +72,13 @@ func runTests() throws { try expectJSON(true, equals: "true", "Swift true") try expectJSON(false, equals: "false", "Swift false") + try expectJSON(UInt64.max, equals: "18446744073709551615", "UInt64.max") + try expectJSON(UInt64(Int64.max) + 1, equals: "9223372036854775808", "Int64.max + 1") + try expectJSON(Int64.max, equals: "9223372036854775807", "Int64.max") + try expectJSON(Int64.min, equals: "-9223372036854775808", "Int64.min") + try expectJSON(Float(0.1), equals: "0.1", "Float 0.1") + try expectNumber(NSNumber(value: Float(1)), equals: 1, "Float 1") + let payload: [String: Any] = [ "timezoneOffset": NSNumber(value: 1.0), "enabled": NSNumber(value: true), From 836b4249587772222cfe86ebd907d701e57530ed Mon Sep 17 00:00:00 2001 From: "Igor K." Date: Wed, 18 Nov 2026 17:08:34 +0100 Subject: [PATCH 3/4] test(cloud_functions): add stream e2e tests for numeric 0 and 1 --- .../example/integration_test/e2e_test.dart | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/cloud_functions/cloud_functions/example/integration_test/e2e_test.dart b/packages/cloud_functions/cloud_functions/example/integration_test/e2e_test.dart index 457017b7ae6d..ca2f665863a6 100644 --- a/packages/cloud_functions/cloud_functions/example/integration_test/e2e_test.dart +++ b/packages/cloud_functions/cloud_functions/example/integration_test/e2e_test.dart @@ -378,6 +378,39 @@ void main() { ); }); + // Regression test for https://github.com/firebase/flutterfire/issues/18982 + test('accepts number values 0 and 1', () async { + for (final value in [0, 1, 0.0, 1.0]) { + final partialData = await callable + .stream(value) + .where((event) => event is Chunk) + .map((event) => (event as Chunk).partialData) + .first + .timeout(_completerTimeout); + expect(partialData, equals('number'), reason: 'sent $value'); + } + }); + + test('keeps number values 0 and 1 in a [Map] and [List]', () async { + final input = { + 'type': 'rawData', + 'zero': 0, + 'one': 1, + 'zeroDouble': 0.0, + 'oneDouble': 1.0, + 'booleanTrue': true, + 'booleanFalse': false, + 'list': [0, 1, 1.0, true, false], + }; + final partialData = await callable + .stream(input) + .where((event) => event is Chunk) + .map((event) => (event as Chunk).partialData) + .first + .timeout(_completerTimeout); + expect(partialData, equals(input)); + }); + test('can be called using an String url', () async { final localhostMapped = kIsWeb || !Platform.isAndroid ? 'localhost' From c54e251bcd0ae0b2a424815a7ea9c7a3bf06a997 Mon Sep 17 00:00:00 2001 From: "Igor K." Date: Thu, 19 Nov 2026 15:40:32 +0100 Subject: [PATCH 4/4] test(cloud_functions): remove standalone Swift encoder check --- .../Tests/CodecUtilityEncodingTests.swift | 119 ------------------ 1 file changed, 119 deletions(-) delete mode 100644 packages/cloud_functions/cloud_functions/ios/cloud_functions/Tests/CodecUtilityEncodingTests.swift diff --git a/packages/cloud_functions/cloud_functions/ios/cloud_functions/Tests/CodecUtilityEncodingTests.swift b/packages/cloud_functions/cloud_functions/ios/cloud_functions/Tests/CodecUtilityEncodingTests.swift deleted file mode 100644 index 8ba00dc68ec6..000000000000 --- a/packages/cloud_functions/cloud_functions/ios/cloud_functions/Tests/CodecUtilityEncodingTests.swift +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright 2026 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// AnyEncodable is compiled with this file. The macOS CodecUtility.swift is a -// symlink to the iOS source, so this covers both platforms. -// -// swiftc -o codec-check \ -// Sources/cloud_functions/CodecUtility.swift \ -// Tests/CodecUtilityEncodingTests.swift \ -// && ./codec-check - -import Foundation - -func encodedJSON(_ value: Any) throws -> String { - let data = try JSONEncoder().encode(AnyEncodable(value)) - guard let json = String(data: data, encoding: .utf8) else { - throw TestFailure("encoded data is not UTF-8") - } - return json -} - -struct TestFailure: Error, CustomStringConvertible { - let description: String - init(_ description: String) { self.description = description } -} - -func expect(_ condition: Bool, _ message: String) throws { - if !condition { - throw TestFailure(message) - } -} - -func expectJSON(_ value: Any, equals expected: String, _ label: String) throws { - let actual = try encodedJSON(value) - try expect(actual == expected, "\(label): expected \(expected), got \(actual)") -} - -func expectNumber(_ value: Any, equals expected: Double, _ label: String) throws { - let json = try encodedJSON(value) - try expect(json != "true" && json != "false", "\(label) encoded as a boolean: \(json)") - let data = Data(json.utf8) - let parsed = try JSONSerialization.jsonObject(with: data, options: [.fragmentsAllowed]) - guard let number = parsed as? NSNumber else { - throw TestFailure("\(label) did not decode as a number: \(json)") - } - try expect( - CFGetTypeID(number) != CFBooleanGetTypeID(), - "\(label) decoded as CFBoolean: \(json)" - ) - try expect(number.doubleValue == expected, "\(label): expected \(expected), got \(number)") -} - -func runTests() throws { - let cases: [(String, Any, Double)] = [ - ("double 0", NSNumber(value: 0.0), 0), - ("double 1", NSNumber(value: 1.0), 1), - ("double -1", NSNumber(value: -1.0), -1), - ("double 5.5", NSNumber(value: 5.5), 5.5), - ("int 0", NSNumber(value: Int(0)), 0), - ("int 1", NSNumber(value: Int(1)), 1), - ("swift double 1", 1.0, 1), - ("swift int 0", 0, 0), - ] - - for (label, value, expected) in cases { - try expectNumber(value, equals: expected, label) - } - - try expectJSON(NSNumber(value: true), equals: "true", "NSNumber true") - try expectJSON(NSNumber(value: false), equals: "false", "NSNumber false") - try expectJSON(true, equals: "true", "Swift true") - try expectJSON(false, equals: "false", "Swift false") - - try expectJSON(UInt64.max, equals: "18446744073709551615", "UInt64.max") - try expectJSON(UInt64(Int64.max) + 1, equals: "9223372036854775808", "Int64.max + 1") - try expectJSON(Int64.max, equals: "9223372036854775807", "Int64.max") - try expectJSON(Int64.min, equals: "-9223372036854775808", "Int64.min") - try expectJSON(Float(0.1), equals: "0.1", "Float 0.1") - try expectNumber(NSNumber(value: Float(1)), equals: 1, "Float 1") - - let payload: [String: Any] = [ - "timezoneOffset": NSNumber(value: 1.0), - "enabled": NSNumber(value: true), - "message": "hello", - ] - let payloadData = try JSONEncoder().encode(AnyEncodable(payload)) - let payloadObject = try JSONSerialization.jsonObject(with: payloadData) as? [String: Any] - guard let payloadObject else { - throw TestFailure("payload did not encode as an object") - } - guard let offset = payloadObject["timezoneOffset"] as? NSNumber else { - throw TestFailure("timezoneOffset missing") - } - try expect(CFGetTypeID(offset) != CFBooleanGetTypeID(), "timezoneOffset became a boolean") - try expect(offset.doubleValue == 1, "timezoneOffset was \(offset)") - guard let enabled = payloadObject["enabled"] as? NSNumber else { - throw TestFailure("enabled missing") - } - try expect( - CFGetTypeID(enabled) == CFBooleanGetTypeID() && enabled.boolValue, - "enabled was not true" - ) - try expect(payloadObject["message"] as? String == "hello", "message changed") - - print("CodecUtility encoding tests passed") -} - -@main -struct CodecUtilityEncodingTests { - static func main() { - do { - try runTests() - } catch { - fputs("\(error)\n", stderr) - exit(EXIT_FAILURE) - } - } -}