smithy-rs
smithy-rs copied to clipboard
Make `Instantiator` use modeled default values when returning dummy output in HTTP request tests
When generating an HTTP request test case, we code-generate an operation handler that returns "dummy" operation output, just so the test compiles. We do so very naively by filling out the operation output struct's fields with dummy data in Instantiator
:
https://github.com/smithy-lang/smithy-rs/blob/17545f6fc1ce303198e5740ca0c5b65fbcb48948/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/generators/Instantiator.kt#L448-L471
As the docs warn, these dummy values do not honor any modeled constraint traits.
This can make a test fail when building the operation output. For example, the RpcV2CborServerPopulatesDefaultsWhenMissingInRequestBody
request test case generates:
let expected = crate::input::OperationWithDefaultsInput {
defaults: ::std::option::Option::Some(crate::model::Defaults {
default_string: "hi".to_owned(),
default_boolean: true,
default_list: vec![],
// <snip>
default_enum: "FOO"
.parse::<crate::model::TestEnum>()
.expect("static value validated to member"),
// <snip>
}),
// <snip>
};
Which fails with:
thread 'operation::operation_with_defaults_test::rpc_v2_cbor_server_populates_defaults_when_missing_in_request_body_request' panicked at rpcv2Cbor/rust-server-codegen/src/operation.rs:468:30:
static value validated to member: ConstraintViolation("")
Indeed, ""
is not a valid value for the TestEnum
shape.
Instead of resorting to these dummy naive values, we should use any modeled default values. It might happen that there are no defaults for a @required
shape, in which case we'd have to code for generating a dummy value that honors the modeled constraint traits, but since doing that might require some effort, we can punt on that for the time being.