diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/model/ShapeExt.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/model/ShapeExt.kt index b6b097277..2401238a2 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/model/ShapeExt.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/model/ShapeExt.kt @@ -21,7 +21,6 @@ import software.amazon.smithy.rulesengine.language.EndpointRuleSet import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait import software.amazon.smithy.rulesengine.traits.EndpointTestCase import software.amazon.smithy.rulesengine.traits.EndpointTestsTrait -import kotlin.streams.toList /** * Get all shapes of a particular type from the model. @@ -193,6 +192,15 @@ val Shape.isSparse: Boolean val Shape.isStreaming: Boolean get() = hasTrait() +/** + * Returns boolean indicating if operations explicitly set HTTP payload is a union + */ +fun OperationShape.payloadIsUnionShape(model: Model): Boolean { + val requestShape = model.expectShape(input.get()) + val payload = requestShape.findMemberWithTrait(model)?.targetOrSelf(model) + return payload is UnionShape +} + /** * Test if a member targets an event stream */ diff --git a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpBindingProtocolGenerator.kt b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpBindingProtocolGenerator.kt index 4a671bfb1..09cb6b5a2 100644 --- a/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpBindingProtocolGenerator.kt +++ b/codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpBindingProtocolGenerator.kt @@ -12,7 +12,10 @@ import software.amazon.smithy.kotlin.codegen.core.* import software.amazon.smithy.kotlin.codegen.lang.KotlinTypes import software.amazon.smithy.kotlin.codegen.lang.toEscapedLiteral import software.amazon.smithy.kotlin.codegen.model.* -import software.amazon.smithy.kotlin.codegen.rendering.serde.* +import software.amazon.smithy.kotlin.codegen.rendering.serde.deserializerName +import software.amazon.smithy.kotlin.codegen.rendering.serde.formatInstant +import software.amazon.smithy.kotlin.codegen.rendering.serde.parseInstant +import software.amazon.smithy.kotlin.codegen.rendering.serde.serializerName import software.amazon.smithy.kotlin.codegen.utils.getOrNull import software.amazon.smithy.model.Model import software.amazon.smithy.model.knowledge.HttpBinding @@ -267,7 +270,15 @@ abstract class HttpBindingProtocolGenerator : ProtocolGenerator { writer.write("val payload = #T(context, input)", opBodySerializerFn) writer.write("builder.body = #T.fromBytes(payload)", RuntimeTypes.Http.HttpBody) } + renderContentTypeHeader(ctx, op, writer, resolver) + } + protected open fun renderContentTypeHeader( + ctx: ProtocolGenerator.GenerationContext, + op: OperationShape, + writer: KotlinWriter, + resolver: HttpBindingResolver = getProtocolHttpBindingResolver(ctx.model, ctx.service), + ) { resolver.determineRequestContentType(op)?.let { contentType -> writer.withBlock("if (builder.body !is HttpBody.Empty) {", "}") { write("builder.headers.setMissing(\"Content-Type\", #S)", contentType)