Skip to content

Commit 84f5464

Browse files
authored
Avoid treating addedDefault as default when serializing numbers and bools (#4117)
## Description This PR fixes a regression in input serializers. Previously, fields of numbers/bool annotated with the `required` trait were always serialized. With the introduction of the [addedDefault](https://smithy.io/2.0/spec/type-refinement-traits.html#addeddefault-trait) trait, they stopped being serialized when an input values match the default even if explicitly set by the user. An example is `LoggingConfig` in CloudFront ([change](awslabs/aws-sdk-rust@3465444#diff-8ff83311bcd0649ac0bee175e8a1f3d1fce9c8863c64b896c58f41ac17051bf0R8)). The previously required fields `enabled` and `include_cookies` were annotated with the `addedDefault` trait and the `LoggingConfig`'s serializer made serialization of those fields conditional: ``` pub fn ser_logging_config(...) -> ... { #[allow(unused_mut)] let mut scope = writer.finish(); if input.enabled { let mut inner_writer = scope.start_el("Enabled").finish(); inner_writer.data(::aws_smithy_types::primitive::Encoder::from(input.enabled).encode()); } if input.include_cookies { let mut inner_writer = scope.start_el("IncludeCookies").finish(); inner_writer.data(::aws_smithy_types::primitive::Encoder::from(input.include_cookies).encode()); } ... ``` When their values were `false`, they did not get serialized, regardless of whether they were explicitly set; when they were `required` fields previously, they had always been serialized. This has caused CloudFront SDK users to encounter an error response from the service, e.g, ``` You must specify IncludeCookies when Enabled is missing or set to true within a Logging object ``` This PR treats the `addedDefault` trait as non `default`, making the fields always be serialized. ## Testing - CI and release pipeline - Confirmed that `LoggingConfig`'s serializer always serializes the fields in question ## Checklist <!--- If a checkbox below is not applicable, then please DELETE it rather than leaving it unchecked --> - [x] For changes to the smithy-rs codegen or runtime crates, I have created a changelog entry Markdown file in the `.changelog` directory, specifying "client," "server," or both in the `applies_to` key. - [x] For changes to the AWS SDK, generated SDK code, or SDK runtime crates, I have created a changelog entry Markdown file in the `.changelog` directory, specifying "aws-sdk-rust" in the `applies_to` key. ---- _By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice._
1 parent 9db6b04 commit 84f5464

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

.changelog/1746737221.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
applies_to:
3+
- client
4+
- server
5+
- aws-sdk-rust
6+
authors:
7+
- ysaito1001
8+
references:
9+
- smithy-rs#4117
10+
breaking: false
11+
new_feature: false
12+
bug_fix: true
13+
---
14+
Fix a bug where fields that were initially annotated with the `required` trait and later updated to use the `addedDefault` trait were not serialized when their values matched the default, even when the values were explicitly set. With this fix, fields with `addedDefault` are now always serialized.

codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/protocols/serialize/SerializerUtil.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import software.amazon.smithy.codegen.core.SymbolProvider
99
import software.amazon.smithy.model.Model
1010
import software.amazon.smithy.model.shapes.MemberShape
1111
import software.amazon.smithy.model.shapes.StructureShape
12+
import software.amazon.smithy.model.traits.AddedDefaultTrait
1213
import software.amazon.smithy.model.traits.ClientOptionalTrait
1314
import software.amazon.smithy.model.traits.InputTrait
1415
import software.amazon.smithy.rust.codegen.core.rustlang.RustWriter
@@ -30,6 +31,10 @@ class SerializerUtil(private val model: Model, private val symbolProvider: Symbo
3031
if (
3132
shape.isRequired ||
3233
shape.hasTrait<ClientOptionalTrait>() ||
34+
// Treating `addedDefault` as `default` could break existing serializers.
35+
// Fields that were previously marked as `required` but later replaced with `addedDefault`
36+
// may no longer be serialized. This could occur when an explicitly set value matches the default.
37+
shape.hasTrait<AddedDefaultTrait>() ||
3338
// Zero values are always serialized in lists and collections, this only applies to structures
3439
container !is StructureShape ||
3540
container.hasTrait<InputTrait>()

0 commit comments

Comments
 (0)