|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +package software.amazon.smithy.kotlin.codegen.aws.protocols |
| 6 | + |
| 7 | +import software.amazon.smithy.kotlin.codegen.aws.protocols.core.AwsHttpBindingProtocolGenerator |
| 8 | +import software.amazon.smithy.kotlin.codegen.aws.protocols.core.StaticHttpBindingResolver |
| 9 | +import software.amazon.smithy.kotlin.codegen.core.KotlinWriter |
| 10 | +import software.amazon.smithy.kotlin.codegen.core.RuntimeTypes |
| 11 | +import software.amazon.smithy.kotlin.codegen.model.* |
| 12 | +import software.amazon.smithy.kotlin.codegen.model.traits.SyntheticClone |
| 13 | +import software.amazon.smithy.kotlin.codegen.rendering.protocol.* |
| 14 | +import software.amazon.smithy.kotlin.codegen.rendering.serde.CborParserGenerator |
| 15 | +import software.amazon.smithy.kotlin.codegen.rendering.serde.CborSerializerGenerator |
| 16 | +import software.amazon.smithy.kotlin.codegen.rendering.serde.StructuredDataParserGenerator |
| 17 | +import software.amazon.smithy.kotlin.codegen.rendering.serde.StructuredDataSerializerGenerator |
| 18 | +import software.amazon.smithy.model.Model |
| 19 | +import software.amazon.smithy.model.knowledge.HttpBinding |
| 20 | +import software.amazon.smithy.model.pattern.UriPattern |
| 21 | +import software.amazon.smithy.model.shapes.OperationShape |
| 22 | +import software.amazon.smithy.model.shapes.ServiceShape |
| 23 | +import software.amazon.smithy.model.shapes.ShapeId |
| 24 | +import software.amazon.smithy.model.traits.HttpTrait |
| 25 | +import software.amazon.smithy.model.traits.TimestampFormatTrait |
| 26 | +import software.amazon.smithy.model.traits.UnitTypeTrait |
| 27 | +import software.amazon.smithy.protocol.traits.Rpcv2CborTrait |
| 28 | + |
| 29 | +class RpcV2Cbor : AwsHttpBindingProtocolGenerator() { |
| 30 | + override val protocol: ShapeId = Rpcv2CborTrait.ID |
| 31 | + |
| 32 | + // TODO Timestamp format is not used in RpcV2Cbor since it's a binary protocol. We seem to be missing an abstraction |
| 33 | + // between text-based and binary-based protocols |
| 34 | + override val defaultTimestampFormat = TimestampFormatTrait.Format.UNKNOWN |
| 35 | + |
| 36 | + override fun getProtocolHttpBindingResolver(model: Model, serviceShape: ServiceShape): HttpBindingResolver = |
| 37 | + RpcV2CborHttpBindingResolver(model, serviceShape) |
| 38 | + |
| 39 | + override fun structuredDataSerializer(ctx: ProtocolGenerator.GenerationContext): StructuredDataSerializerGenerator = |
| 40 | + CborSerializerGenerator(this) |
| 41 | + |
| 42 | + override fun structuredDataParser(ctx: ProtocolGenerator.GenerationContext): StructuredDataParserGenerator = |
| 43 | + CborParserGenerator(this) |
| 44 | + |
| 45 | + override fun renderDeserializeErrorDetails(ctx: ProtocolGenerator.GenerationContext, op: OperationShape, writer: KotlinWriter) { |
| 46 | + writer.write("#T.deserialize(payload)", RuntimeTypes.SmithyRpcV2Protocols.Cbor.RpcV2CborErrorDeserializer) |
| 47 | + } |
| 48 | + |
| 49 | + override fun getDefaultHttpMiddleware(ctx: ProtocolGenerator.GenerationContext): List<ProtocolMiddleware> { |
| 50 | + // Every request MUST contain a `smithy-protocol` header with the value of `rpc-v2-cbor` |
| 51 | + val smithyProtocolHeaderMiddleware = MutateHeadersMiddleware(overrideHeaders = mapOf("smithy-protocol" to "rpc-v2-cbor")) |
| 52 | + |
| 53 | + // Every response MUST contain the same `smithy-protocol` header, otherwise it's considered invalid |
| 54 | + val validateSmithyProtocolHeaderMiddleware = object : ProtocolMiddleware { |
| 55 | + override val name: String = "RpcV2CborValidateSmithyProtocolResponseHeader" |
| 56 | + override fun render(ctx: ProtocolGenerator.GenerationContext, op: OperationShape, writer: KotlinWriter) { |
| 57 | + val interceptorSymbol = RuntimeTypes.SmithyRpcV2Protocols.Cbor.RpcV2CborSmithyProtocolResponseHeaderInterceptor |
| 58 | + writer.write("op.interceptors.add(#T)", interceptorSymbol) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // Requests with event stream responses MUST include an `Accept` header set to the value `application/vnd.amazon.eventstream` |
| 63 | + val eventStreamsAcceptHeaderMiddleware = object : ProtocolMiddleware { |
| 64 | + private val mutateHeadersMiddleware = MutateHeadersMiddleware(extraHeaders = mapOf("Accept" to "application/vnd.amazon.eventstream")) |
| 65 | + |
| 66 | + override fun isEnabledFor(ctx: ProtocolGenerator.GenerationContext, op: OperationShape): Boolean = op.isOutputEventStream(ctx.model) |
| 67 | + override val name: String = "RpcV2CborEventStreamsAcceptHeaderMiddleware" |
| 68 | + override fun render(ctx: ProtocolGenerator.GenerationContext, op: OperationShape, writer: KotlinWriter) = mutateHeadersMiddleware.render(ctx, op, writer) |
| 69 | + } |
| 70 | + |
| 71 | + // Emit a metric to track usage of RpcV2Cbor |
| 72 | + val businessMetricsMiddleware = object : ProtocolMiddleware { |
| 73 | + override val name: String = "RpcV2CborBusinessMetricsMiddleware" |
| 74 | + override fun render(ctx: ProtocolGenerator.GenerationContext, op: OperationShape, writer: KotlinWriter) { |
| 75 | + writer.write("op.context.#T(#T.PROTOCOL_RPC_V2_CBOR)", RuntimeTypes.Core.BusinessMetrics.emitBusinessMetric, RuntimeTypes.Core.BusinessMetrics.SmithyBusinessMetric) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + return super.getDefaultHttpMiddleware(ctx) + listOf( |
| 80 | + smithyProtocolHeaderMiddleware, |
| 81 | + validateSmithyProtocolHeaderMiddleware, |
| 82 | + eventStreamsAcceptHeaderMiddleware, |
| 83 | + businessMetricsMiddleware, |
| 84 | + ) |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Exact copy of [HttpBindingProtocolGenerator.renderSerializeHttpBody] but with a custom |
| 89 | + * [OperationShape.hasHttpBody] function to handle protocol-specific serialization rules. |
| 90 | + */ |
| 91 | + override fun renderSerializeHttpBody( |
| 92 | + ctx: ProtocolGenerator.GenerationContext, |
| 93 | + op: OperationShape, |
| 94 | + writer: KotlinWriter, |
| 95 | + ) { |
| 96 | + val resolver = getProtocolHttpBindingResolver(ctx.model, ctx.service) |
| 97 | + if (!op.hasHttpBody(ctx)) return |
| 98 | + |
| 99 | + // payload member(s) |
| 100 | + val requestBindings = resolver.requestBindings(op) |
| 101 | + val httpPayload = requestBindings.firstOrNull { it.location == HttpBinding.Location.PAYLOAD } |
| 102 | + if (httpPayload != null) { |
| 103 | + renderExplicitHttpPayloadSerializer(ctx, httpPayload, writer) |
| 104 | + } else { |
| 105 | + val documentMembers = requestBindings.filterDocumentBoundMembers() |
| 106 | + // Unbound document members that should be serialized into the document format for the protocol. |
| 107 | + // delegate to the generate operation body serializer function |
| 108 | + val sdg = structuredDataSerializer(ctx) |
| 109 | + val opBodySerializerFn = sdg.operationSerializer(ctx, op, documentMembers) |
| 110 | + writer.write("builder.body = #T(context, input)", opBodySerializerFn) |
| 111 | + } |
| 112 | + renderContentTypeHeader(ctx, op, writer, resolver) |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @return whether the operation input does _not_ target the unit shape ([UnitTypeTrait.UNIT]) |
| 117 | + */ |
| 118 | + private fun OperationShape.hasHttpBody(ctx: ProtocolGenerator.GenerationContext): Boolean { |
| 119 | + val input = ctx.model.expectShape(inputShape).targetOrSelf(ctx.model).let { |
| 120 | + // If the input has been synthetically cloned from the original (most likely), |
| 121 | + // pull the archetype and check _that_ |
| 122 | + it.getTrait<SyntheticClone>()?.let { clone -> |
| 123 | + ctx.model.expectShape(clone.archetype).targetOrSelf(ctx.model) |
| 124 | + } ?: it |
| 125 | + } |
| 126 | + |
| 127 | + return input.id != UnitTypeTrait.UNIT |
| 128 | + } |
| 129 | + |
| 130 | + override fun renderContentTypeHeader( |
| 131 | + ctx: ProtocolGenerator.GenerationContext, |
| 132 | + op: OperationShape, |
| 133 | + writer: KotlinWriter, |
| 134 | + resolver: HttpBindingResolver, |
| 135 | + ) { |
| 136 | + writer.write("builder.headers.setMissing(\"Content-Type\", #S)", resolver.determineRequestContentType(op)) |
| 137 | + } |
| 138 | + |
| 139 | + class RpcV2CborHttpBindingResolver( |
| 140 | + model: Model, |
| 141 | + val serviceShape: ServiceShape, |
| 142 | + ) : StaticHttpBindingResolver( |
| 143 | + model, |
| 144 | + serviceShape, |
| 145 | + HttpTrait.builder().code(200).method("POST").uri(UriPattern.parse("/")).build(), |
| 146 | + "application/cbor", |
| 147 | + TimestampFormatTrait.Format.UNKNOWN, |
| 148 | + ) { |
| 149 | + |
| 150 | + override fun httpTrait(operationShape: OperationShape): HttpTrait = HttpTrait |
| 151 | + .builder() |
| 152 | + .code(200) |
| 153 | + .method("POST") |
| 154 | + .uri(UriPattern.parse("/service/${serviceShape.id.name}/operation/${operationShape.id.name}")) |
| 155 | + .build() |
| 156 | + |
| 157 | + override fun determineRequestContentType(operationShape: OperationShape): String = when { |
| 158 | + operationShape.isInputEventStream(model) -> "application/vnd.amazon.eventstream" |
| 159 | + else -> "application/cbor" |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments