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..311b84889f 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..01ce7bb4d7 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,9 +282,17 @@ abstract class QuerySerializerGenerator(private val codegenContext: CodegenConte } } - private fun RustWriter.serializeCollection( + protected open fun RustWriter.serializeCollection( memberContext: MemberContext, context: Context, + ) { + serializeCollectionInner(memberContext, context, this) + } + + protected fun serializeCollectionInner( + memberContext: MemberContext, + context: Context, + writer: RustWriter, ) { val flat = memberContext.shape.isFlattened() val memberOverride = @@ -292,20 +300,20 @@ abstract class QuerySerializerGenerator(private val codegenContext: CodegenConte 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 itemName = writer.safeName("item") + writer.safeName("list").also { listName -> + writer.rust("let mut $listName = ${context.writerExpression}.start_list($flat, $memberOverride);") + writer.rustBlock("for $itemName in ${context.valueExpression.asRef()}") { val entryName = safeName("entry") Attribute.AllowUnusedMut.render(this) - rust("let mut $entryName = $listName.entry();") + writer.rust("let mut $entryName = $listName.entry();") val targetShape = model.expectShape(context.shape.member.target) - serializeMemberValue( + writer.serializeMemberValue( MemberContext(entryName, ValueExpression.Reference(itemName), context.shape.member), targetShape, ) } - rust("$listName.finish();") + writer.rust("$listName.finish();") } }