-
Notifications
You must be signed in to change notification settings - Fork 188
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
Creating aliases outside the context of the original struct #790
Comments
The docs say:
That doesn't seem to be the case, as creating a manual type alias and using that instead doesn't work. It seems to take the name, but then falls back to the same problem. |
I tried with: #[derive(ToSchema)]
pub struct SearchResultFoo(SearchResult<Foo>>); However, that only creates a direct ref to And it doesn't look like there's an |
Actually I would expect this to work, but it doesn't: pub struct SearchResultFoo(SearchResult<Vec<Foo>>);
impl<'__s> ToSchema<'__s> for SearchResultFoo {
fn schema() -> (&'__s str, RefOr<Schema>) {
("SearchResultFoo", schema!(#[inline] SearchResult<Vec<Foo>>))
}
} |
I would expect this to work too: #[derive(ToSchema)]
#[aliases(SearchResultFoo = LocalSearchResult<Vec<Foo>>)]
pub struct LocalSearchResult<T>(SearchResult<T>); However that fails to compile with:
|
The only thing that seems to work is to copy the type into the downstream creates … which is far from optimal. |
Rust's type aliases are quite tricky because because they are not real types thus no traits can be applied directly to them. More over when the proc macro is compiled it reads the available source code as token stream and there is no way of evaluating at compile time what would be the actual type of a rust type alias that I know. I know there is an existing issue that suggests creating a "global" register for type aliases that utoipa could leverage when it is compiled by looking for a registered schema by the type alias. Something like this could be possibly added in future. Also to this one PRs are wellcome. 🙂 Now to implement schema for a third party type, there are basically 3 options.
The 1. option is a quite close to the serde's More about this topic here: #656 (comment) #507 #390 |
What about adding an alias/with/external attribute to the |
If it is an arbitrary schema it has a type of a utoipa/utoipa/src/openapi/schema.rs Line 132 in 83356da
The At the moment if I am not mistaken the |
I tried the "global" register for type aliases in my other project but it didn't work. Rust makes no gaurentee that one process will handle all the proc macro invocations. From my observation, a process is spawned for EACH invocation. To make it work, a file db like SQLite or shared memory is needed |
@JakkuSakura I have some plans for the global aliases, but still in early stages. More updates coming up later.
Yes, at least shared file to read from is necessary. |
Assuming I have some struct:
Which I use throughout different APIs. that makes it impossible to use the
#[alias]
, as it doesn't know anything about the downstream types. And even does not have access to them.I tried using an simple
pub type SearchResult = common::SearchResult<Foo>
, using it as part of the actix function, as well as registering it to the OpenAPI schema. However, this again results in an object referencingT
:The text was updated successfully, but these errors were encountered: