diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/Ec2QuerySerializerGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/Ec2QuerySerializerGenerator.kt index ed3a83c667..e3514c2aba 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/Ec2QuerySerializerGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/Ec2QuerySerializerGenerator.kt @@ -6,10 +6,13 @@ package software.amazon.smithy.rust.codegen.core.smithy.protocols.serialize import software.amazon.smithy.aws.traits.protocols.Ec2QueryNameTrait +import software.amazon.smithy.model.shapes.CollectionShape import software.amazon.smithy.model.shapes.MemberShape import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.model.shapes.ShapeId import software.amazon.smithy.model.traits.XmlNameTrait +import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter +import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock import software.amazon.smithy.rust.codegen.core.smithy.CodegenContext import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType import software.amazon.smithy.rust.codegen.core.util.getTrait @@ -32,4 +35,13 @@ class Ec2QuerySerializerGenerator(codegenContext: CodegenContext) : QuerySeriali override fun serverErrorSerializer(shape: ShapeId): RuntimeType { TODO("Not yet implemented") } + + override fun RustWriter.serializeCollection( + memberContext: MemberContext, + context: Context, + ) { + rustBlock("if !${context.valueExpression.asRef()}.is_empty()") { + super.serializeCollectionInner(memberContext, context, this) + } + } } diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/QuerySerializerGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/QuerySerializerGenerator.kt index b47e5dd98c..a38581e6b6 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/QuerySerializerGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/QuerySerializerGenerator.kt @@ -282,30 +282,40 @@ abstract class QuerySerializerGenerator(private val codegenContext: CodegenConte } } - private fun RustWriter.serializeCollection( + protected open fun RustWriter.serializeCollection( memberContext: MemberContext, context: Context, ) { - val flat = memberContext.shape.isFlattened() - val memberOverride = - when (val override = context.shape.member.getTrait()?.value) { - null -> "None" - else -> "Some(${override.dq()})" - } - val itemName = safeName("item") - safeName("list").also { listName -> - rust("let mut $listName = ${context.writerExpression}.start_list($flat, $memberOverride);") - rustBlock("for $itemName in ${context.valueExpression.asRef()}") { - val entryName = safeName("entry") - Attribute.AllowUnusedMut.render(this) - rust("let mut $entryName = $listName.entry();") - val targetShape = model.expectShape(context.shape.member.target) - serializeMemberValue( - MemberContext(entryName, ValueExpression.Reference(itemName), context.shape.member), - targetShape, - ) + serializeCollectionInner(memberContext, context, this) + } + + protected fun serializeCollectionInner( + memberContext: MemberContext, + context: Context, + writer: RustWriter, + ) { + writer.apply { + val flat = memberContext.shape.isFlattened() + val memberOverride = + when (val override = context.shape.member.getTrait()?.value) { + null -> "None" + else -> "Some(${override.dq()})" + } + val itemName = safeName("item") + safeName("list").also { listName -> + rust("let mut $listName = ${context.writerExpression}.start_list($flat, $memberOverride);") + rustBlock("for $itemName in ${context.valueExpression.asRef()}") { + val entryName = safeName("entry") + Attribute.AllowUnusedMut.render(this) + rust("let mut $entryName = $listName.entry();") + val targetShape = model.expectShape(context.shape.member.target) + serializeMemberValue( + MemberContext(entryName, ValueExpression.Reference(itemName), context.shape.member), + targetShape, + ) + } + rust("$listName.finish();") } - rust("$listName.finish();") } } diff --git a/gradle.properties b/gradle.properties index e33333a351..6d06ec08dc 100644 --- a/gradle.properties +++ b/gradle.properties @@ -21,7 +21,7 @@ kotlin.code.style=official # codegen smithyGradlePluginVersion=0.9.0 -smithyVersion=1.47.0 +smithyVersion=1.49.0 allowLocalDeps=false # kotlin