From 3e649eb4a7d64e0614c3f9adc967220d1bcde1ee Mon Sep 17 00:00:00 2001 From: 0marperez Date: Fri, 10 Nov 2023 02:28:50 -0500 Subject: [PATCH 1/3] temp --- .../amazon/smithy/kotlin/codegen/model/ShapeExt.kt | 9 ++++++++- .../protocol/HttpBindingProtocolGenerator.kt | 13 ++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) 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..3ff18b28b 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. @@ -218,6 +217,14 @@ fun OperationShape.isOutputEventStream(model: Model): Boolean { return respShape.hasEventStreamMember(model) } +/** + * Returns boolean indicating if operation input is union shaped + */ +fun OperationShape.inputIsUnionShape(model: Model): Boolean { + val reqShape = model.expectShape(input.get()) + return reqShape.isUnionShape +} + /** * Test if a structure shape is an operation input */ 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) From cae26c510907b13d2afe44bc4ad23aa3cc70d1ac Mon Sep 17 00:00:00 2001 From: 0marperez Date: Fri, 10 Nov 2023 04:17:08 -0500 Subject: [PATCH 2/3] function to find if operation payload is union --- .../amazon/smithy/kotlin/codegen/model/ShapeExt.kt | 9 +++++++++ 1 file changed, 9 insertions(+) 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 3ff18b28b..a65402bc2 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 @@ -192,6 +192,15 @@ val Shape.isSparse: Boolean val Shape.isStreaming: Boolean get() = hasTrait() +/** + * Returns boolean indicating if operation input is union shaped + */ +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 */ From 554adcf5eeb66b9810c4a648f622491dcdcdffb2 Mon Sep 17 00:00:00 2001 From: 0marperez Date: Fri, 10 Nov 2023 12:58:38 -0500 Subject: [PATCH 3/3] Clean up and doc correctness --- .../amazon/smithy/kotlin/codegen/model/ShapeExt.kt | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) 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 a65402bc2..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 @@ -193,7 +193,7 @@ val Shape.isStreaming: Boolean get() = hasTrait() /** - * Returns boolean indicating if operation input is union shaped + * Returns boolean indicating if operations explicitly set HTTP payload is a union */ fun OperationShape.payloadIsUnionShape(model: Model): Boolean { val requestShape = model.expectShape(input.get()) @@ -226,14 +226,6 @@ fun OperationShape.isOutputEventStream(model: Model): Boolean { return respShape.hasEventStreamMember(model) } -/** - * Returns boolean indicating if operation input is union shaped - */ -fun OperationShape.inputIsUnionShape(model: Model): Boolean { - val reqShape = model.expectShape(input.get()) - return reqShape.isUnionShape -} - /** * Test if a structure shape is an operation input */