Skip to content

Commit

Permalink
Serialize zero values
Browse files Browse the repository at this point in the history
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](#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.
  • Loading branch information
milesziemer committed Nov 21, 2023
1 parent 26fc3fa commit 65d2022
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<ClientOptionalTrait>()) {
Default.NoDefault
} else if (target.isDocumentShape || target.isTimestampShape) {
Default.NonZeroDefault(trait.toNode())
} else {
when (val value = trait.toNode()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -378,10 +377,8 @@ class JsonSerializerGenerator(
)
}

with(serializerUtil) {
ignoreZeroValues(context.shape, context.valueExpression) {
serializeMemberValue(context, targetShape)
}
rustBlock("") {
serializeMemberValue(context, targetShape)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class XmlBindingTraitSerializerGenerator(

private val xmlIndex = XmlNameIndex.of(model)
private val rootNamespace = codegenContext.serviceShape.getTrait<XmlNamespaceTrait>()
private val util = SerializerUtil(model)

sealed class Ctx {
abstract val input: String
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions rust-runtime/aws-smithy-types/src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down

0 comments on commit 65d2022

Please sign in to comment.