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

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

Open
swwu opened this issue Apr 24, 2024 · 0 comments
Open

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

swwu opened this issue Apr 24, 2024 · 0 comments

Comments

@swwu
Copy link

swwu commented Apr 24, 2024

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>,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant