Utoipa recursive objects causing stack overflows

Here’s an example of an Enum which is completely legal in Rust and … makes utoipa / axum stack overflow while generating a schema:

#[derive(ToSchema)]
pub enum Filter {
    Eq(String, String),
    Cnt(String, String),
    Pres(String),
    Or(Vec<Filter>),
    And(Vec<Filter>),
    AndNot(Box<Filter>),
}

The problem is that whole “contains itself” thingin the second half of the variants. Again, completely legal, nothing weird about it, but codegen just falls in a heap.

Here’s the fixed version with appropriate attributes:

#[derive(ToSchema)]
pub enum Filter {
    Eq(String, String),
    Cnt(String, String),
    Pres(String),
    #[schema(no_recursion)]
    Or(Vec<Filter>),
    #[schema(no_recursion)]
    And(Vec<Filter>),
    #[schema(no_recursion)]
    AndNot(Box<Filter>),
}

This tells the schema generator to just chill when it’s doing its thing, and stops the stack overflows in their tracks!

References



#rust #utoipa #axum #codegen #openapi