Skip to content

Commit 3ed321f

Browse files
authored
[App Check] Fix JSON key ordering in HeartbeatsPayload (#11482)
Key ordering from `JSONEncoder` is not guaranteed, resulting in test failures when output is compared directly. Added the `sortedKeys` output formatting option in debug builds to fix non-deterministic test results while avoiding unnecessary sorting costs in release builds.
1 parent ba99920 commit 3ed321f

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

FirebaseCore/Internal/Sources/HeartbeatLogging/HeartbeatsPayload.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ extension HeartbeatsPayload: HTTPHeaderRepresentable {
9191
public func headerValue() -> String {
9292
let encoder = JSONEncoder()
9393
encoder.dateEncodingStrategy = .formatted(Self.dateFormatter)
94+
#if DEBUG
95+
// TODO: Remove the following #available check when FirebaseCore's minimum deployment target
96+
// is iOS 11+; all other supported platforms already meet the minimum for `.sortedKeys`.
97+
if #available(iOS 11, *) {
98+
// Sort keys in debug builds to simplify output comparisons in unit tests.
99+
encoder.outputFormatting = .sortedKeys
100+
}
101+
#endif // DEBUG
94102

95103
guard let data = try? encoder.encode(self) else {
96104
// If encoding fails, fall back to encoding with an empty payload.

0 commit comments

Comments
 (0)