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

feature request: JSON schema support via https://github.com/GREsau/schemars #612

Open
yazshel opened this issue Oct 3, 2023 · 1 comment

Comments

@yazshel
Copy link

yazshel commented Oct 3, 2023

Hi,

I'm keen to use rust-decimal in my rust/rocket web apps which utilise the rocket-okapi, okapi and schemars to produce OpenAPI/Swagger API specifications. Supporting this basically requires adding impls: impl schemars::JsonSchema for rust_decimal::Decimal but will likely require different impls depending on the selected serde feature (eg. providing a JSON string schema with regex for the serde-str option).

I'm open to doing some work on this but thought I'd check in to see if there were any ideas on how best to implement this in a manner consistent with to existing serde support.

One challenge is that schemars doesn't support #[serde(with = <mod>)]-style macros. However, these could be supported through adding dummy "schema" structs for relevant serde features and explicitly referring to these with #[schemars(with = <struct>)] decorators.

For example:
in rust_decimal::schemars.rs:

pub struct DecimalWithAbitraryPrecision {}

impl JsonSchema for DecimalWithAbitraryPrecision {
  <implement JsonSchema functions specific to abitrary_precision feature>
}

Then in relevant application code:

#[derive(Serialize, Deserialize, JsonSchema)]
pub struct MyApiObject {
    ...
    #[serde(with = rust_decimal::serde::abitrary_precision)] 
    #[schemars(with = rust_decimal::schemars::DecimalWithAbitraryPrecision)]
    my_decimal: rust_decimal::Decimal,
    ...
}

This is a bit clunky but was the best I could come up with. Any better ideas about how to implement such support?

@Tony-Samuels
Copy link
Collaborator

We're trying to decide the best way to approach this in general. E.g. #611.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants