smithy-lang / smithy-rs

Code generation for the AWS SDK for Rust, as well as server and generic smithy client generation.
Apache License 2.0
508 stars 190 forks source link

Fix directly constrained `List` shape with indirectly constrained aggregate type member shape #3894

Closed drganjoo closed 2 weeks ago

drganjoo commented 1 month ago

This PR fixes a bug in the code generation for a directly constrained List shape that has an indirectly constrained aggregate type as a member shape.

For example, in the following model:

@http(uri: "/sample", method: "POST")
operation SampleOp {
  input := {
      a: ItemList
  }
  errors: [ValidationException]
}
@length(min: 1 max: 100)
list ItemList {
  member: Item
}
map Item {
  key: ItemName
  value: ItemDescription
}
@length(min: 0 max: 65535)
string ItemName
string ItemDescription

A TryFrom implementation is generated that converts from ItemListUnconstrained to ItemList by converting each member of the inner list of ItemUnconstrained to ItemConstrained and then converting it to Vec<Vec<ItemName>>:

    impl std::convert::TryFrom<ItemListUnconstrained> for crate::model::ItemList {
        type Error = crate::model::item_list::ConstraintViolation;
        fn try_from(value: ItemListUnconstrained) -> Result<Self, Self::Error> {
            let res: Result<
                ::std::vec::Vec<
                    ::std::collections::HashMap<crate::model::ItemName, ::std::string::String>,
                >,
                (usize, crate::model::item::ConstraintViolation),
            > = value
                .0
                .into_iter()
                .enumerate()
                .map(|(idx, inner)| {
                    inner.try_into()
                        .map(|ic: crate::constrained::item_constrained::ItemConstrained| ic.into())
                        .map_err(|inner_violation| (idx, inner_violation))
                })
                .collect();
            let inner =
                res.map_err(|(idx, inner_violation)| Self::Error::Member(idx, inner_violation))?;
            Self::try_from(inner)
        }
    }

Partially fixes issue: #3885 for publicly constrained shapes only.

github-actions[bot] commented 1 month ago

A new generated diff is ready to view.

A new doc preview is ready to view.

github-actions[bot] commented 1 month ago

A new generated diff is ready to view.

A new doc preview is ready to view.

github-actions[bot] commented 1 month ago

A new generated diff is ready to view.

A new doc preview is ready to view.

github-actions[bot] commented 1 month ago

A new generated diff is ready to view.

A new doc preview is ready to view.

github-actions[bot] commented 1 month ago

A new generated diff is ready to view.

A new doc preview is ready to view.

github-actions[bot] commented 1 month ago

A new generated diff is ready to view.

A new doc preview is ready to view.

github-actions[bot] commented 2 weeks ago

A new generated diff is ready to view.

A new doc preview is ready to view.

github-actions[bot] commented 2 weeks ago

A new generated diff is ready to view.

A new doc preview is ready to view.