From f97ebf2d5b47924126ec4c3bedb403831f399ed3 Mon Sep 17 00:00:00 2001 From: Guy Margalit Date: Tue, 25 Jan 2022 18:54:36 +0200 Subject: [PATCH] fix #1101 server XML response use correct members and return null if nothing to serialize (#1098) Signed-off-by: Guy Margalit --- .../protocols/ServerHttpProtocolGenerator.kt | 4 ++-- .../codegen/smithy/protocols/XmlNameIndex.kt | 3 ++- .../serialize/AwsQuerySerializerGenerator.kt | 2 +- .../serialize/Ec2QuerySerializerGenerator.kt | 2 +- .../serialize/JsonSerializerGenerator.kt | 2 +- .../StructuredDataSerializerGenerator.kt | 2 +- .../XmlBindingTraitSerializerGenerator.kt | 16 +++++++++++----- 7 files changed, 19 insertions(+), 12 deletions(-) diff --git a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpProtocolGenerator.kt b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpProtocolGenerator.kt index 495662449..da31f42c7 100644 --- a/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpProtocolGenerator.kt +++ b/codegen-server/src/main/kotlin/software/amazon/smithy/rust/codegen/server/smithy/protocols/ServerHttpProtocolGenerator.kt @@ -451,12 +451,12 @@ private class ServerHttpProtocolImplGenerator( bindings: List, ) { val structuredDataSerializer = protocol.structuredDataSerializer(operationShape) - structuredDataSerializer.serverOutputSerializer(operationShape).also { serializer -> + structuredDataSerializer.serverOutputSerializer(operationShape)?.let { serializer -> rust( "let payload = #T(output)?;", serializer ) - } + } ?: rust("""let payload = "";""") // avoid non-usage warnings for response Attribute.AllowUnusedMut.render(this) rustTemplate("let mut response = #{http}::Response::builder();", *codegenScope) diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/XmlNameIndex.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/XmlNameIndex.kt index d837d9a59..767daab42 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/XmlNameIndex.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/XmlNameIndex.kt @@ -63,5 +63,6 @@ data class XmlMemberIndex(val dataMembers: List, val attributeMembe } } - fun isNotEmpty() = dataMembers.isNotEmpty() || attributeMembers.isNotEmpty() + fun isEmpty() = dataMembers.isEmpty() && attributeMembers.isEmpty() + fun isNotEmpty() = !isEmpty() } diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/AwsQuerySerializerGenerator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/AwsQuerySerializerGenerator.kt index 6f8cf6fcc..75385d3e3 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/AwsQuerySerializerGenerator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/AwsQuerySerializerGenerator.kt @@ -22,7 +22,7 @@ class AwsQuerySerializerGenerator(codegenContext: CodegenContext) : QuerySeriali override fun MemberShape.isFlattened(): Boolean = getTrait() != null - override fun serverOutputSerializer(operationShape: OperationShape): RuntimeType { + override fun serverOutputSerializer(operationShape: OperationShape): RuntimeType? { TODO("Not yet implemented") } diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/Ec2QuerySerializerGenerator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/Ec2QuerySerializerGenerator.kt index cfcd4fde4..3a1e3af79 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/Ec2QuerySerializerGenerator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/Ec2QuerySerializerGenerator.kt @@ -25,7 +25,7 @@ class Ec2QuerySerializerGenerator(codegenContext: CodegenContext) : QuerySeriali override fun MemberShape.isFlattened(): Boolean = true - override fun serverOutputSerializer(operationShape: OperationShape): RuntimeType { + override fun serverOutputSerializer(operationShape: OperationShape): RuntimeType? { TODO("Not yet implemented") } diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/JsonSerializerGenerator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/JsonSerializerGenerator.kt index 57466d2fc..f97144802 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/JsonSerializerGenerator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/JsonSerializerGenerator.kt @@ -242,7 +242,7 @@ class JsonSerializerGenerator( } } - override fun serverOutputSerializer(operationShape: OperationShape): RuntimeType { + override fun serverOutputSerializer(operationShape: OperationShape): RuntimeType? { val outputShape = operationShape.outputShape(model) val includedMembers = httpBindingResolver.responseMembers(operationShape, HttpLocation.DOCUMENT) val fnName = symbolProvider.serializeFunctionName(outputShape) diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/StructuredDataSerializerGenerator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/StructuredDataSerializerGenerator.kt index 0a4d1d0ee..117a950b5 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/StructuredDataSerializerGenerator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/StructuredDataSerializerGenerator.kt @@ -55,7 +55,7 @@ interface StructuredDataSerializerGenerator { * } * ``` */ - fun serverOutputSerializer(operationShape: OperationShape): RuntimeType + fun serverOutputSerializer(operationShape: OperationShape): RuntimeType? /** * Generate a serializer for a server operation error structure diff --git a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/XmlBindingTraitSerializerGenerator.kt b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/XmlBindingTraitSerializerGenerator.kt index 6ac9ea9ea..66b697c9e 100644 --- a/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/XmlBindingTraitSerializerGenerator.kt +++ b/codegen/src/main/kotlin/software/amazon/smithy/rust/codegen/smithy/protocols/serialize/XmlBindingTraitSerializerGenerator.kt @@ -106,8 +106,8 @@ class XmlBindingTraitSerializerGenerator( override fun operationSerializer(operationShape: OperationShape): RuntimeType? { val fnName = symbolProvider.serializeFunctionName(operationShape) val inputShape = operationShape.inputShape(model) - val xmlMembers = operationShape.operationXmlMembers() - if (!xmlMembers.isNotEmpty()) { + val xmlMembers = operationShape.requestBodyMembers() + if (xmlMembers.isEmpty()) { return null } val operationXmlName = xmlIndex.operationInputShapeName(operationShape) @@ -191,10 +191,13 @@ class XmlBindingTraitSerializerGenerator( } } - override fun serverOutputSerializer(operationShape: OperationShape): RuntimeType { + override fun serverOutputSerializer(operationShape: OperationShape): RuntimeType? { val fnName = symbolProvider.serializeFunctionName(operationShape) val outputShape = operationShape.outputShape(model) - val xmlMembers = operationShape.operationXmlMembers() + val xmlMembers = operationShape.responseBodyMembers() + if (xmlMembers.isEmpty()) { + return null + } val operationXmlName = xmlIndex.operationOutputShapeName(operationShape) ?: throw CodegenException("operation must have a name if it has members") return RuntimeType.forInlineFun(fnName, operationSerModule) { @@ -466,9 +469,12 @@ class XmlBindingTraitSerializerGenerator( } } - private fun OperationShape.operationXmlMembers(): XmlMemberIndex = + private fun OperationShape.requestBodyMembers(): XmlMemberIndex = XmlMemberIndex.fromMembers(httpBindingResolver.requestMembers(this, HttpLocation.DOCUMENT)) + private fun OperationShape.responseBodyMembers(): XmlMemberIndex = + XmlMemberIndex.fromMembers(httpBindingResolver.responseMembers(this, HttpLocation.DOCUMENT)) + private fun Shape.xmlNamespace(root: Boolean): XmlNamespaceTrait? { return this.getTrait().letIf(root) { it ?: rootNamespace } } -- GitLab