Skip to content

Commit

Permalink
Fix decoding of Message.createdAt
Browse files Browse the repository at this point in the history
Noticed this when writing an integration test for getPreviousMessages.
  • Loading branch information
lawrence-forooghian committed Nov 7, 2024
1 parent c6525a2 commit 77456aa
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
9 changes: 8 additions & 1 deletion Sources/AblyChat/ChatAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: Decodable>(_: T.Type, from dictionary: NSDictionary) throws -> T {
Expand Down
2 changes: 1 addition & 1 deletion Tests/AblyChatTests/ChatAPITests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ struct ChatAPITests {
clientID: "random",
roomID: roomId,
text: "hello",
createdAt: nil,
createdAt: .init(timeIntervalSince1970: 1_730_943_049.269),
metadata: [:],
headers: [:]
),
Expand Down
1 change: 1 addition & 0 deletions Tests/AblyChatTests/Mocks/MockHTTPPaginatedResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ extension MockHTTPPaginatedResponse {
[
"clientId": "random",
"timeserial": "3446456",
"createdAt": 1_730_943_049_269,
"roomId": "basketball::$chat::$chatMessages",
"text": "hello",
"metadata": [:],
Expand Down

0 comments on commit 77456aa

Please sign in to comment.