Skip to content

Commit

Permalink
Correctly renders empty bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
epau committed Nov 15, 2022
1 parent 29c28ea commit 9c48ff6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,32 @@ class HttpBodyMiddleware(
renderEncodedBodyAddedToRequest(bodyDeclaration, dataDeclaration)
}
}
ShapeType.DOCUMENT, ShapeType.STRUCTURE, ShapeType.UNION -> {
ShapeType.STRUCTURE, ShapeType.UNION -> {
// delegate to the member encode function
writer.openBlock("do {", "} catch let err {") {
writer.write("let encoder = context.getEncoder()")
writer.openBlock("if let $memberName = input.operationInput.$memberName {", "} else {") {
writer.write("let $dataDeclaration = try encoder.encode(\$L)", memberName)
renderEncodedBodyAddedToRequest(bodyDeclaration, dataDeclaration)
}
writer.indent()
writer.openBlock("if encoder is JSONEncoder {", "} else if encoder is XMLEncoder {") {
writer.write("// Encode an empty body as an empty structure in JSON")
writer.write("let \$L = \"{}\".data(using: .utf8)!", dataDeclaration)
renderEncodedBodyAddedToRequest(bodyDeclaration, dataDeclaration)
}
writer.indent()
writer.write("// Encode an empty body as an empty string in XML")
writer.write("let \$L = \"\".data(using: .utf8)!", dataDeclaration)
renderEncodedBodyAddedToRequest(bodyDeclaration, dataDeclaration)
writer.dedent()
writer.write("}")
writer.dedent()
writer.write("}")
}
renderErrorCase()
}
ShapeType.DOCUMENT -> {
writer.openBlock("do {", "} catch let err {") {
writer.openBlock("if let $memberName = input.operationInput.$memberName {", "}") {
writer.write("let encoder = context.getEncoder()")
Expand Down
14 changes: 13 additions & 1 deletion smithy-swift-codegen/src/test/kotlin/HttpBodyMiddlewareTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,23 @@ class HttpBodyMiddlewareTests {
Self.Context == H.Context
{
do {
let encoder = context.getEncoder()
if let payload1 = input.operationInput.payload1 {
let encoder = context.getEncoder()
let payload1data = try encoder.encode(payload1)
let payload1body = ClientRuntime.HttpBody.data(payload1data)
input.builder.withBody(payload1body)
} else {
if encoder is JSONEncoder {
// Encode an empty body as an empty structure in JSON
let payload1data = "{}".data(using: .utf8)!
let payload1body = ClientRuntime.HttpBody.data(payload1data)
input.builder.withBody(payload1body)
} else if encoder is XMLEncoder {
// Encode an empty body as an empty string in XML
let payload1data = "".data(using: .utf8)!
let payload1body = ClientRuntime.HttpBody.data(payload1data)
input.builder.withBody(payload1body)
}
}
} catch let err {
throw SdkError<ExplicitStructOutputError>.client(ClientRuntime.ClientError.serializationFailed(err.localizedDescription))
Expand Down

0 comments on commit 9c48ff6

Please sign in to comment.