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

Recursive Generics not possible with derive(ToSchema)? #861

Closed
DragonDev1906 opened this issue Feb 9, 2024 · 1 comment · Fixed by #1034
Closed

Recursive Generics not possible with derive(ToSchema)? #861

DragonDev1906 opened this issue Feb 9, 2024 · 1 comment · Fixed by #1034
Labels
Generics - Hard Stuff concerning generics implementation

Comments

@DragonDev1906
Copy link

I have a (recursive) struct/enum with a generic that contains itself in one of its fields. I don't think that can currently be specified using the derive macro.

There have been two issues regarding similar situations:

I have tried using aliases (as shown below), but due to the type itself (and not just its generic C) being in a field this does not work and results in an error in the swagger ui (not exactly the same, as my type was more complex when I took the screenshot):
2024-02-09-110149_1256x385_scrot

#[derive(Debug, Serialize, Deserialize)]
#[derive(utoipa::ToSchema)]
#[aliases(RuleX = Rule<Check>, PrunedRule = Rule<PrunedCheck>)]
pub enum Rule<C> {
    All(Vec<Rule<C>>),
}

I think the only way to do this would be manually implementing ToSchema, at which point I'm starting to wonder how useful it is to generate the openapi docs from the Code in the first place.

From what I've understood solving this with type aliases is fundamentally impossible, as it would need one alias in the fields for RuleX and another for PrunedRule. The only other way I can think of solving this would be to use Rule<Rule<Check>> and remove the use of Rule in the enum fields, but I don't really want to do that.


Related question: Is it possible to use utoipa for some things (e.g. the endpoints) and add additional things (e.g. additional schemas) for situations like this where specifying it as yaml is just simpler?

@juhaku juhaku added the Generics - Hard Stuff concerning generics implementation label Sep 4, 2024
@juhaku
Copy link
Owner

juhaku commented Sep 10, 2024

There is now new implementation for generics coming up in #1034 which should solve the issue with aliases approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Generics - Hard Stuff concerning generics implementation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants