juhaku / utoipa

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

Can't derive `ToSchema` for maps whose value type is an `Option<Enum>` #897

Open swwu opened 2 months ago

swwu commented 2 months ago

E.g. the following fails with a compile error:

#[derive(utoipa::ToSchema)]
pub enum InnerEnum {
    A(String),
    B(f64),
}

// this works
#[derive(utoipa::ToSchema)]
pub struct OkOuter1 {
    map: Option<InnerEnum>,
}
// this works
#[derive(utoipa::ToSchema)]
pub struct OkOuter2 {
    map: HashMap<String, InnerEnum>,
}

// this fails
#[derive(utoipa::ToSchema)]
pub struct BadOuter {
    map: HashMap<String, Option<InnerEnum>>,
}

The (presumably) same issue also occurs for BTreeMap<String, Option<InnerEnum>>.

This seems to be a particular edge case of jsonschema type composition, as this workaround, which would describe an identical JSON-serialized document, works fine:

pub struct OptionInnerEnum(Option<InnerEnum>);
#[derive(utoipa::ToSchema)]
pub struct WorkaroundOuter {
    map: HashMap<String, OptionInnerEnum>,
}