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

Add support for rust_decimal and bigdecimal #101

Merged
merged 2 commits into from
Nov 25, 2021
Merged

Add support for rust_decimal and bigdecimal #101

merged 2 commits into from
Nov 25, 2021

Conversation

timando
Copy link
Contributor

@timando timando commented Aug 27, 2021

No description provided.

@rudolphfroger-ds
Copy link

Would be really nice to get this merged!

@rudolphfroger-ds
Copy link

Is there anything I can do to help get support for decimal support?

@GREsau GREsau merged commit f0d2b1c into GREsau:master Nov 25, 2021
@GREsau
Copy link
Owner

GREsau commented Nov 25, 2021

Thanks, this is now published in v0.8.8 :)

@ghazalrashno
Copy link

HI,
I have updated to 0.8.8 but still get the error

Cargo.toml:


[dependencies]
bigdecimal = { version = "0.1.2", features = ["serde"] }
chrono = { version = "0.4.19", default-features = false, features = ["clock", "std", "serde"] }
diesel = { version = "1.4.8", features = ["postgres", "r2d2", "chrono", "numeric"] }
diesel-derive-enum = { version = "1.1.1", features = ["postgres"] }
dotenv = "0.15.0"
r2d2 = "0.8.9"
rocket = { version = "0.5.0-rc.1", features = ["json"] }
rocket_okapi = { version = "0.8.0-rc.1", features = ["swagger", "rapidoc"] }
rocket_sync_db_pools = { version = "0.1.0-rc.1", features = ["diesel_postgres_pool"] }
schemars = { version = "0.8.8", features = ["chrono", "bigdecimal"] }


[features]

My model:

use rocket::serde::{Serialize, Deserialize};
// use rocket_okapi::okapi::schemars;
// use rocket_okapi::okapi::schemars::JsonSchema; 
use chrono::{ NaiveDateTime };
use bigdecimal::BigDecimal;
use schemars::JsonSchema;

use crate::schema::*;  

#[derive( Debug, Deserialize, Serialize, AsChangeset, JsonSchema, Queryable, Associations)]
#[serde(crate = "rocket::serde")] 
#[table_name="products"]
pub struct Product {
    pub id: i32,
    
    pub name: String,
    pub name_mobile: Option<String>,  
    pub price: BigDecimal,

    pub size_id: Option<i32>,
    pub standard_id:Option<i32>,
    pub weight_id :Option<i32>,
    pub width_id :Option<i32>,
    pub diameter_id :Option<i32>,
    pub thickness_id :Option<i32>,
    pub factory_id :Option<i32>,
    pub loading_id :Option<i32>,

    pub created_at: NaiveDateTime,
    pub updated_at: NaiveDateTime,
}

Stack trace:


error[E0277]: the trait bound `BigDecimal: JsonSchema` is not satisfied
  --> src/api/model.rs:20:16
   |
20 |     pub price: BigDecimal,
   |                ^^^^^^^^^^ the trait `JsonSchema` is not implemented for `BigDecimal`

error[E0277]: the trait bound `BigDecimal: JsonSchema` is not satisfied
   --> src/api/model.rs:12:55
    |
12  | #[derive( Debug, Deserialize, Serialize, AsChangeset, JsonSchema, Queryable, Associations)]
    |                                                       ^^^^^^^^^^ the trait `JsonSchema` is not implemented for `BigDecimal`
    |
note: required by `_schemars_private_is_option`
   --> /Users/amin/.cargo/registry/src/github.com-1ecc6299db9ec823/schemars-0.8.8/src/lib.rs:382:5
    |
382 |     fn _schemars_private_is_option() -> bool {
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: this error originates in the derive macro `JsonSchema` (in Nightly builds, run with -Z macro-backtrace for more info)

did i miss something or it is just not published publicly?

@GREsau
Copy link
Owner

GREsau commented Nov 26, 2021

You're referencing bigdecimal 0.1.2, but schemars only references 0.3 which is not semver-compatible with 0.1.2

If possible, update your reference of bigdecimal to 0.3

@timando timando deleted the decimal branch November 28, 2021 23:17
sanlee42 added a commit to sanlee42/schemars that referenced this pull request Jan 2, 2023
* Read #[validate(...)] attributes

* Handle required flattened Option fields

* Refactor out `add_schema_as_property`

* Process validation attributes in newtype structs

* Process validation attributes in tuple structs

* Refactor out "local_id" for type definitions

* Refactoring

* Support inline regex

* Allow setting validation attributes via #[schemars(...)]

* Add some doc comments

* Fix doc test

* Emit compilation errors for duplicate validation attributes

* Fix indexmap tests for rust 1.37

* upgrade diem dep (#1)

* Update changelog and docs

* Add newline to attributes docs

* v0.8.4

* Allow empty #[validate] attributes.

Fixes GREsau#109

* v0.8.5

* Use oneOf for enums when possible (GREsau#108)

* v0.8.6

* Correct latest changelog entry

* update diem dep

* Implement JsonSchema on EnumSet type

* update diem dep

* Upgrade move deps (#3)

* [deps] Upgrade move types dep.

* Update examples after 0a1200b

* Allow non-Serialize default values.

Default values that don't implement Serialize are now ignored, rather than causing a compile error.
This is done by simulating specialization using a technique copied from Rocket:
https://github.com/SergioBenitez/Rocket/blob/5ebefa97c992c37bdc476299304a339d429a43fc/core/lib/src/sentinel.rs#L391-L445

Fixes GREsau#115

* v0.8.7

* update diem deps

* Add example for optional dependency in readme

Based on https://github.com/GREsau/schemars/pull/118/files

* Add support for rust_decimal and bigdecimal (GREsau#101)

* Document new optional dependencies

* Internally tagged enums don't honor deny_unknown_fields as precisely as
they might.

flatten doesn't act quite as intended with regard to
additional_properties

* v0.8.8

* update diem deps to latest

* update diem deps

* Update dep

* update diem deps

* Update rust toolchain and dep

* update diem dep

* Update diem dep

* [crypto] Update dependency crypto to use starcoinorg/starcoin-crypto repo (#5)

* [crypto] Update dependency crypto to use starcoinorg/starcoin-crypto repo.

* [deps] Update move-core-types to starcoinorg/move

* Remove diem types

Co-authored-by: Graham Esau <gesau@hotmail.co.uk>
Co-authored-by: lerencao <funfriendcjf@gmail.com>
Co-authored-by: Graham Esau <graham.esau@vonage.com>
Co-authored-by: Adam Leventhal <adam.leventhal@gmail.com>
Co-authored-by: Matt Campbell <mattcampbell@pobox.com>
Co-authored-by: jolestar <jolestar@gmail.com>
Co-authored-by: timando <github@timando.net>
Co-authored-by: Adam H. Leventhal <ahl@oxide.computer>
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

Successfully merging this pull request may close these issues.

None yet

4 participants