juhaku / utoipa

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

Unable to ignore field : compile error with fn field on a struct #1203

Open reneklacan opened 1 week ago

reneklacan commented 1 week ago

Utoipa version: 5.2

Minimal code to replicate:

#[derive(ToSchema)]
pub struct Foo {
    // ...
    #[schema(ignore)]
    pub bar: Option<fn(String) -> String>,
}

Build error:

error: unexpected type in component part get type path, expected one of: Path, Tuple, Reference, Group, Array, Slice, TraitObject

Expected behavior: I'd at least hope field like this can be ignored and won't trigger a compilation error.

Workaround (create type that has subset of fields and alias it with as):

pub struct Foo {
    // ...
    #[schema(ignore)]
    pub bar: Option<fn(String) -> String>,
}

#[derive(ToSchema)]
#[schema(as = Foo)]
pub struct FooSimple {
    // ...
}
juhaku commented 1 week ago

This is due to utoipa is not able to recognize pub bar: Option<fn(String) -> String>, type with fn argument as value while parsing ToSchema. This support could be added though and should be quite simple, but I might be wrong as well. For quick fix as well you can add #[schema(value_type=()] for example to get around the compile error.

reneklacan commented 1 week ago

For quick fix as well you can add #[schema(value_type=()] for example to get around the compile error.

That unfortunately doesn't work. It was one of the first things I tried, it still fails with the same error.