Skip to content

Commit 28fc3d3

Browse files
author
luigi
committed
httplabel and httpquery
1 parent 34e1a24 commit 28fc3d3

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/protocol/HttpBindingProtocolGenerator.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,12 @@ abstract class HttpBindingProtocolGenerator : ProtocolGenerator {
191191
"awsRestjson1" -> KotlinTypes.String
192192
else -> KotlinTypes.ByteArray
193193
}
194+
195+
val defaultResponse = when (protocolName) {
196+
"smithyRpcv2cbor" -> "ByteArray(0)"
197+
"awsRestjson1" -> "\"\""
198+
else -> "ByteArray(0)"
199+
}
194200
writer
195201
.openBlock("internal class #T {", serializerSymbol)
196202
.call {
@@ -200,7 +206,7 @@ abstract class HttpBindingProtocolGenerator : ProtocolGenerator {
200206
serializationSymbol,
201207
serializerResultSymbol,
202208
)
203-
.write("var response: Any")
209+
.write("var response: #T = $defaultResponse", serializerResultSymbol)
204210
.call {
205211
renderSerializeHttpBody(ctx, op, writer)
206212
}

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/service/KtorStubGenerator.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import software.amazon.smithy.kotlin.codegen.model.getTrait
1313
import software.amazon.smithy.model.shapes.OperationShape
1414
import software.amazon.smithy.model.traits.AuthTrait
1515
import software.amazon.smithy.model.traits.HttpBearerAuthTrait
16+
import software.amazon.smithy.model.traits.HttpLabelTrait
17+
import software.amazon.smithy.model.traits.HttpQueryTrait
1618
import software.amazon.smithy.model.traits.HttpTrait
1719
import software.amazon.smithy.utils.AbstractCodeWriter
1820

@@ -261,7 +263,7 @@ internal class KtorStubGenerator(
261263
)
262264
write("val deserializer = ${shape.id.name}OperationDeserializer()")
263265
withBlock(
264-
"val requestObj = try { deserializer.deserialize(#T(), request) } catch (ex: Exception) {",
266+
"var requestObj = try { deserializer.deserialize(#T(), request) } catch (ex: Exception) {",
265267
"}",
266268
RuntimeTypes.Core.ExecutionContext,
267269
) {
@@ -271,6 +273,10 @@ internal class KtorStubGenerator(
271273
"Malformed CBOR input",
272274
)
273275
}
276+
withBlock("requestObj = requestObj.copy {", "}") {
277+
call { readHttpLabel(shape, writer) }
278+
call { readHttpQuery(shape, writer) }
279+
}
274280
write("val responseObj = handle${shape.id.name}Request(requestObj)")
275281
write("val serializer = ${shape.id.name}OperationSerializer()")
276282
withBlock(
@@ -298,6 +304,25 @@ internal class KtorStubGenerator(
298304
}
299305
}
300306

307+
private fun readHttpLabel(shape: OperationShape, writer: KotlinWriter) {
308+
val inputShape = ctx.model.expectShape(shape.input.get())
309+
inputShape.allMembers.forEach { member ->
310+
val memberName = member.key
311+
if (!member.value.hasTrait(HttpLabelTrait.ID)) return@forEach
312+
writer.write("$memberName = call.parameters[#S]", memberName)
313+
}
314+
}
315+
316+
private fun readHttpQuery(shape: OperationShape, writer: KotlinWriter) {
317+
val inputShape = ctx.model.expectShape(shape.input.get())
318+
inputShape.allMembers.forEach { member ->
319+
val memberName = member.key
320+
if (!member.value.hasTrait(HttpQueryTrait.ID)) return@forEach
321+
val httpQueryTrait = member.value.getTrait<HttpQueryTrait>()!!
322+
writer.write("$memberName = call.request.queryParameters[#S]", httpQueryTrait.value)
323+
}
324+
}
325+
301326
private fun renderRoutingAuth(w: KotlinWriter, shape: OperationShape) {
302327
val hasServiceHttpBearerAuthTrait = serviceShape.hasTrait(HttpBearerAuthTrait.ID)
303328
val authTrait = shape.getTrait<AuthTrait>()

0 commit comments

Comments
 (0)