Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Instantiator use modeled default values when returning dummy output in HTTP request tests #3743

Open
david-perez opened this issue Jul 8, 2024 · 0 comments

Comments

@david-perez
Copy link
Contributor

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:

/**
* Returns a default value for a shape.
*
* Warning: this method does not take into account any constraint traits attached to the shape.
*/
private fun fillDefaultValue(shape: Shape): Node =
when (shape) {
is MemberShape -> fillDefaultValue(model.expectShape(shape.target))
// Aggregate shapes.
is StructureShape -> Node.objectNode()
is UnionShape -> Node.objectNode()
is CollectionShape -> Node.arrayNode()
is MapShape -> Node.objectNode()
// Simple Shapes
is TimestampShape -> Node.from(0) // Number node for timestamp
is BlobShape -> Node.from("") // String node for bytes
is StringShape -> Node.from("")
is NumberShape -> Node.from(0)
is BooleanShape -> Node.from(false)
is DocumentShape -> Node.objectNode()
else -> throw CodegenException("Unrecognized shape `$shape`")
}

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant