Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gotcha/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ worker = {version = "0.5.0", features = ["http"], optional = true}
thiserror = "1.0"
serde_json = "1"
bigdecimal = "0.4"
rust_decimal = "1.36"
uuid = {version = "1.10" }
regex = "1.7"

Expand Down
18 changes: 18 additions & 0 deletions gotcha/src/openapi/schematic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,24 @@ impl Schematic for BigDecimal {
}
}

impl Schematic for rust_decimal::Decimal {
fn name() -> &'static str {
"decimal"
}

fn required() -> bool {
true
}

fn type_() -> &'static str {
"string"
}

fn format() -> Option<String> {
Some("decimal".to_string())
}
}

impl<T: Schematic> Schematic for HashSet<T> {
fn name() -> &'static str {
T::name()
Expand Down
21 changes: 21 additions & 0 deletions gotcha/tests/pass/openapi/rust_decimal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"schema": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"price": {
"type": "string",
"format": "decimal"
},
"discount": {
"type": "string",
"format": "decimal",
"nullable": true
}
},
"required": ["name", "price"]
},
"required": true
}
23 changes: 23 additions & 0 deletions gotcha/tests/pass/openapi/rust_decimal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use gotcha::openapi::schematic::Schematic;
use gotcha_macro::Schematic;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Schematic)]
struct Product {
name: String,
price: Decimal,
discount: Option<Decimal>,
}

#[test]
fn test_decimal_schema_generation() {
let schema = Product::generate_schema();
let json = serde_json::to_string_pretty(&schema).unwrap();

let expected = include_str!("rust_decimal.json");
assert_json_diff::assert_json_eq!(
serde_json::from_str::<serde_json::Value>(&json).unwrap(),
serde_json::from_str::<serde_json::Value>(expected).unwrap()
);
}
Loading