Skip to content

Commit

Permalink
Ignore actual default value instead of 0/false
Browse files Browse the repository at this point in the history
Previously, there was logic used to ignore the default 0/false values
for numbers/booleans when serializing members if they weren't boxed.
This was to fix issues that occured when upstream models didn't
properly box shapes that were meant to be optional, and for the
most part worked because services would just fill in the default if
it wasn't passed. However, with Smithy 2.0, models may have defaults
!= 0/false, but the codegenerator still ignores the zero value.

This commit makes it so that the actual default value for the member
is ignored for booleans and numbers, instead of just 0/false. It
also updates serialization for http bindings so that headers and
query parameters with 0/false values aren't ignored if they are
optional parameters.
  • Loading branch information
milesziemer committed Nov 24, 2023
1 parent 3d0cb5c commit 71e37cd
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.generators.http.HttpBindi
import software.amazon.smithy.rust.codegen.core.smithy.generators.operationBuildError
import software.amazon.smithy.rust.codegen.core.smithy.isOptional
import software.amazon.smithy.rust.codegen.core.smithy.protocols.Protocol
import software.amazon.smithy.rust.codegen.core.smithy.protocols.serialize.SerializerUtil
import software.amazon.smithy.rust.codegen.core.smithy.protocols.serialize.ValueExpression
import software.amazon.smithy.rust.codegen.core.util.dq
import software.amazon.smithy.rust.codegen.core.util.expectMember
Expand Down Expand Up @@ -70,6 +71,7 @@ class RequestBindingGenerator(
HttpBindingGenerator(protocol, codegenContext, codegenContext.symbolProvider, operationShape)
private val index = HttpBindingIndex.of(model)
private val encoder = RuntimeType.smithyTypes(runtimeConfig).resolve("primitive::Encoder")
private val util = SerializerUtil(model, symbolProvider)

private val codegenScope = arrayOf(
*preludeScope,
Expand Down Expand Up @@ -238,9 +240,15 @@ class RequestBindingGenerator(

paramList(target, derefName, param, writer, memberShape)
} else {
ifSet(target, memberSymbol, ValueExpression.Reference("&_input.$memberName")) { field ->
// if `param` is a list, generate another level of iteration
paramList(target, field.name, param, writer, memberShape)
// If we have an Option<T>, there won't be a default so nothing to ignore. If it's a primitive
// boolean or number, we ignore the default.
ifSome(memberSymbol, ValueExpression.Reference("&_input.$memberName")) { field ->
with(util) {
ignoreDefaultsForNumbersAndBools(memberShape, field) {
// if `param` is a list, generate another level of iteration
paramList(target, field.name, param, writer, memberShape)
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import software.amazon.smithy.codegen.core.SymbolDependencyContainer
import software.amazon.smithy.codegen.core.SymbolWriter
import software.amazon.smithy.codegen.core.SymbolWriter.Factory
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.node.Node
import software.amazon.smithy.model.shapes.BooleanShape
import software.amazon.smithy.model.shapes.CollectionShape
import software.amazon.smithy.model.shapes.DoubleShape
Expand All @@ -24,9 +25,11 @@ import software.amazon.smithy.model.shapes.ShapeId
import software.amazon.smithy.model.traits.DeprecatedTrait
import software.amazon.smithy.model.traits.DocumentationTrait
import software.amazon.smithy.rust.codegen.core.rustlang.Attribute.Companion.deprecated
import software.amazon.smithy.rust.codegen.core.smithy.Default
import software.amazon.smithy.rust.codegen.core.smithy.ModuleDocProvider
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType.Companion.preludeScope
import software.amazon.smithy.rust.codegen.core.smithy.defaultValue
import software.amazon.smithy.rust.codegen.core.smithy.isOptional
import software.amazon.smithy.rust.codegen.core.smithy.protocols.serialize.ValueExpression
import software.amazon.smithy.rust.codegen.core.smithy.rustType
Expand Down Expand Up @@ -678,23 +681,42 @@ class RustWriter private constructor(

/**
* Generate a wrapping if statement around a primitive field.
* The specified block will only be called if the field is not set to its default value - `0` for
* numbers, `false` for booleans.
* If the field is a number or boolean, the specified block is only called if the field is not equal to the
* member's default value.
*/
fun ifNotDefault(shape: Shape, variable: ValueExpression, block: RustWriter.(field: ValueExpression) -> Unit) {
fun ifNotNumberOrBoolDefault(
shape: Shape,
memberSymbol: Symbol,
variable: ValueExpression,
block: RustWriter.(field: ValueExpression) -> Unit,
) {
when (shape) {
is FloatShape, is DoubleShape -> rustBlock("if ${variable.asValue()} != 0.0") {
block(variable)
}

is NumberShape -> rustBlock("if ${variable.asValue()} != 0") {
block(variable)
}

is BooleanShape -> rustBlock("if ${variable.asValue()}") {
block(variable)
is NumberShape, is BooleanShape -> {
if (memberSymbol.defaultValue() is Default.RustDefault) {
when (shape) {
is FloatShape, is DoubleShape -> rustBlock("if ${variable.asValue()} != 0.0") {
block(variable)
}

is NumberShape -> rustBlock("if ${variable.asValue()} != 0") {
block(variable)
}

is BooleanShape -> rustBlock("if ${variable.asValue()}") {
block(variable)
}
}
} else if (memberSymbol.defaultValue() is Default.NonZeroDefault) {
val default = Node.printJson((memberSymbol.defaultValue() as Default.NonZeroDefault).value)
rustBlock("if ${variable.asValue()} != $default") {
block(variable)
}
} else {
rustBlock("") {
block(variable)
}
}
}

else -> rustBlock("") {
this.block(variable)
}
Expand Down Expand Up @@ -733,7 +755,7 @@ class RustWriter private constructor(
variable: ValueExpression,
block: RustWriter.(field: ValueExpression) -> Unit,
) {
ifSome(member, variable) { inner -> ifNotDefault(shape, inner, block) }
ifSome(member, variable) { inner -> ifNotNumberOrBoolDefault(shape, member, inner, block) }
}

fun listForEach(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import software.amazon.smithy.rust.codegen.core.smithy.protocols.HttpLocation
import software.amazon.smithy.rust.codegen.core.smithy.protocols.Protocol
import software.amazon.smithy.rust.codegen.core.smithy.protocols.ProtocolFunctions
import software.amazon.smithy.rust.codegen.core.smithy.protocols.parse.EventStreamUnmarshallerGenerator
import software.amazon.smithy.rust.codegen.core.smithy.protocols.serialize.SerializerUtil
import software.amazon.smithy.rust.codegen.core.smithy.protocols.serialize.ValueExpression
import software.amazon.smithy.rust.codegen.core.smithy.rustType
import software.amazon.smithy.rust.codegen.core.util.UNREACHABLE
Expand Down Expand Up @@ -129,6 +130,7 @@ class HttpBindingGenerator(
private val headerUtil = RuntimeType.smithyHttp(runtimeConfig).resolve("header")
private val defaultTimestampFormat = TimestampFormatTrait.Format.EPOCH_SECONDS
private val protocolFunctions = ProtocolFunctions(codegenContext)
private val serializerUtil = SerializerUtil(model, symbolProvider)

/**
* Generate a function to deserialize [binding] from HTTP headers.
Expand Down Expand Up @@ -560,7 +562,6 @@ class HttpBindingGenerator(
// default value for that primitive type (e.g. `Some(false)` for an `Option<bool>` header).
// If a header is multivalued, we always want to serialize its primitive members, regardless of their
// values.
val serializePrimitiveValuesIfDefault = memberSymbol.isOptional() || (targetShape is CollectionShape)
ifSome(memberSymbol, ValueExpression.Reference("&input.$memberName")) { variableName ->
if (targetShape is CollectionShape) {
renderMultiValuedHeader(
Expand All @@ -579,7 +580,8 @@ class HttpBindingGenerator(
false,
timestampFormat,
renderErrorMessage,
serializePrimitiveValuesIfDefault,
serializeIfDefault = memberSymbol.isOptional(),
memberShape,
)
}
}
Expand Down Expand Up @@ -610,6 +612,7 @@ class HttpBindingGenerator(
timestampFormat,
renderErrorMessage,
serializeIfDefault = true,
shape.member,
)
}
}
Expand All @@ -629,6 +632,7 @@ class HttpBindingGenerator(
timestampFormat: TimestampFormatTrait.Format,
renderErrorMessage: (String) -> Writable,
serializeIfDefault: Boolean,
memberShape: MemberShape,
) {
val context = HeaderValueSerializationContext(value, shape)
for (customization in customizations) {
Expand Down Expand Up @@ -668,7 +672,11 @@ class HttpBindingGenerator(
if (serializeIfDefault) {
block(context.valueExpression)
} else {
ifNotDefault(context.shape, context.valueExpression, block)
with(serializerUtil) {
ignoreDefaultsForNumbersAndBools(memberShape, context.valueExpression) {
block(context.valueExpression)
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class JsonSerializerGenerator(
"JsonValueWriter" to RuntimeType.smithyJson(runtimeConfig).resolve("serialize::JsonValueWriter"),
"ByteSlab" to RuntimeType.ByteSlab,
)
private val serializerUtil = SerializerUtil(model)
private val serializerUtil = SerializerUtil(model, symbolProvider)

/**
* Reusable structure serializer implementation that can be used to generate serializing code for
Expand Down Expand Up @@ -379,7 +379,7 @@ class JsonSerializerGenerator(
}

with(serializerUtil) {
ignoreZeroValues(context.shape, context.valueExpression) {
ignoreDefaultsForNumbersAndBools(context.shape, context.valueExpression) {
serializeMemberValue(context, targetShape)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package software.amazon.smithy.rust.codegen.core.smithy.protocols.serialize

import software.amazon.smithy.model.knowledge.NullableIndex
import software.amazon.smithy.model.shapes.BlobShape
import software.amazon.smithy.model.shapes.BooleanShape
import software.amazon.smithy.model.shapes.CollectionShape
Expand Down Expand Up @@ -90,13 +89,12 @@ abstract class QuerySerializerGenerator(private val codegenContext: CodegenConte
protected val model = codegenContext.model
protected val symbolProvider = codegenContext.symbolProvider
protected val runtimeConfig = codegenContext.runtimeConfig
private val nullableIndex = NullableIndex(model)
private val target = codegenContext.target
private val serviceShape = codegenContext.serviceShape
private val serializerError = runtimeConfig.serializationError()
private val smithyTypes = RuntimeType.smithyTypes(runtimeConfig)
private val smithyQuery = RuntimeType.smithyQuery(runtimeConfig)
private val serdeUtil = SerializerUtil(model)
private val serdeUtil = SerializerUtil(model, symbolProvider)
private val protocolFunctions = ProtocolFunctions(codegenContext)
private val codegenScope = arrayOf(
"String" to RuntimeType.String,
Expand Down Expand Up @@ -209,7 +207,7 @@ abstract class QuerySerializerGenerator(private val codegenContext: CodegenConte
}
} else {
with(serdeUtil) {
ignoreZeroValues(context.shape, context.valueExpression) {
ignoreDefaultsForNumbersAndBools(context.shape, context.valueExpression) {
serializeMemberValue(context, targetShape)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,38 @@

package software.amazon.smithy.rust.codegen.core.smithy.protocols.serialize

import software.amazon.smithy.codegen.core.SymbolProvider
import software.amazon.smithy.model.Model
import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.traits.ClientOptionalTrait
import software.amazon.smithy.model.traits.InputTrait
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
import software.amazon.smithy.rust.codegen.core.util.hasTrait

class SerializerUtil(private val model: Model) {
fun RustWriter.ignoreZeroValues(shape: MemberShape, value: ValueExpression, inner: Writable) {
// Required shapes should always be serialized
class SerializerUtil(private val model: Model, private val symbolProvider: SymbolProvider) {
fun RustWriter.ignoreDefaultsForNumbersAndBools(shape: MemberShape, value: ValueExpression, inner: Writable) {
// @required shapes should always be serialized, and members with @clientOptional or part of @input structures
// should ignore default values. If we have an Option<T>, it won't have a default anyway, so we don't need to
// ignore it.
// See https://github.com/smithy-lang/smithy-rs/issues/230 and https://github.com/aws/aws-sdk-go-v2/pull/1129
val container = model.expectShape(shape.container)
if (
shape.isRequired ||
shape.hasTrait<ClientOptionalTrait>() ||
// Zero values are always serialized in lists and collections, this only applies to structures
model.expectShape(shape.container) !is StructureShape
container !is StructureShape ||
container.hasTrait<InputTrait>()
) {
rustBlock("") {
inner(this)
}
} else {
this.ifNotDefault(model.expectShape(shape.target), value) { inner(this) }
this.ifNotNumberOrBoolDefault(model.expectShape(shape.target), symbolProvider.toSymbol(shape), value) {
inner(this)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class XmlBindingTraitSerializerGenerator(

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

sealed class Ctx {
abstract val input: String
Expand Down Expand Up @@ -523,7 +523,7 @@ class XmlBindingTraitSerializerGenerator(
} else {
ValueExpression.Value(ctx.input)
}
ignoreZeroValues(member, valueExpression) {
ignoreDefaultsForNumbersAndBools(member, valueExpression) {
inner(ctx)
}
}
Expand Down

0 comments on commit 71e37cd

Please sign in to comment.