From 77456aa4f82cd4339bd47fc0f685b05aafbbc3d4 Mon Sep 17 00:00:00 2001 From: Lawrence Forooghian Date: Wed, 6 Nov 2024 22:37:14 -0300 Subject: [PATCH] Fix decoding of Message.createdAt Noticed this when writing an integration test for getPreviousMessages. --- Sources/AblyChat/ChatAPI.swift | 9 ++++++++- Tests/AblyChatTests/ChatAPITests.swift | 2 +- .../AblyChatTests/Mocks/MockHTTPPaginatedResponse.swift | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Sources/AblyChat/ChatAPI.swift b/Sources/AblyChat/ChatAPI.swift index 7a610f14..6af519e3 100644 --- a/Sources/AblyChat/ChatAPI.swift +++ b/Sources/AblyChat/ChatAPI.swift @@ -127,7 +127,14 @@ internal final class ChatAPI: Sendable { } internal struct DictionaryDecoder { - private let decoder = JSONDecoder() + private let decoder = { + var decoder = JSONDecoder() + + // Ably’s REST APIs always serialise dates as milliseconds since Unix epoch + decoder.dateDecodingStrategy = .millisecondsSince1970 + + return decoder + }() // Function to decode from a dictionary internal func decode(_: T.Type, from dictionary: NSDictionary) throws -> T { diff --git a/Tests/AblyChatTests/ChatAPITests.swift b/Tests/AblyChatTests/ChatAPITests.swift index 761232ea..cd0bf003 100644 --- a/Tests/AblyChatTests/ChatAPITests.swift +++ b/Tests/AblyChatTests/ChatAPITests.swift @@ -170,7 +170,7 @@ struct ChatAPITests { clientID: "random", roomID: roomId, text: "hello", - createdAt: nil, + createdAt: .init(timeIntervalSince1970: 1_730_943_049.269), metadata: [:], headers: [:] ), diff --git a/Tests/AblyChatTests/Mocks/MockHTTPPaginatedResponse.swift b/Tests/AblyChatTests/Mocks/MockHTTPPaginatedResponse.swift index cbf2ed37..466979fd 100644 --- a/Tests/AblyChatTests/Mocks/MockHTTPPaginatedResponse.swift +++ b/Tests/AblyChatTests/Mocks/MockHTTPPaginatedResponse.swift @@ -99,6 +99,7 @@ extension MockHTTPPaginatedResponse { [ "clientId": "random", "timeserial": "3446456", + "createdAt": 1_730_943_049_269, "roomId": "basketball::$chat::$chatMessages", "text": "hello", "metadata": [:],