juhaku/utoipa

ToSchema fails with Option

Closed this issue · 6 comments

In 5.0.0 taking the example from the README and using an Option:

#[derive(ToSchema)]
struct Account {
    id: Option<i32>,
}

the following error is produced:

no variant or associated item named `from_iter` found for enum `SchemaType` in the current scope

Has something else changed which is not clear in the docs or that I am missing? Thanks!

That's weird, utoipa should not compile if Option<T> was not possible. I created test project and it compiles without issues.
image

Perhaps you have some cache from the old present in your system. You could try to run cargo update utoipa and also utiopa-* other projects that you are using. If you run cargo update it will update your whole lock file. Alternatively you can try to remove the cargo cache instance of utoipa and or cargo clean.

I just figured out that the difference between my working project and non-working project is the edition in the toml file.
my project had edition = "2018" and with edition = "2021" it works 🙈

Woah, never thought that it should have an impact, I was in belief that Rust edition is backwards compatible. 😮 But seemingly that is not completely true.

Nonetheless probably the edition 2018 compiles something differently to 2021 which cause it not work work and might be that utoipa relies on some functionality that is only available since edition 2021.

Nonetheless probably the edition 2018 compiles something differently to 2021 which cause it not work work and might be that utoipa relies on some functionality that is only available since edition 2021.

If this is true (probably needs testing) can we document it?

@mikereid1 Ah, now I know what is going on, Indeed 2021 versus 2018 editions work bit differently. If you import the use std::iter::FromIterator; it will work on the above cases, Seems like edition 2021 will be able to work without external import of FromIterator trait.