Skip to content

Commit

Permalink
refactor: xml content type header renderer to be overridable (#977)
Browse files Browse the repository at this point in the history
  • Loading branch information
0marperez authored Nov 14, 2023
1 parent 0409317 commit 7abd49a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -193,6 +192,15 @@ val Shape.isSparse: Boolean
val Shape.isStreaming: Boolean
get() = hasTrait<StreamingTrait>()

/**
* Returns boolean indicating if operations explicitly set HTTP payload is a union
*/
fun OperationShape.payloadIsUnionShape(model: Model): Boolean {
val requestShape = model.expectShape<StructureShape>(input.get())
val payload = requestShape.findMemberWithTrait<HttpPayloadTrait>(model)?.targetOrSelf(model)
return payload is UnionShape
}

/**
* Test if a member targets an event stream
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 7abd49a

Please sign in to comment.