Skip to content
This repository has been archived by the owner on Feb 15, 2024. It is now read-only.

Commit

Permalink
fix(logging): fix http request body logging
Browse files Browse the repository at this point in the history
Fix an issue that would crash the code in debug mode when the optional
request body logging was enabled.

The object already comes parsed by the generated API, so it needs
extra parsing before being ready to be printed.
  • Loading branch information
CassiusPacheco committed Aug 11, 2022
1 parent e1a99ce commit 3241475
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -655,10 +655,11 @@ private struct Logger {
}
privateLog += "\n]"
case .requestBody:
guard let parameters = parameters,
let body = try? JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) else { break }

privateLog += "\n\nBody: \(body)"
guard let data = parameters?["jsonData"] as? Data,
let object = try? JSONSerialization.jsonObject(with: data, options: []),
let jsonData = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]),
let jsonString = String(data: jsonData, encoding: .utf8) else { break }
privateLog += "\n\nBody: \(jsonString)"
case .responseHeaders:
break
case .responseBody:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,10 +655,11 @@ private struct Logger {
}
privateLog += "\n]"
case .requestBody:
guard let parameters = parameters,
let body = try? JSONSerialization.data(withJSONObject: parameters, options: .prettyPrinted) else { break }

privateLog += "\n\nBody: \(body)"
guard let data = parameters?["jsonData"] as? Data,
let object = try? JSONSerialization.jsonObject(with: data, options: []),
let jsonData = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]),
let jsonString = String(data: jsonData, encoding: .utf8) else { break }
privateLog += "\n\nBody: \(jsonString)"
case .responseHeaders:
break
case .responseBody:
Expand Down

0 comments on commit 3241475

Please sign in to comment.