Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Sources/SmithyJSON/Reader/Reader+JSONDeserialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
//

import struct Foundation.Data
import class Foundation.NSError
import class Foundation.JSONSerialization
import class Foundation.NSNull

extension Reader {

public static func from(data: Data) throws -> Reader {
// Empty bodies are allowed. When the body is empty,
// return a reader with no JSON content.
guard !data.isEmpty else { return Reader(nodeInfo: "", parent: nil) }
do {
let jsonObject = try JSONSerialization.jsonObject(with: data, options: [.fragmentsAllowed])
return try Reader(nodeInfo: "", jsonObject: jsonObject)
} catch let error as NSError where error.domain == "NSCocoaErrorDomain" && error.code == 3840 {
return try Reader(nodeInfo: "", jsonObject: [:])
}

// Attempt to parse JSON from the non-empty body.
// Throw an error if JSON is invalid.
// (Determine whether to wrap this error)
let jsonObject = try JSONSerialization.jsonObject(with: data)
return try Reader(nodeInfo: "", jsonObject: jsonObject)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,19 +223,20 @@ class OrchestratorTests: XCTestCase {
.attributes(attributes)
.serialize({ input, builder, _ in
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests below used JSON fragments (i.e. a JSON string only) for serde since fragments were allowed and it kept the test setup simpler. Tests now serialize JSON objects.

trace.append("serialize")
let data = try JSONEncoder().encode(["foo": input.foo])
builder.withMethod(.get)
.withPath("/")
.withHost("localhost")
.withBody(.data(try! JSONEncoder().encode(input.foo)))
.withBody(.data(data))
})
.deserialize({ response, _ in
trace.append("deserialize")
if (200..<300).contains(response.statusCode.rawValue) {
guard case let .data(data) = response.body else {
return TestOutput(bar: "")
}
let bar = try! JSONDecoder().decode(String.self, from: data!)
return TestOutput(bar: bar)
let object = try! JSONDecoder().decode([String: String].self, from: data!)
return TestOutput(bar: object["foo"]!)
} else {
let responseReader = try SmithyJSON.Reader.from(data: try await response.data())
let baseError = try TestBaseError(httpResponse: response, responseReader: responseReader, noErrorWrapping: true)
Expand Down Expand Up @@ -1373,7 +1374,8 @@ class OrchestratorTests: XCTestCase {
let orchestrator = traceOrchestrator(trace: trace)
.retryStrategy(DefaultRetryStrategy(options: RetryStrategyOptions(backoffStrategy: ImmediateBackoffStrategy())))
.serialize({ (input: TestInput, builder: HTTPRequestBuilder, context) in
builder.withBody(.data(Data("\"\(input.foo)\"".utf8)))
let data = try JSONEncoder().encode(["foo": input.foo])
builder.withBody(.data(data))
})
.executeRequest(executeRequest)
let result = await asyncResult {
Expand Down
Loading