Skip to content

Commit 9e6d363

Browse files
authored
Merge pull request #45 from pusher/bugfix_ordered_json
Bugfix: ordered json - request body vs body_md5
2 parents afd3a75 + 63f07c4 commit 9e6d363

File tree

7 files changed

+25
-6
lines changed

7 files changed

+25
-6
lines changed

Sources/Pusher/Networking/Client/GetChannelEndpoint.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct GetChannelEndpoint: APIotaCodableEndpoint {
88
typealias ErrorResponse = Data
99
typealias Body = String
1010

11-
let encoder: JSONEncoder = JSONEncoder()
11+
let encoder: JSONEncoder = JSONEncoder.iso8601Ordered
1212

1313
let headers: HTTPHeaders? = APIClient.defaultHeaders
1414

Sources/Pusher/Networking/Client/GetChannelsEndpoint.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct GetChannelsEndpoint: APIotaCodableEndpoint {
88
typealias ErrorResponse = Data
99
typealias Body = String
1010

11-
let encoder: JSONEncoder = JSONEncoder()
11+
let encoder: JSONEncoder = JSONEncoder.iso8601Ordered
1212

1313
let headers: HTTPHeaders? = APIClient.defaultHeaders
1414

Sources/Pusher/Networking/Client/GetUsersEndpoint.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct GetUsersEndpoint: APIotaCodableEndpoint {
88
typealias ErrorResponse = Data
99
typealias Body = String
1010

11-
let encoder: JSONEncoder = JSONEncoder()
11+
let encoder: JSONEncoder = JSONEncoder.iso8601Ordered
1212

1313
let headers: HTTPHeaders? = APIClient.defaultHeaders
1414

Sources/Pusher/Networking/Client/TriggerBatchEventsEndpoint.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct TriggerBatchEventsEndpoint: APIotaCodableEndpoint {
88
typealias ErrorResponse = Data
99
typealias Body = EventBatch
1010

11-
let encoder: JSONEncoder = JSONEncoder()
11+
let encoder: JSONEncoder = JSONEncoder.iso8601Ordered
1212

1313
var headers: HTTPHeaders? {
1414

Sources/Pusher/Networking/Client/TriggerEventEndpoint.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ struct TriggerEventEndpoint: APIotaCodableEndpoint {
88
typealias ErrorResponse = Data
99
typealias Body = Event
1010

11-
let encoder: JSONEncoder = JSONEncoder()
11+
let encoder: JSONEncoder = JSONEncoder.iso8601Ordered
1212

1313
var headers: HTTPHeaders? {
1414

Sources/Pusher/Networking/Models/AuthInfo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct AuthInfo: AuthInfoRecord {
4747
version: String = "1.0") where Body: Encodable {
4848

4949
// Generate a MD5 digest of the body (if provided)
50-
if let httpBody = httpBody, let bodyData = try? JSONEncoder().encode(httpBody) {
50+
if let httpBody = httpBody, let bodyData = try? JSONEncoder.iso8601Ordered.encode(httpBody) {
5151
self.bodyMD5 = CryptoService.md5Digest(data: bodyData).hexEncodedString()
5252
} else {
5353
self.bodyMD5 = nil
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Foundation
2+
3+
extension JSONEncoder {
4+
convenience init(dateEncodingStrategy: DateEncodingStrategy,
5+
outputFormatting: OutputFormatting = [],
6+
keyEncodingStrategy: KeyEncodingStrategy = .useDefaultKeys) {
7+
self.init()
8+
self.dateEncodingStrategy = dateEncodingStrategy
9+
self.outputFormatting = outputFormatting
10+
self.keyEncodingStrategy = keyEncodingStrategy
11+
}
12+
}
13+
14+
extension JSONEncoder {
15+
static let shared = JSONEncoder()
16+
static let iso8601 = JSONEncoder(dateEncodingStrategy: .iso8601)
17+
static let iso8601Ordered = JSONEncoder(dateEncodingStrategy: .iso8601, outputFormatting: .sortedKeys)
18+
static let iso8601PrettyPrinted = JSONEncoder(dateEncodingStrategy: .iso8601, outputFormatting: .prettyPrinted)
19+
}

0 commit comments

Comments
 (0)