GREsau / schemars

Generate JSON Schema documents from Rust code
https://graham.cool/schemars/
MIT License
835 stars 229 forks source link

Can't use `#[serde(with = "rust_decimal::serde::arbitrary_precision")]` when `#[derive(JsonSchema)]` is used #350

Open elipp opened 1 week ago

elipp commented 1 week ago

Consider the following struct:

#[derive(Deserialize, Serialize, JsonSchema)]
struct SomeStruct {
    #[serde(with = "rust_decimal::serde::arbitrary_precision")]
    field: Decimal,
}

Compilation fails with: error[E0573]: expected type, found module rust_decimal::serde::arbitrary_precision

How can I make that work? Ideally, I'd like this Decimal field to show up as a JSON number in the spec.

elipp commented 1 week ago

I did try the #[serde_as] macro from the serde_with crate with feature=schemars_0_8 enabled, but couldn't figure out how to make it work.

elipp commented 3 days ago

Ended up just manually impl JsonSchemaing (examples can be found on the schemars docs.)

jikrow commented 23 hours ago

I think adding #[schemars(with = "serde_json::Number"] to field would make it work