Unverified Commit 345624eb authored by Miles Ziemer's avatar Miles Ziemer Committed by GitHub
Browse files

Ignore actual default value instead of 0/false (#3252)



## Motivation and Context
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here -->
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.

## Description
<!--- Describe your changes in detail -->
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.

## Testing
<!--- Please describe in detail how you tested your changes -->
<!--- Include details of your testing environment, and the tests you ran
to -->
<!--- see how your change affects other areas of the code, etc. -->
- [x] Generated AWS SDK and inspected diff. Only changes are not
ignoring default value inside `if let Some() = ...` blocks, and ignoring
default value instead of just 0 (only seems to effect nimble).
- [x] Added protocol tests for serializing 0/false in query params for
restXml and restJson

## Checklist
<!--- If a checkbox below is not applicable, then please DELETE it
rather than leaving it unchecked -->
- [x] I have updated `CHANGELOG.next.toml` if I made changes to the
smithy-rs codegen or runtime crates
- [x] I have updated `CHANGELOG.next.toml` if I made changes to the AWS
SDK, generated SDK code, or SDK runtime crates

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._

---------

Co-authored-by: default avatarJohn DiSanti <john@vinylsquid.com>
parent 952b7d89
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -109,3 +109,15 @@ message = "Add support for constructing [`SdkBody`] and [`ByteStream`] from `htt
references = ["smithy-rs#3300", "aws-sdk-rust#977"]
meta = { "breaking" = false, "tada" = true, "bug" = false }
author = "rcoh"

[[smithy-rs]]
message = "Serialize 0/false in query parameters, and ignore actual default value during serialization instead of just 0/false. See [changelog discussion](https://github.com/smithy-lang/smithy-rs/discussions/3312) for details."
references = ["smithy-rs#3252", "smithy-rs#3312"]
meta = { "breaking" = false, "tada" = false, "bug" = true, "target" = "client" }
author = "milesziemer"

[[aws-sdk-rust]]
message = "Serialize 0/false in query parameters, and ignore actual default value during serialization instead of just 0/false. See [changelog discussion](https://github.com/smithy-lang/smithy-rs/discussions/3312) for details."
references = ["smithy-rs#3252", "smithy-rs#3312"]
meta = { "breaking" = false, "tada" = false, "bug" = true }
author = "milesziemer"
+31 −0
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ service RestXmlExtras {
        StringHeader,
        CreateFoo,
        RequiredMember,
        // TODO(https://github.com/smithy-lang/smithy-rs/issues/3315)
        ZeroAndFalseQueryParams,
    ]
}

@@ -254,3 +256,32 @@ structure RequiredMemberInputOutput {
    @required
    requiredString: String
}

@httpRequestTests([
    {
        id: "RestXmlZeroAndFalseQueryParamsAreSerialized"
        protocol: restXml
        code: 200
        method: "GET"
        uri: "/ZeroAndFalseQueryParams"
        body: ""
        queryParams: [
            "Zero=0",
            "False=false"
        ]
        params: {
            zeroValue: 0
            falseValue: false
        }
    }
])
@http(uri: "/ZeroAndFalseQueryParams", method: "GET")
operation ZeroAndFalseQueryParams {
    input := {
        @httpQuery("Zero")
        zeroValue: Integer

        @httpQuery("False")
        falseValue: Boolean
    }
}
+11 −3
Original line number Diff line number Diff line
@@ -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
@@ -74,6 +75,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(
@@ -246,12 +248,18 @@ class RequestBindingGenerator(

                    paramList(target, derefName, param, writer, memberShape)
                } else {
                    ifSet(target, memberSymbol, ValueExpression.Reference("&_input.$memberName")) { field ->
                    // 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)
                            }
                        }
                    }
                }
            }
            writer.rustTemplate("#{Ok}(())", *codegenScope)
        }
        return true
+32 −0
Original line number Diff line number Diff line
@@ -68,6 +68,8 @@ service RestJsonExtras {
        // TODO(https://github.com/smithy-lang/smithy-rs/issues/2968): Remove the following once these tests are included in Smithy
        // They're being added in https://github.com/smithy-lang/smithy/pull/1908
        HttpPayloadWithUnion,
        // TODO(https://github.com/smithy-lang/smithy-rs/issues/3315)
        ZeroAndFalseQueryParams,
    ],
    errors: [ExtraError]
}
@@ -351,3 +353,33 @@ structure EmptyStructWithContentOnWireOpOutput {
operation EmptyStructWithContentOnWireOp {
    output: EmptyStructWithContentOnWireOpOutput,
}
@http(uri: "/zero-and-false-query-params", method: "GET")
@httpRequestTests([
    {
        id: "RestJsonZeroAndFalseQueryParamsAreSerialized",
        protocol: restJson1,
        code: 200,
        method: "GET",
        uri: "/zero-and-false-query-params",
        body: "",
        queryParams: [
            "Zero=0",
            "False=false"
        ],
        params: {
            zeroValue: 0,
            falseValue: false
        }
    }
])
operation ZeroAndFalseQueryParams {
    input: ZeroAndFalseQueryParamsInput
}

structure ZeroAndFalseQueryParamsInput {
    @httpQuery("Zero")
    zeroValue: Integer

    @httpQuery("False")
    falseValue: Boolean
}
+37 −19
Original line number Diff line number Diff line
@@ -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
@@ -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
@@ -729,14 +732,18 @@ 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(
        fun ifNotNumberOrBoolDefault(
            shape: Shape,
            memberSymbol: Symbol,
            variable: ValueExpression,
            block: RustWriter.(field: ValueExpression) -> Unit,
        ) {
            when (shape) {
                is NumberShape, is BooleanShape -> {
                    if (memberSymbol.defaultValue() is Default.RustDefault) {
                        when (shape) {
                            is FloatShape, is DoubleShape ->
                                rustBlock("if ${variable.asValue()} != 0.0") {
@@ -752,10 +759,21 @@ class RustWriter private constructor(
                                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)
                        block(variable)
                    }
            }
        }
@@ -792,7 +810,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(
Loading