juhaku / utoipa

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

bug: the trait bound `BigDecimal: PartialSchema` is not satisfied #1205

Closed francis2tm closed 1 week ago

francis2tm commented 1 week ago

Hello, Amazing project! Thank you for your work. I've just upgraded my utopia crate from 5.0.0-beta to the latest version 5.2.0 and now I have a problem:

error[E0277]: the trait bound `BigDecimal: PartialSchema` is not satisfied
   --> server/src/db/file.rs:193:30
    |
193 |     pub price_amount: Option<BigDecimal>,
    |                              ^^^^^^^^^^ the trait `ComposeSchema` is not implemented for `BigDecimal`, which is required by `BigDecimal: PartialSchema`
    |
    = help: the following other types implement trait `ComposeSchema`:
              &[T]
              &mut [T]
              &str
              Account
              AccountsMembership
              BTreeMap<K, T>
              BTreeSet<K>
              BillingCustomer
            and 51 others
    = note: required for `BigDecimal` to implement `PartialSchema`
note: required by a bound in `utoipa::ToSchema::name`
   --> /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/utoipa-5.2.0/src/lib.rs:364:21
    |
364 | pub trait ToSchema: PartialSchema {
    |                     ^^^^^^^^^^^^^ required by this bound in `ToSchema::name`
...
395 |     fn name() -> Cow<'static, str> {
    |        ---- required by a bound in this associated function

For more information about this error, try `rustc --explain E0277`.
error: could not compile `server` (bin "server") due to 12 previous errors

In 5.0.0-beta I didn't have this problem. BigDecimal is from the https://github.com/akubera/bigdecimal-rs crate, v0.4.6

Thanks in advance!

juhaku commented 1 week ago

This is actually same issue as described here: https://github.com/juhaku/utoipa/issues/1162#issuecomment-2435057054 and here #1078.

Before the types implementing ToSchema could contain fields that does not implement ToSchema trait. However in the release version of utoipa 5.x.x with full support for generics this is a necessary limitation for types. It is required to all fields and variants to implement ToSchema trait if type itself implements ToSchema. This is needed to compose the possible generic schemas and correctly compose the name of the schema.

As described behind the link for the comment and since you have a struct that implements ToSchema you can use either use utoipa-config to define globa aliases for the type. Alternatively you can use value_type = ... attribute over the field to locally alias the type to something else that assumably implements ToSchema trait. With this value_type you can create another a new type as shown below to wrap the BigDecimal and use it instead. With as = ... You can rename it in schema to something else as well if necessary.

#[derive(ToSchema)]
#[schema(value_type  = f64)] // or String, etc
struct MyDecimal(BigDecimal);

Utoipa does not have buildin support for BigDecimal but perhaps it could be evaluated in future if there is need for the support.

francis2tm commented 1 week ago

Got it, thanks a lot!

Cheers