From 39ac99fed5407777ead359e446f972032488196e Mon Sep 17 00:00:00 2001 From: Miles Ziemer Date: Mon, 20 Nov 2023 18:42:18 -0500 Subject: [PATCH] Serialize zero values Removes logic from RustWriter that was used to not serialize zero values, ie. 0 for numbers and false for booleans, if that was also the default value for the member. With this change, the only time the default value for a member is not serialized is if the member is also `@clientOptional`. The Blob type was also updated to implement std::Default, since SymbolVisitor treats `""` as a RustDefault. It _seems_ like this logic was originally added to fix issues with [S3](https://github.com/smithy-lang/smithy-rs/issues/102) which are technically modeling issues and have since been fixed. However, this change impacts all the protocols and will cause many members to now be sent over the wire that previously weren't. --- .../rust/codegen/core/smithy/SymbolVisitor.kt | 5 ++- .../serialize/JsonSerializerGenerator.kt | 7 ++--- .../serialize/QuerySerializerGenerator.kt | 7 ++--- .../protocols/serialize/SerializerUtil.kt | 31 ------------------- .../XmlBindingTraitSerializerGenerator.kt | 12 ++----- rust-runtime/aws-smithy-types/src/blob.rs | 8 +++++ 6 files changed, 18 insertions(+), 52 deletions(-) delete mode 100644 codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/SerializerUtil.kt diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/SymbolVisitor.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/SymbolVisitor.kt index af74c80d9f..f3cca65c92 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/SymbolVisitor.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/SymbolVisitor.kt @@ -38,6 +38,7 @@ import software.amazon.smithy.model.shapes.StringShape import software.amazon.smithy.model.shapes.StructureShape import software.amazon.smithy.model.shapes.TimestampShape import software.amazon.smithy.model.shapes.UnionShape +import software.amazon.smithy.model.traits.ClientOptionalTrait import software.amazon.smithy.model.traits.DefaultTrait import software.amazon.smithy.model.traits.EnumTrait import software.amazon.smithy.model.traits.ErrorTrait @@ -269,7 +270,9 @@ open class SymbolVisitor( override fun memberShape(shape: MemberShape): Symbol { val target = model.expectShape(shape.target) val defaultValue = shape.getMemberTrait(model, DefaultTrait::class.java).orNull()?.let { trait -> - if (target.isDocumentShape || target.isTimestampShape) { + if (shape.hasTrait()) { + Default.NoDefault + } else if (target.isDocumentShape || target.isTimestampShape) { Default.NonZeroDefault(trait.toNode()) } else { when (val value = trait.toNode()) { diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/JsonSerializerGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/JsonSerializerGenerator.kt index 981706b566..609d32c3f8 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/JsonSerializerGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/JsonSerializerGenerator.kt @@ -178,7 +178,6 @@ class JsonSerializerGenerator( "JsonValueWriter" to RuntimeType.smithyJson(runtimeConfig).resolve("serialize::JsonValueWriter"), "ByteSlab" to RuntimeType.ByteSlab, ) - private val serializerUtil = SerializerUtil(model) /** * Reusable structure serializer implementation that can be used to generate serializing code for @@ -378,10 +377,8 @@ class JsonSerializerGenerator( ) } - with(serializerUtil) { - ignoreZeroValues(context.shape, context.valueExpression) { - serializeMemberValue(context, targetShape) - } + rustBlock("") { + serializeMemberValue(context, targetShape) } } } 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 23c8bbb4fb..14c313f674 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 @@ -96,7 +96,6 @@ abstract class QuerySerializerGenerator(private val codegenContext: CodegenConte private val serializerError = runtimeConfig.serializationError() private val smithyTypes = RuntimeType.smithyTypes(runtimeConfig) private val smithyQuery = RuntimeType.smithyQuery(runtimeConfig) - private val serdeUtil = SerializerUtil(model) private val protocolFunctions = ProtocolFunctions(codegenContext) private val codegenScope = arrayOf( "String" to RuntimeType.String, @@ -208,10 +207,8 @@ abstract class QuerySerializerGenerator(private val codegenContext: CodegenConte } } } else { - with(serdeUtil) { - ignoreZeroValues(context.shape, context.valueExpression) { - serializeMemberValue(context, targetShape) - } + rustBlock("") { + serializeMemberValue(context, targetShape) } } } diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/SerializerUtil.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/SerializerUtil.kt deleted file mode 100644 index 27293e4303..0000000000 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/SerializerUtil.kt +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -package software.amazon.smithy.rust.codegen.core.smithy.protocols.serialize - -import software.amazon.smithy.model.Model -import software.amazon.smithy.model.shapes.MemberShape -import software.amazon.smithy.model.shapes.StructureShape -import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter -import software.amazon.smithy.rust.codegen.core.rustlang.Writable -import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock - -class SerializerUtil(private val model: Model) { - fun RustWriter.ignoreZeroValues(shape: MemberShape, value: ValueExpression, inner: Writable) { - // Required shapes should always be serialized - // See https://github.com/smithy-lang/smithy-rs/issues/230 and https://github.com/aws/aws-sdk-go-v2/pull/1129 - if ( - shape.isRequired || - // Zero values are always serialized in lists and collections, this only applies to structures - model.expectShape(shape.container) !is StructureShape - ) { - rustBlock("") { - inner(this) - } - } else { - this.ifNotDefault(model.expectShape(shape.target), value) { inner(this) } - } - } -} diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/XmlBindingTraitSerializerGenerator.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/XmlBindingTraitSerializerGenerator.kt index fc4f019838..68e31a533a 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/XmlBindingTraitSerializerGenerator.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/XmlBindingTraitSerializerGenerator.kt @@ -73,7 +73,6 @@ class XmlBindingTraitSerializerGenerator( private val xmlIndex = XmlNameIndex.of(model) private val rootNamespace = codegenContext.serviceShape.getTrait() - private val util = SerializerUtil(model) sealed class Ctx { abstract val input: String @@ -517,15 +516,8 @@ class XmlBindingTraitSerializerGenerator( inner(Ctx.updateInput(ctx, tmp)) } } else { - with(util) { - val valueExpression = if (ctx.input.startsWith("&")) { - ValueExpression.Reference(ctx.input) - } else { - ValueExpression.Value(ctx.input) - } - ignoreZeroValues(member, valueExpression) { - inner(ctx) - } + rustBlock("") { + inner(ctx) } } } diff --git a/rust-runtime/aws-smithy-types/src/blob.rs b/rust-runtime/aws-smithy-types/src/blob.rs index ab7ae30d3c..1087b6d930 100644 --- a/rust-runtime/aws-smithy-types/src/blob.rs +++ b/rust-runtime/aws-smithy-types/src/blob.rs @@ -31,6 +31,14 @@ impl AsRef<[u8]> for Blob { } } +impl Default for Blob { + fn default() -> Self { + Blob { + inner: Vec::new() + } + } +} + #[cfg(all(aws_sdk_unstable, feature = "serde-serialize"))] mod serde_serialize { use super::*;