Skip to content

Commit ead9050

Browse files
Resolve issue where Dates passed as input that contain a microsecond portion would cause API exceptions
1 parent 1cea28d commit ead9050

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

Sources/AppStoreServerLibrary/Utility.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ internal func getJsonDecoder() -> JSONDecoder {
1919
}
2020

2121
internal func getJsonEncoder() -> JSONEncoder {
22-
let decoder = JSONEncoder()
23-
decoder.dateEncodingStrategy = .millisecondsSince1970
24-
return decoder
22+
let encoder = JSONEncoder()
23+
encoder.dateEncodingStrategy = .custom({ date, e in
24+
// To encode the same as millisecondsSince1970, however truncating the decimal part
25+
var container = e.singleValueContainer()
26+
try container.encode((date.timeIntervalSince1970 * 1000.0).rounded(.towardZero))
27+
})
28+
return encoder
2529
}

Tests/AppStoreServerLibraryTests/AppStoreServerAPIClientTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,25 @@ final class AppStoreServerAPIClientTests: XCTestCase {
246246
TestingUtility.confirmCodableInternallyConsistent(notificationHistoryResponse)
247247
}
248248

249+
public func testGetNotificationHistoryWithMicrosecondValues() async throws {
250+
let client = try getClientWithBody("resources/models/getNotificationHistoryResponse.json") { request, body in
251+
let decodedJson = try! JSONSerialization.jsonObject(with: body!) as! [String: Any]
252+
XCTAssertEqual(1698148900000, decodedJson["startDate"] as! Int)
253+
XCTAssertEqual(1698148950000, decodedJson["endDate"] as! Int)
254+
}
255+
256+
let notificationHistoryRequest = NotificationHistoryRequest(
257+
startDate: Date(timeIntervalSince1970: 1698148900).advanced(by: 0.000_9), // 900 microseconds
258+
endDate: Date(timeIntervalSince1970: 1698148950).advanced(by: 0.000_001), // 1 microsecond
259+
notificationType: NotificationTypeV2.subscribed,
260+
notificationSubtype: Subtype.initialBuy,
261+
transactionId: "999733843",
262+
onlyFailures: true
263+
)
264+
265+
let _ = await client.getNotificationHistory(paginationToken: "a036bc0e-52b8-4bee-82fc-8c24cb6715d6", notificationHistoryRequest: notificationHistoryRequest)
266+
}
267+
249268
public func testGetTransactionHistoryV1() async throws {
250269
let client = try getClientWithBody("resources/models/transactionHistoryResponse.json") { request, body in
251270
XCTAssertEqual(.GET, request.method)

0 commit comments

Comments
 (0)