-
Notifications
You must be signed in to change notification settings - Fork 202
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
Add support for real generics #1034
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This was referenced Sep 8, 2024
juhaku
force-pushed
the
feature-real-generics
branch
2 times, most recently
from
September 10, 2024 10:26
84a7a73
to
c16794a
Compare
This PR adds support for real generics which allows users to use deeply nested generic types as schemas without the alias hassle known to users. This commit will remove the old aliases support and implement completely new generics handling. (Something that many has been longing for long) This PR will also change the `schema!(...)` macro functionality to correctly generate schemas for arbitrary types even generic ones. Prior to this commit the generics where not resolved correctly. Example of new syntax. ```rust #[derive(ToSchema)] struct Type<T> { t: T, } #[derive(ToSchema)] struct Person<'p, T: Sized, P> { id: usize, name: Cow<'p, str>, field: T, t: P, } #[derive(ToSchema)] struct Page<T> { total: usize, page: usize, pages: usize, items: Vec<T>, } #[derive(OpenApi)] #[openapi( components( schemas( Person::<'_, String, Type<i32>>, Page::<Person<'_, String, Type<i32>>>, ) ) )] struct ApiDoc; ```
This commit further enhances the implementation for full generic support. From now on the full type paths need used to declare the types in the `OpenApi` and `#[utoipa::path]` macros. As further elaborated here #1020 (comment) the name of the schema is now resolved form the type name and possible generic arguments or via `as = ...` attribute and possible generic arguments. This name is then used across the OpenApi. This makes it a single place to define the name or prefixed name for the type unlike previously the path of the schema in `OpenApi` macro or `request_body` or `response body` was added to the name. This is a breaking change. Fixes #993 Closes #1020
juhaku
force-pushed
the
feature-real-generics
branch
from
September 10, 2024 12:52
c16794a
to
f8e2188
Compare
juhaku
changed the title
Wip add support for real generics
Add support for real generics
Sep 10, 2024
This was referenced Sep 10, 2024
juhaku
force-pushed
the
feature-real-generics
branch
from
September 10, 2024 13:20
f8e2188
to
815d387
Compare
Clean up the code and enhance documentation. This commit removes the old `aliases` functionality completely.
juhaku
force-pushed
the
feature-real-generics
branch
from
September 10, 2024 13:26
815d387
to
5474cee
Compare
This was referenced Sep 11, 2024
juhaku
added a commit
that referenced
this pull request
Oct 8, 2024
This PR fixes implementation of `ToSchema` on container types like `Vec<T>` or `Option<T>`. Prior to this commit it was not implemented which resulted errors when such types was used as generic argument. This commit fixes this which correctly allows following syntax. ```rust #[derive(ToSchema)] pub struct FooStruct<B> { pub foo: B, } #[derive(ToSchema)] enum FoosEnum { ThingNoAliasOption(FooStruct<Option<i32>>), FooEnumThing(#[schema(inline)] FooStruct<Vec<i32>>), FooThingOptionVec(#[schema(inline)] FooStruct<Option<Vec<i32>>>), FooThingLinkedList(#[schema(inline)] FooStruct<std::collections::LinkedList<i32>>), FooThingBTreeMap(#[schema(inline)] FooStruct<std::collections::BTreeMap<String, String>>), FooThingHashMap(#[schema(inline)] FooStruct<std::collections::HashMap<i32, String>>), FooThingHashSet(#[schema(inline)] FooStruct<std::collections::HashSet<i32>>), FooThingBTreeSet(#[schema(inline)] FooStruct<std::collections::BTreeSet<i32>>), } ``` **Note!** Currently generic argument cannot be `slice`, `array` or `tuple`. That is a hard limitation of current `TypeTree` implementation and would need some work before such types were supported. Relates #1034 Fixes #1099
juhaku
added a commit
that referenced
this pull request
Oct 8, 2024
This PR fixes implementation of `ToSchema` on container types like `Vec<T>` or `Option<T>`. Prior to this commit it was not implemented which resulted errors when such types was used as generic argument. This commit fixes this which correctly allows following syntax. ```rust #[derive(ToSchema)] pub struct FooStruct<B> { pub foo: B, } #[derive(ToSchema)] enum FoosEnum { ThingNoAliasOption(FooStruct<Option<i32>>), FooEnumThing(#[schema(inline)] FooStruct<Vec<i32>>), FooThingOptionVec(#[schema(inline)] FooStruct<Option<Vec<i32>>>), FooThingLinkedList(#[schema(inline)] FooStruct<std::collections::LinkedList<i32>>), FooThingBTreeMap(#[schema(inline)] FooStruct<std::collections::BTreeMap<String, String>>), FooThingHashMap(#[schema(inline)] FooStruct<std::collections::HashMap<i32, String>>), FooThingHashSet(#[schema(inline)] FooStruct<std::collections::HashSet<i32>>), FooThingBTreeSet(#[schema(inline)] FooStruct<std::collections::BTreeSet<i32>>), } ``` **Note!** Currently generic argument cannot be `slice`, `array` or `tuple`. That is a hard limitation of current `TypeTree` implementation and would need some work before such types were supported. Relates #1034 Fixes #1099
juhaku
added a commit
that referenced
this pull request
Oct 8, 2024
This PR fixes implementation of `ToSchema` on container types like `Vec<T>` or `Option<T>`. Prior to this commit it was not implemented which resulted errors when such types was used as generic argument. This commit fixes this which correctly allows following syntax. ```rust #[derive(ToSchema)] pub struct FooStruct<B> { pub foo: B, } #[derive(ToSchema)] enum FoosEnum { ThingNoAliasOption(FooStruct<Option<i32>>), FooEnumThing(#[schema(inline)] FooStruct<Vec<i32>>), FooThingOptionVec(#[schema(inline)] FooStruct<Option<Vec<i32>>>), FooThingLinkedList(#[schema(inline)] FooStruct<std::collections::LinkedList<i32>>), FooThingBTreeMap(#[schema(inline)] FooStruct<std::collections::BTreeMap<String, String>>), FooThingHashMap(#[schema(inline)] FooStruct<std::collections::HashMap<i32, String>>), FooThingHashSet(#[schema(inline)] FooStruct<std::collections::HashSet<i32>>), FooThingBTreeSet(#[schema(inline)] FooStruct<std::collections::BTreeSet<i32>>), } ``` **Note!** Currently generic argument cannot be `slice`, `array` or `tuple`. That is a hard limitation of current `TypeTree` implementation and would need some work before such types were supported. Relates #1034 Fixes #1099
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds support for real generics which allows users to use deeply nested generic types as schemas without the alias hassle known to users. This commit will remove the old aliases support and implement completely new generics handling. (Something that many has been longing for long)
This commit further enhances the implementation for full generic support. From now on the full type paths need used to declare the types in the
OpenApi
and#[utoipa::path]
macros.As further elaborated here #1020 (comment) the name of the schema is now resolved form the type name and possible generic arguments or via
as = ...
attribute and possible generic arguments. This name is then used across the OpenApi. This makes it a single place to define the name or prefixed name for the type unlikepreviously the path of the schema in
OpenApi
macro orrequest_body
orresponse body
was added to the name.This PR will also change the
schema!(...)
macro functionality to correctly generate schemas for arbitrary types even generic ones. Prior to this commit the generics where not resolved correctly. Still all generic arguments must implementToSchema
trait in order to generate schema in first place.Clean up the code and enhance documentation.
Breaking changes
schema(as = ...)
attribute now defines the prefix for the name of the component throughout OpenAPI references thus path that is possibly defined torequest_body
or responsesbody
will not affect the name of the schema anymore.Example of new syntax.
Fixes #703 Fixes #818 Fixes #574 Fixes #566 Fixes #861 Fixes #979 Fixes #503 Fixes #644 Fixes #790 Fixes #835 Fixes #817 Fixes #993 Fixes #961 Fixes #939 Fixes #729 Fixes #862
Closes #1020
Relates #751