Repository navigation
fix(cloud_functions): keep Apple stream numbers 0 and 1 as JSON numbers #18983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ik-make
wants to merge
1
commit into
firebase:main
Choose a base branch
from
ik-make:fix/apple-stream-numeric-encoding
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+149
−28
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
...cloud_functions/cloud_functions/ios/cloud_functions/Tests/CodecUtilityEncodingTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous implementation of
AnyEncodableexplicitly supportedUInt64values viacase let uint64Value as UInt64:. In the new implementation, all non-boolean and non-floating-point numeric types are cast toNSNumberand encoded usingnumber.int64Value.However, if a
UInt64value greater thanInt64.max(9223372036854775807) is passed, casting it toNSNumberand callingint64Valuewill cause a silent integer overflow/wrap-around (e.g.,UInt64.maxbecomes-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 theNSNumber. If it is"Q"(unsigned long long) or"L"(unsigned long), we can safely encode it usinguint64Value.