Skip to content

Commit

Permalink
Merge pull request #45 from pusher/bugfix_ordered_json
Browse files Browse the repository at this point in the history
Bugfix: ordered json - request body vs body_md5
  • Loading branch information
MeenaAlfons authored Dec 30, 2021
2 parents afd3a75 + 63f07c4 commit 9e6d363
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Sources/Pusher/Networking/Client/GetChannelEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct GetChannelEndpoint: APIotaCodableEndpoint {
typealias ErrorResponse = Data
typealias Body = String

let encoder: JSONEncoder = JSONEncoder()
let encoder: JSONEncoder = JSONEncoder.iso8601Ordered

let headers: HTTPHeaders? = APIClient.defaultHeaders

Expand Down
2 changes: 1 addition & 1 deletion Sources/Pusher/Networking/Client/GetChannelsEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct GetChannelsEndpoint: APIotaCodableEndpoint {
typealias ErrorResponse = Data
typealias Body = String

let encoder: JSONEncoder = JSONEncoder()
let encoder: JSONEncoder = JSONEncoder.iso8601Ordered

let headers: HTTPHeaders? = APIClient.defaultHeaders

Expand Down
2 changes: 1 addition & 1 deletion Sources/Pusher/Networking/Client/GetUsersEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct GetUsersEndpoint: APIotaCodableEndpoint {
typealias ErrorResponse = Data
typealias Body = String

let encoder: JSONEncoder = JSONEncoder()
let encoder: JSONEncoder = JSONEncoder.iso8601Ordered

let headers: HTTPHeaders? = APIClient.defaultHeaders

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct TriggerBatchEventsEndpoint: APIotaCodableEndpoint {
typealias ErrorResponse = Data
typealias Body = EventBatch

let encoder: JSONEncoder = JSONEncoder()
let encoder: JSONEncoder = JSONEncoder.iso8601Ordered

var headers: HTTPHeaders? {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct TriggerEventEndpoint: APIotaCodableEndpoint {
typealias ErrorResponse = Data
typealias Body = Event

let encoder: JSONEncoder = JSONEncoder()
let encoder: JSONEncoder = JSONEncoder.iso8601Ordered

var headers: HTTPHeaders? {

Expand Down
2 changes: 1 addition & 1 deletion Sources/Pusher/Networking/Models/AuthInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct AuthInfo: AuthInfoRecord {
version: String = "1.0") where Body: Encodable {

// Generate a MD5 digest of the body (if provided)
if let httpBody = httpBody, let bodyData = try? JSONEncoder().encode(httpBody) {
if let httpBody = httpBody, let bodyData = try? JSONEncoder.iso8601Ordered.encode(httpBody) {
self.bodyMD5 = CryptoService.md5Digest(data: bodyData).hexEncodedString()
} else {
self.bodyMD5 = nil
Expand Down
19 changes: 19 additions & 0 deletions Sources/Pusher/Networking/Models/JSONEncoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Foundation

extension JSONEncoder {
convenience init(dateEncodingStrategy: DateEncodingStrategy,
outputFormatting: OutputFormatting = [],
keyEncodingStrategy: KeyEncodingStrategy = .useDefaultKeys) {
self.init()
self.dateEncodingStrategy = dateEncodingStrategy
self.outputFormatting = outputFormatting
self.keyEncodingStrategy = keyEncodingStrategy
}
}

extension JSONEncoder {
static let shared = JSONEncoder()
static let iso8601 = JSONEncoder(dateEncodingStrategy: .iso8601)
static let iso8601Ordered = JSONEncoder(dateEncodingStrategy: .iso8601, outputFormatting: .sortedKeys)
static let iso8601PrettyPrinted = JSONEncoder(dateEncodingStrategy: .iso8601, outputFormatting: .prettyPrinted)
}

0 comments on commit 9e6d363

Please sign in to comment.