From dc8d4436cd31af20df811b8d08e3dbe3a47c078f Mon Sep 17 00:00:00 2001 From: Harry Barber <106155934+hlbarber@users.noreply.github.com> Date: Tue, 13 Sep 2022 22:15:45 +0100 Subject: [PATCH] Move methods from Protocol to ServerProtocol (#1731) --- .../client/smithy/protocols/AwsJson.kt | 20 +----- .../client/smithy/protocols/AwsQuery.kt | 14 ---- .../client/smithy/protocols/Ec2Query.kt | 14 ---- .../client/smithy/protocols/Protocol.kt | 24 ------- .../client/smithy/protocols/RestJson.kt | 13 ---- .../client/smithy/protocols/RestXml.kt | 13 ---- .../smithy/PythonServerCodegenVisitor.kt | 3 +- .../PythonServerServiceGenerator.kt | 4 +- .../server/smithy/ServerCodegenVisitor.kt | 3 +- .../ServerOperationRegistryGenerator.kt | 4 +- .../generators/ServerServiceGenerator.kt | 3 +- .../generators/protocol/ServerProtocol.kt | 69 +++++++++++++++++++ .../server/smithy/protocols/ServerAwsJson.kt | 15 +--- .../ServerHttpBoundProtocolGenerator.kt | 5 +- .../smithy/protocols/ServerRestJsonFactory.kt | 3 +- .../smithy/protocols/ServerRestXmlFactory.kt | 3 +- .../ServerOperationRegistryGeneratorTest.kt | 3 +- 17 files changed, 90 insertions(+), 123 deletions(-) diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/AwsJson.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/AwsJson.kt index 44a0cfc176..c43100c469 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/AwsJson.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/AwsJson.kt @@ -18,7 +18,6 @@ import software.amazon.smithy.rust.codegen.client.rustlang.asType import software.amazon.smithy.rust.codegen.client.rustlang.rust import software.amazon.smithy.rust.codegen.client.rustlang.rustBlockTemplate import software.amazon.smithy.rust.codegen.client.rustlang.rustTemplate -import software.amazon.smithy.rust.codegen.client.rustlang.writable import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext import software.amazon.smithy.rust.codegen.client.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.client.smithy.RuntimeType @@ -130,7 +129,7 @@ class AwsJsonSerializerGenerator( open class AwsJson( val coreCodegenContext: CoreCodegenContext, - private val awsJsonVersion: AwsJsonVersion, + val awsJsonVersion: AwsJsonVersion, ) : Protocol { private val runtimeConfig = coreCodegenContext.runtimeConfig private val errorScope = arrayOf( @@ -183,23 +182,6 @@ open class AwsJson( *errorScope, ) } - - /** - * Returns the operation name as required by the awsJson1.x protocols. - */ - override fun serverRouterRequestSpec( - operationShape: OperationShape, - operationName: String, - serviceName: String, - requestSpecModule: RuntimeType, - ) = writable { - rust("""String::from("$serviceName.$operationName")""") - } - - override fun serverRouterRuntimeConstructor() = when (awsJsonVersion) { - AwsJsonVersion.Json10 -> "new_aws_json_10_router" - AwsJsonVersion.Json11 -> "new_aws_json_11_router" - } } fun awsJsonFieldName(member: MemberShape): String = member.memberName diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/AwsQuery.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/AwsQuery.kt index c2b5e620da..d749526484 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/AwsQuery.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/AwsQuery.kt @@ -14,7 +14,6 @@ import software.amazon.smithy.model.traits.HttpTrait import software.amazon.smithy.model.traits.TimestampFormatTrait import software.amazon.smithy.rust.codegen.client.rustlang.CargoDependency import software.amazon.smithy.rust.codegen.client.rustlang.RustModule -import software.amazon.smithy.rust.codegen.client.rustlang.Writable import software.amazon.smithy.rust.codegen.client.rustlang.asType import software.amazon.smithy.rust.codegen.client.rustlang.rust import software.amazon.smithy.rust.codegen.client.rustlang.rustBlockTemplate @@ -105,17 +104,4 @@ class AwsQueryProtocol(private val coreCodegenContext: CoreCodegenContext) : Pro rust("#T::parse_generic_error(payload.as_ref())", awsQueryErrors) } } - - override fun serverRouterRequestSpec( - operationShape: OperationShape, - operationName: String, - serviceName: String, - requestSpecModule: RuntimeType, - ): Writable { - TODO("Not yet implemented") - } - - override fun serverRouterRuntimeConstructor(): String { - TODO("Not yet implemented") - } } diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/Ec2Query.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/Ec2Query.kt index 1d87294d89..8af691bfe6 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/Ec2Query.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/Ec2Query.kt @@ -11,7 +11,6 @@ import software.amazon.smithy.model.traits.HttpTrait import software.amazon.smithy.model.traits.TimestampFormatTrait import software.amazon.smithy.rust.codegen.client.rustlang.CargoDependency import software.amazon.smithy.rust.codegen.client.rustlang.RustModule -import software.amazon.smithy.rust.codegen.client.rustlang.Writable import software.amazon.smithy.rust.codegen.client.rustlang.asType import software.amazon.smithy.rust.codegen.client.rustlang.rust import software.amazon.smithy.rust.codegen.client.rustlang.rustBlockTemplate @@ -96,17 +95,4 @@ class Ec2QueryProtocol(private val coreCodegenContext: CoreCodegenContext) : Pro rust("#T::parse_generic_error(payload.as_ref())", ec2QueryErrors) } } - - override fun serverRouterRequestSpec( - operationShape: OperationShape, - operationName: String, - serviceName: String, - requestSpecModule: RuntimeType, - ): Writable { - TODO("Not yet implemented") - } - - override fun serverRouterRuntimeConstructor(): String { - TODO("Not yet implemented") - } } diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/Protocol.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/Protocol.kt index cb87ac31c9..1be6d8ec11 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/Protocol.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/Protocol.kt @@ -20,7 +20,6 @@ import software.amazon.smithy.model.shapes.ShapeId import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.model.traits.TimestampFormatTrait import software.amazon.smithy.model.traits.Trait -import software.amazon.smithy.rust.codegen.client.rustlang.Writable import software.amazon.smithy.rust.codegen.client.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.client.smithy.RuntimeType import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ProtocolGenerator @@ -74,29 +73,6 @@ interface Protocol { * there are no response headers or statuses available to further inform the error parsing. */ fun parseEventStreamGenericError(operationShape: OperationShape): RuntimeType - - /** - * Returns a writable for the `RequestSpec` for an operation. - */ - fun serverRouterRequestSpec( - operationShape: OperationShape, - operationName: String, - serviceName: String, - requestSpecModule: RuntimeType, - ): Writable - - /** - * Returns the name of the constructor to be used on the `Router` type, to instantiate a `Router` using this - * protocol. - */ - fun serverRouterRuntimeConstructor(): String - - /** - * In some protocols, such as restJson1, - * when there is no modeled body input, content type must not be set and the body must be empty. - * Returns a boolean indicating whether to perform this check. - */ - fun serverContentTypeCheckNoModeledInput(): Boolean = false } typealias ProtocolMap = Map> diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestJson.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestJson.kt index 83e569464d..a71c2277ff 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestJson.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestJson.kt @@ -16,13 +16,11 @@ import software.amazon.smithy.model.traits.StreamingTrait import software.amazon.smithy.model.traits.TimestampFormatTrait import software.amazon.smithy.rust.codegen.client.rustlang.CargoDependency import software.amazon.smithy.rust.codegen.client.rustlang.RustModule -import software.amazon.smithy.rust.codegen.client.rustlang.Writable import software.amazon.smithy.rust.codegen.client.rustlang.asType import software.amazon.smithy.rust.codegen.client.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext import software.amazon.smithy.rust.codegen.client.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.client.smithy.RuntimeType -import software.amazon.smithy.rust.codegen.client.smithy.generators.http.RestRequestSpecGenerator import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ProtocolSupport import software.amazon.smithy.rust.codegen.client.smithy.protocols.parse.JsonParserGenerator import software.amazon.smithy.rust.codegen.client.smithy.protocols.parse.StructuredDataParserGenerator @@ -141,17 +139,6 @@ open class RestJson(val coreCodegenContext: CoreCodegenContext) : Protocol { *errorScope, ) } - - override fun serverRouterRequestSpec( - operationShape: OperationShape, - operationName: String, - serviceName: String, - requestSpecModule: RuntimeType, - ): Writable = RestRequestSpecGenerator(httpBindingResolver, requestSpecModule).generate(operationShape) - - override fun serverRouterRuntimeConstructor() = "new_rest_json_router" - - override fun serverContentTypeCheckNoModeledInput() = true } fun restJsonFieldName(member: MemberShape): String { diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestXml.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestXml.kt index 038f49ff24..f2cc52023e 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestXml.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/protocols/RestXml.kt @@ -13,14 +13,12 @@ import software.amazon.smithy.model.traits.AnnotationTrait import software.amazon.smithy.model.traits.TimestampFormatTrait import software.amazon.smithy.rust.codegen.client.rustlang.CargoDependency import software.amazon.smithy.rust.codegen.client.rustlang.RustModule -import software.amazon.smithy.rust.codegen.client.rustlang.Writable import software.amazon.smithy.rust.codegen.client.rustlang.asType import software.amazon.smithy.rust.codegen.client.rustlang.rust import software.amazon.smithy.rust.codegen.client.rustlang.rustBlockTemplate import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext import software.amazon.smithy.rust.codegen.client.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.client.smithy.RuntimeType -import software.amazon.smithy.rust.codegen.client.smithy.generators.http.RestRequestSpecGenerator import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ProtocolSupport import software.amazon.smithy.rust.codegen.client.smithy.protocols.parse.RestXmlParserGenerator import software.amazon.smithy.rust.codegen.client.smithy.protocols.parse.StructuredDataParserGenerator @@ -103,17 +101,6 @@ open class RestXml(val coreCodegenContext: CoreCodegenContext) : Protocol { rust("#T::parse_generic_error(payload.as_ref())", restXmlErrors) } } - - override fun serverRouterRequestSpec( - operationShape: OperationShape, - operationName: String, - serviceName: String, - requestSpecModule: RuntimeType, - ): Writable = RestRequestSpecGenerator(httpBindingResolver, requestSpecModule).generate(operationShape) - - override fun serverRouterRuntimeConstructor() = "new_rest_xml_router" - - override fun serverContentTypeCheckNoModeledInput() = true } /** diff --git a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt index de0b3d920e..1f894ea4ad 100644 --- a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt +++ b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/PythonServerCodegenVisitor.kt @@ -26,6 +26,7 @@ import software.amazon.smithy.rust.codegen.server.python.smithy.generators.Pytho import software.amazon.smithy.rust.codegen.server.python.smithy.generators.PythonServerServiceGenerator import software.amazon.smithy.rust.codegen.server.python.smithy.generators.PythonServerStructureGenerator import software.amazon.smithy.rust.codegen.server.smithy.ServerCodegenVisitor +import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol import software.amazon.smithy.rust.codegen.server.smithy.protocols.ServerProtocolLoader /** @@ -136,7 +137,7 @@ class PythonServerCodegenVisitor( rustCrate, protocolGenerator, protocolGeneratorFactory.support(), - protocolGeneratorFactory.protocol(codegenContext), + ServerProtocol.fromCoreProtocol(protocolGeneratorFactory.protocol(codegenContext)), codegenContext, ) .render() diff --git a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerServiceGenerator.kt b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerServiceGenerator.kt index 9e38c48bab..58c87effbe 100644 --- a/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerServiceGenerator.kt +++ b/codegen-server/python/src/main/kotlin/software/amazon/smithy/rust/codegen/server/python/smithy/generators/PythonServerServiceGenerator.kt @@ -12,8 +12,8 @@ import software.amazon.smithy.rust.codegen.client.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.client.smithy.RustCrate import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ProtocolGenerator import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ProtocolSupport -import software.amazon.smithy.rust.codegen.client.smithy.protocols.Protocol import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerServiceGenerator +import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol /** * PythonServerServiceGenerator @@ -25,7 +25,7 @@ class PythonServerServiceGenerator( private val rustCrate: RustCrate, protocolGenerator: ProtocolGenerator, protocolSupport: ProtocolSupport, - protocol: Protocol, + protocol: ServerProtocol, private val context: CoreCodegenContext, ) : ServerServiceGenerator(rustCrate, protocolGenerator, protocolSupport, protocol, context) { diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt index 856ee0fe9c..921ce79aae 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/ServerCodegenVisitor.kt @@ -40,6 +40,7 @@ import software.amazon.smithy.rust.codegen.client.util.getTrait import software.amazon.smithy.rust.codegen.client.util.runCommand import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerEnumGenerator import software.amazon.smithy.rust.codegen.server.smithy.generators.ServerServiceGenerator +import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol import software.amazon.smithy.rust.codegen.server.smithy.protocols.ServerProtocolLoader import java.util.logging.Logger @@ -229,7 +230,7 @@ open class ServerCodegenVisitor( rustCrate, protocolGenerator, protocolGeneratorFactory.support(), - protocolGeneratorFactory.protocol(codegenContext), + ServerProtocol.fromCoreProtocol(protocolGeneratorFactory.protocol(codegenContext)), codegenContext, ) .render() diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationRegistryGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationRegistryGenerator.kt index 3e7126a2ab..aa5f25e3c9 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationRegistryGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerOperationRegistryGenerator.kt @@ -28,13 +28,13 @@ import software.amazon.smithy.rust.codegen.client.smithy.Outputs import software.amazon.smithy.rust.codegen.client.smithy.RuntimeType import software.amazon.smithy.rust.codegen.client.smithy.generators.CodegenTarget import software.amazon.smithy.rust.codegen.client.smithy.generators.error.errorSymbol -import software.amazon.smithy.rust.codegen.client.smithy.protocols.Protocol import software.amazon.smithy.rust.codegen.client.util.getTrait import software.amazon.smithy.rust.codegen.client.util.inputShape import software.amazon.smithy.rust.codegen.client.util.outputShape import software.amazon.smithy.rust.codegen.client.util.toSnakeCase import software.amazon.smithy.rust.codegen.server.smithy.ServerCargoDependency import software.amazon.smithy.rust.codegen.server.smithy.ServerRuntimeType +import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol /** * [ServerOperationRegistryGenerator] renders the `OperationRegistry` struct, a place where users can register their @@ -49,7 +49,7 @@ import software.amazon.smithy.rust.codegen.server.smithy.ServerRuntimeType */ class ServerOperationRegistryGenerator( private val coreCodegenContext: CoreCodegenContext, - private val protocol: Protocol, + private val protocol: ServerProtocol, private val operations: List, ) { private val crateName = coreCodegenContext.settings.moduleName diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGenerator.kt index 2a28b8fca3..99c59ff51b 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/ServerServiceGenerator.kt @@ -17,7 +17,6 @@ import software.amazon.smithy.rust.codegen.client.smithy.DefaultPublicModules import software.amazon.smithy.rust.codegen.client.smithy.RustCrate import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ProtocolGenerator import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ProtocolSupport -import software.amazon.smithy.rust.codegen.client.smithy.protocols.Protocol import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocolTestGenerator @@ -31,7 +30,7 @@ open class ServerServiceGenerator( private val rustCrate: RustCrate, private val protocolGenerator: ProtocolGenerator, private val protocolSupport: ProtocolSupport, - private val protocol: Protocol, + private val protocol: ServerProtocol, private val coreCodegenContext: CoreCodegenContext, ) { private val index = TopDownIndex.of(coreCodegenContext.model) diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocol.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocol.kt index bf879d8f34..bdd23431f6 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocol.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/generators/protocol/ServerProtocol.kt @@ -9,18 +9,22 @@ import software.amazon.smithy.model.knowledge.TopDownIndex import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.rust.codegen.client.rustlang.Writable import software.amazon.smithy.rust.codegen.client.rustlang.asType +import software.amazon.smithy.rust.codegen.client.rustlang.rust import software.amazon.smithy.rust.codegen.client.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.client.rustlang.writable import software.amazon.smithy.rust.codegen.client.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.client.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.client.smithy.RuntimeType +import software.amazon.smithy.rust.codegen.client.smithy.generators.http.RestRequestSpecGenerator import software.amazon.smithy.rust.codegen.client.smithy.protocols.AwsJson import software.amazon.smithy.rust.codegen.client.smithy.protocols.AwsJsonVersion import software.amazon.smithy.rust.codegen.client.smithy.protocols.Protocol import software.amazon.smithy.rust.codegen.client.smithy.protocols.RestJson import software.amazon.smithy.rust.codegen.client.smithy.protocols.RestXml +import software.amazon.smithy.rust.codegen.client.smithy.protocols.serialize.StructuredDataSerializerGenerator import software.amazon.smithy.rust.codegen.server.smithy.ServerCargoDependency import software.amazon.smithy.rust.codegen.server.smithy.ServerRuntimeType +import software.amazon.smithy.rust.codegen.server.smithy.protocols.ServerAwsJsonSerializerGenerator private fun allOperations(coreCodegenContext: CoreCodegenContext): List { val index = TopDownIndex.of(coreCodegenContext.model) @@ -40,6 +44,29 @@ interface ServerProtocol : Protocol { */ fun routerConstruction(operationValues: Iterable): Writable + /** + * Returns the name of the constructor to be used on the `Router` type, to instantiate a `Router` using this + * protocol. + */ + fun serverRouterRuntimeConstructor(): String + + /** + * Returns a writable for the `RequestSpec` for an operation. + */ + fun serverRouterRequestSpec( + operationShape: OperationShape, + operationName: String, + serviceName: String, + requestSpecModule: RuntimeType, + ): Writable + + /** + * In some protocols, such as restJson1, + * when there is no modeled body input, content type must not be set and the body must be empty. + * Returns a boolean indicating whether to perform this check. + */ + fun serverContentTypeCheckNoModeledInput(): Boolean = false + companion object { /** Upgrades the core protocol to a `ServerProtocol`. */ fun fromCoreProtocol(protocol: Protocol): ServerProtocol = when (protocol) { @@ -62,6 +89,9 @@ class ServerAwsJsonProtocol( private val symbolProvider = coreCodegenContext.symbolProvider private val service = coreCodegenContext.serviceShape + override fun structuredDataSerializer(operationShape: OperationShape): StructuredDataSerializerGenerator = + ServerAwsJsonSerializerGenerator(coreCodegenContext, httpBindingResolver, awsJsonVersion) + companion object { fun fromCoreProtocol(awsJson: AwsJson): ServerAwsJsonProtocol = ServerAwsJsonProtocol(awsJson.coreCodegenContext, awsJson.version) } @@ -110,6 +140,23 @@ class ServerAwsJsonProtocol( "Pairs" to pairs, ) } + + /** + * Returns the operation name as required by the awsJson1.x protocols. + */ + override fun serverRouterRequestSpec( + operationShape: OperationShape, + operationName: String, + serviceName: String, + requestSpecModule: RuntimeType, + ) = writable { + rust("""String::from("$serviceName.$operationName")""") + } + + override fun serverRouterRuntimeConstructor() = when (version) { + AwsJsonVersion.Json10 -> "new_aws_json_10_router" + AwsJsonVersion.Json11 -> "new_aws_json_11_router" + } } private fun restRouterType(runtimeConfig: RuntimeConfig) = RuntimeType("RestRouter", ServerCargoDependency.SmithyHttpServer(runtimeConfig), "${runtimeConfig.crateSrcPrefix}_http_server::routing::routers::rest") @@ -170,6 +217,17 @@ class ServerRestJsonProtocol( override fun routerType() = restRouterType(runtimeConfig) override fun routerConstruction(operationValues: Iterable): Writable = restRouterConstruction(this, operationValues, coreCodegenContext) + + override fun serverRouterRequestSpec( + operationShape: OperationShape, + operationName: String, + serviceName: String, + requestSpecModule: RuntimeType, + ): Writable = RestRequestSpecGenerator(httpBindingResolver, requestSpecModule).generate(operationShape) + + override fun serverRouterRuntimeConstructor() = "new_rest_json_router" + + override fun serverContentTypeCheckNoModeledInput() = true } class ServerRestXmlProtocol( @@ -188,4 +246,15 @@ class ServerRestXmlProtocol( override fun routerType() = restRouterType(runtimeConfig) override fun routerConstruction(operationValues: Iterable): Writable = restRouterConstruction(this, operationValues, coreCodegenContext) + + override fun serverRouterRequestSpec( + operationShape: OperationShape, + operationName: String, + serviceName: String, + requestSpecModule: RuntimeType, + ): Writable = RestRequestSpecGenerator(httpBindingResolver, requestSpecModule).generate(operationShape) + + override fun serverRouterRuntimeConstructor() = "new_rest_xml_router" + + override fun serverContentTypeCheckNoModeledInput() = true } diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerAwsJson.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerAwsJson.kt index a272ae7828..b1b4d86f0d 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerAwsJson.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerAwsJson.kt @@ -5,7 +5,6 @@ package software.amazon.smithy.rust.codegen.server.smithy.protocols -import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.model.traits.ErrorTrait import software.amazon.smithy.rust.codegen.client.rustlang.Writable import software.amazon.smithy.rust.codegen.client.rustlang.escape @@ -14,10 +13,8 @@ import software.amazon.smithy.rust.codegen.client.rustlang.writable import software.amazon.smithy.rust.codegen.client.smithy.CoreCodegenContext import software.amazon.smithy.rust.codegen.client.smithy.ServerCodegenContext import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.ProtocolSupport -import software.amazon.smithy.rust.codegen.client.smithy.protocols.AwsJson import software.amazon.smithy.rust.codegen.client.smithy.protocols.AwsJsonVersion import software.amazon.smithy.rust.codegen.client.smithy.protocols.HttpBindingResolver -import software.amazon.smithy.rust.codegen.client.smithy.protocols.Protocol import software.amazon.smithy.rust.codegen.client.smithy.protocols.ProtocolGeneratorFactory import software.amazon.smithy.rust.codegen.client.smithy.protocols.awsJsonFieldName import software.amazon.smithy.rust.codegen.client.smithy.protocols.serialize.JsonCustomization @@ -25,6 +22,8 @@ import software.amazon.smithy.rust.codegen.client.smithy.protocols.serialize.Jso import software.amazon.smithy.rust.codegen.client.smithy.protocols.serialize.JsonSerializerGenerator import software.amazon.smithy.rust.codegen.client.smithy.protocols.serialize.StructuredDataSerializerGenerator import software.amazon.smithy.rust.codegen.client.util.hasTrait +import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerAwsJsonProtocol +import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerProtocol /** * AwsJson 1.0 and 1.1 server-side protocol factory. This factory creates the [ServerHttpBoundProtocolGenerator] @@ -32,7 +31,7 @@ import software.amazon.smithy.rust.codegen.client.util.hasTrait */ class ServerAwsJsonFactory(private val version: AwsJsonVersion) : ProtocolGeneratorFactory { - override fun protocol(codegenContext: ServerCodegenContext): Protocol = ServerAwsJson(codegenContext, version) + override fun protocol(codegenContext: ServerCodegenContext): ServerProtocol = ServerAwsJsonProtocol(codegenContext, version) override fun buildProtocolGenerator(codegenContext: ServerCodegenContext): ServerHttpBoundProtocolGenerator = ServerHttpBoundProtocolGenerator(codegenContext, protocol(codegenContext)) @@ -93,11 +92,3 @@ class ServerAwsJsonSerializerGenerator( customizations = listOf(ServerAwsJsonError(awsJsonVersion)), ), ) : StructuredDataSerializerGenerator by jsonSerializerGenerator - -class ServerAwsJson( - coreCodegenContext: CoreCodegenContext, - private val awsJsonVersion: AwsJsonVersion, -) : AwsJson(coreCodegenContext, awsJsonVersion) { - override fun structuredDataSerializer(operationShape: OperationShape): StructuredDataSerializerGenerator = - ServerAwsJsonSerializerGenerator(coreCodegenContext, httpBindingResolver, awsJsonVersion) -} diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpBoundProtocolGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpBoundProtocolGenerator.kt index f8c8aeccdb..391441d791 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpBoundProtocolGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpBoundProtocolGenerator.kt @@ -58,7 +58,6 @@ import software.amazon.smithy.rust.codegen.client.smithy.isOptional import software.amazon.smithy.rust.codegen.client.smithy.protocols.HttpBindingDescriptor import software.amazon.smithy.rust.codegen.client.smithy.protocols.HttpBoundProtocolPayloadGenerator import software.amazon.smithy.rust.codegen.client.smithy.protocols.HttpLocation -import software.amazon.smithy.rust.codegen.client.smithy.protocols.Protocol import software.amazon.smithy.rust.codegen.client.smithy.protocols.parse.StructuredDataParserGenerator import software.amazon.smithy.rust.codegen.client.smithy.toOptional import software.amazon.smithy.rust.codegen.client.smithy.traits.SyntheticInputTrait @@ -89,7 +88,7 @@ import java.util.logging.Logger */ class ServerHttpBoundProtocolGenerator( codegenContext: ServerCodegenContext, - protocol: Protocol, + protocol: ServerProtocol, ) : ProtocolGenerator( codegenContext, protocol, @@ -115,7 +114,7 @@ class ServerHttpBoundProtocolGenerator( */ private class ServerHttpBoundProtocolTraitImplGenerator( private val codegenContext: ServerCodegenContext, - private val protocol: Protocol, + private val protocol: ServerProtocol, ) : ProtocolTraitImplGenerator { private val logger = Logger.getLogger(javaClass.name) private val symbolProvider = codegenContext.symbolProvider diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerRestJsonFactory.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerRestJsonFactory.kt index 2798ce2303..88b299a643 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerRestJsonFactory.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerRestJsonFactory.kt @@ -10,6 +10,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.generators.protocol.Pro import software.amazon.smithy.rust.codegen.client.smithy.protocols.Protocol import software.amazon.smithy.rust.codegen.client.smithy.protocols.ProtocolGeneratorFactory import software.amazon.smithy.rust.codegen.client.smithy.protocols.RestJson +import software.amazon.smithy.rust.codegen.server.smithy.generators.protocol.ServerRestJsonProtocol /** * RestJson1 server-side protocol factory. This factory creates the [ServerHttpProtocolGenerator] @@ -19,7 +20,7 @@ class ServerRestJsonFactory : ProtocolGeneratorFactory