-
Notifications
You must be signed in to change notification settings - Fork 190
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
Unable to create schema with const generic param #1115
Comments
Okay, I need to investigate this. |
Well this is a bit unfortunate as the #[derive(ToSchema)]
pub struct ArrayResponse<T: ToSchema, const N: usize> {
array: [T; N],
}
// for this type the below is what gets generated.
// #[derive(ToSchema)]
struct CombinedResponse<T: ToSchema, const N: usize> {
pub array_response: ArrayResponse<T, N>,
} This is a bit problematic since it will generate the schema with Perhaps we could stringify the impl<T: ToSchema, const N: usize> utoipa::__dev::ComposeSchema for CombinedResponse<T, N>
where
T: utoipa::ToSchema,
{
fn compose(
mut generics: Vec<utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>>,
) -> utoipa::openapi::RefOr<utoipa::openapi::schema::Schema> {
utoipa::openapi::ObjectBuilder::new()
.property(
"array_response",
utoipa::openapi::schema::RefBuilder::new().ref_location_from_schema_name(
std::borrow::Cow::Owned(format!(
"{}_{}",
<ArrayResponse<T, N> as utoipa::ToSchema>::name(),
std::borrow::Cow::<String>::Owned(
[
<T as utoipa::ToSchema>::name(),
<N as utoipa::ToSchema>::name(), // <-- for const this is not possible
]
.to_vec()
.join("_")
)
)),
),
)
.required("array_response")
.into()
}
}
impl<T: ToSchema, const N: usize> utoipa::ToSchema for CombinedResponse<T, N>
where
T: utoipa::ToSchema,
{
fn name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed("CombinedResponse")
}
fn schemas(
schemas: &mut Vec<(
String,
utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>,
)>,
) {
schemas.extend([(
String::from(std::borrow::Cow::Owned(format!(
"{}_{}",
<ArrayResponse<T, N> as utoipa::ToSchema>::name(),
std::borrow::Cow::<String>::Owned(
[
<T as utoipa::ToSchema>::name(),
<N as utoipa::ToSchema>::name(), // <-- for const this is not possible
]
.to_vec()
.join("_")
)
))),
<ArrayResponse<T, N> as utoipa::__dev::ComposeSchema>::compose(
[
<T as utoipa::PartialSchema>::schema(),
<N as utoipa::PartialSchema>::schema(), // <-- for const this is not possible
]
.to_vec(),
),
)]);
<ArrayResponse<T, N> as utoipa::ToSchema>::schemas(schemas);
}
} I am not sure whether this kind of situation is very common for users, but I am afraid that there is no way around this and this comes as mandatory caveat in order to support more dynamic generics and schema collection. EDIT: |
Okay The actual code is just a Test case to Test utoipauto Crate. |
In theory we could find the type bound from the container type's generic params. True, but struct Array<T, const N: usize> {
array: [T; N],
} Still resolving the type could be a way forward and perhaps is one step closer to support This seems to be bit more evolved thus I'll try to work on the PR as a next thing, but it wont be quick fix. If we resolve the |
That Sounds good. For now a quick fix would be to ignore the const so that it Compiles and document it nicely |
Yup, I will ignore the param for now and start preparing for 5.0.0 release. The const generic can be experimented in 5.x later on. |
This commit will filter out const generics from being added to the schema composing because the value is not used in anyway. Perhaps in some future release the real support of const generics could be experimented. Fixes #1115
This commit will filter out const generics from being added to the schema composing because the value is not used in anyway. Perhaps in some future release the real support of const generics could be experimented. Fixes #1115
This commit will filter out const generics from being added to the schema composing because the value is not used in anyway. Perhaps in some future release the real support of const generics could be experimented. Fixes #1115
@DenuxPlays There is PR #1118 which fixes this by ignoring the const generics allowing the build to compile. |
This commit will filter out const generics from being added to the schema composing because the value is not used in anyway. Perhaps in some future release the real support of const generics could be experimented. Fixes #1115
Not really fixed: I've updated the mr where the acceptance tests are failing: ProbablyClem/utoipauto#45 |
@juhaku This needs to be reopened |
Oh man, that likely needs some further experimenting around the const generics. |
This commit fixes path rewrite which previously ignored other that Type generic arguments totally from the path. This changes it to rewrite only the Type generic arguments as previously while keeping the other generic arguments as is in the path. Fixes #1115
This commit fixes path rewrite which previously ignored other that Type generic arguments totally from the path. This changes it to rewrite only the Type generic arguments as previously while keeping the other generic arguments as is in the path. Fixes #1115
This commit fixes path rewrite which previously ignored other that Type generic arguments totally from the path. This changes it to rewrite only the Type generic arguments as previously while keeping the other generic arguments as is in the path. Fixes #1115
Finally fixed it "again", I had a half baked implementation of path rewrite in place which did ignore all other generic arguments type Type generic arguments. #1120 |
This commit fixes path rewrite which previously ignored other that Type generic arguments totally from the path. This changes it to rewrite only the Type generic arguments as previously while keeping the other generic arguments as is in the path. Fixes #1115
Hey,
since a few commits (idk exactly when) the ToSchema derive for generic schemas with a constant generic is broken.
Example:
https://github.com/ProbablyClem/utoipauto/pull/45/files#diff-93ada2911f7e76502c64197e27153dc94a87ed57311b0643fd310d176ce93145R37
The text was updated successfully, but these errors were encountered: