Skip to content

Commit

Permalink
Merge pull request #46 from swift-aws/encodePayloadPathAsJson
Browse files Browse the repository at this point in the history
properly serialize the payloadPath object as json / xml
  • Loading branch information
jonnymacs authored Feb 14, 2019
2 parents ee093b1 + 8ae6070 commit 5ca6f97
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
19 changes: 16 additions & 3 deletions Sources/AWSSDKSwiftCore/AWSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,15 @@ extension AWSClient {
switch serviceProtocol.type {
case .json, .restjson:
if let payload = Input.payloadPath, let payloadBody = mirror.getAttribute(forKey: payload.toSwiftVariableCase()) {
body = Body(anyValue: payloadBody)
switch payloadBody {
case is AWSShape:
let inputBody: Body = .json(try AWSShapeEncoder().encodeToJSONUTF8Data(input))
if let inputDict = try inputBody.asDictionary(), let payloadDict = inputDict[payload] {
body = .json(try JSONSerialization.data(withJSONObject: payloadDict))
}
default:
body = Body(anyValue: payloadBody)
}
headers.removeValue(forKey: payload.toSwiftVariableCase())
} else {
body = .json(try AWSShapeEncoder().encodeToJSONUTF8Data(input))
Expand All @@ -325,7 +333,12 @@ extension AWSClient {

case .restxml:
if let payload = Input.payloadPath, let payloadBody = mirror.getAttribute(forKey: payload.toSwiftVariableCase()) {
body = Body(anyValue: payloadBody)
switch payloadBody {
case let pb as AWSShape:
body = .xml(try AWSShapeEncoder().encodeToXMLNode(pb))
default:
body = Body(anyValue: payloadBody)
}
headers.removeValue(forKey: payload.toSwiftVariableCase())
} else {
body = .xml(try AWSShapeEncoder().encodeToXMLNode(input))
Expand Down Expand Up @@ -355,7 +368,7 @@ extension AWSClient {
queryParams[item.name] = item.value
}
}

urlComponents.queryItems = urlQueryItems(fromDictionary: queryParams)

guard let url = urlComponents.url else {
Expand Down
81 changes: 81 additions & 0 deletions Tests/AWSSDKSwiftCoreTests/AWSClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,38 @@ class AWSClientTests: XCTestCase {
let value = "<html><body><a href=\"https://redsox.com\">Test</a></body></html>"
}

struct E: AWSShape {
public static var members: [AWSShapeMember] = [
AWSShapeMember(label: "Member", required: true, type: .list),
]

let Member = ["memberKey": "memberValue", "memberKey2": "memberValue2"]

private enum CodingKeys: String, CodingKey {
case Member = "Member"
}
}

struct F: AWSShape {
public static let payloadPath: String? = "fooParams"

public static var members: [AWSShapeMember] = [
AWSShapeMember(label: "Member", required: true, type: .list),
AWSShapeMember(label: "fooParams", required: false, type: .structure),
]

public let fooParams: E?

public init(fooParams: E? = nil) {
self.fooParams = fooParams
}

private enum CodingKeys: String, CodingKey {
case fooParams = "fooParams"
}

}

func testCreateAWSRequest() {
let input = C()

Expand Down Expand Up @@ -103,6 +135,55 @@ class AWSClientTests: XCTestCase {
} catch {
XCTFail(error.localizedDescription)
}

// encode Shape with payloadPath
//
let input2 = E()
let input3 = F(fooParams: input2)

// encode for restxml
//
do {
let awsRequest = try s3Client.debugCreateAWSRequest(
operation: "payloadPath",
path: "/Bucket?list-type=2",
httpMethod: "POST",
input: input3
)

XCTAssertNotNil(awsRequest.body)
if let xmlData = try awsRequest.body.asData() {
let xmlNode = try XML2Parser(data: xmlData).parse()
let json = XMLNodeSerializer(node: xmlNode).serializeToJSON()
let json_data = json.data(using: .utf8)!
let dict = try! JSONSerializer().serializeToDictionary(json_data)
let fromJson = dict["E"]! as! [String: String]
XCTAssertEqual(fromJson["MemberKey"], "memberValue")
}
_ = try awsRequest.toNIORequest()
} catch {
XCTFail(error.localizedDescription)
}

// encode for json
//
do {
let awsRequest = try kinesisClient.debugCreateAWSRequest(
operation: "PutRecord",
path: "/",
httpMethod: "POST",
input: input3
)
XCTAssertNotNil(awsRequest.body)
if let jsonData = try awsRequest.body.asData() {
let jsonBody = try! JSONSerialization.jsonObject(with: jsonData, options: .allowFragments) as! [String:Any]
let fromJson = jsonBody["Member"]! as! [String: String]
XCTAssertEqual(fromJson["memberKey"], "memberValue")
}

} catch {
XCTFail(error.localizedDescription)
}
}

static var allTests : [(String, (AWSClientTests) -> () throws -> Void)] {
Expand Down

0 comments on commit 5ca6f97

Please sign in to comment.