juhaku / utoipa

Simple, Fast, Code first and Compile time generated OpenAPI documentation for Rust
Apache License 2.0
2.23k stars 173 forks source link

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

Closed DragonDev1906 closed 3 weeks ago

DragonDev1906 commented 7 months ago

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 commented 3 weeks ago

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