Fix schemas for bigdecimal/rust_decimal (#248)
Both of these crates serialise decimals as strings, not as plain numbers
This commit is contained in:
parent
a5e51b22b3
commit
db1dd47039
5 changed files with 34 additions and 5 deletions
|
@ -120,5 +120,9 @@ required-features = ["smol_str"]
|
||||||
name = "semver"
|
name = "semver"
|
||||||
required-features = ["semver"]
|
required-features = ["semver"]
|
||||||
|
|
||||||
|
[[test]]
|
||||||
|
name = "decimal"
|
||||||
|
required-features = ["rust_decimal", "bigdecimal"]
|
||||||
|
|
||||||
[package.metadata.docs.rs]
|
[package.metadata.docs.rs]
|
||||||
all-features = true
|
all-features = true
|
||||||
|
|
|
@ -4,19 +4,20 @@ use crate::JsonSchema;
|
||||||
|
|
||||||
macro_rules! decimal_impl {
|
macro_rules! decimal_impl {
|
||||||
($type:ty) => {
|
($type:ty) => {
|
||||||
decimal_impl!($type => Number, "Number");
|
|
||||||
};
|
|
||||||
($type:ty => $instance_type:ident, $name:expr) => {
|
|
||||||
impl JsonSchema for $type {
|
impl JsonSchema for $type {
|
||||||
no_ref_schema!();
|
no_ref_schema!();
|
||||||
|
|
||||||
fn schema_name() -> String {
|
fn schema_name() -> String {
|
||||||
$name.to_owned()
|
"Decimal".to_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
fn json_schema(_: &mut SchemaGenerator) -> Schema {
|
||||||
SchemaObject {
|
SchemaObject {
|
||||||
instance_type: Some(InstanceType::$instance_type.into()),
|
instance_type: Some(InstanceType::String.into()),
|
||||||
|
string: Some(Box::new(StringValidation {
|
||||||
|
pattern: Some(r"^-?[0-9]+(\.[0-9]+)?$".to_owned()),
|
||||||
|
..Default::default()
|
||||||
|
})),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
.into()
|
.into()
|
||||||
|
|
12
schemars/tests/decimal.rs
Normal file
12
schemars/tests/decimal.rs
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
mod util;
|
||||||
|
use util::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn rust_decimal() -> TestResult {
|
||||||
|
test_default_generated_schema::<rust_decimal::Decimal>("rust_decimal")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn bigdecimal() -> TestResult {
|
||||||
|
test_default_generated_schema::<bigdecimal::BigDecimal>("bigdecimal")
|
||||||
|
}
|
6
schemars/tests/expected/bigdecimal.json
Normal file
6
schemars/tests/expected/bigdecimal.json
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
|
"title": "Decimal",
|
||||||
|
"type": "string",
|
||||||
|
"pattern": "^-?[0-9]+(\\.[0-9]+)?$"
|
||||||
|
}
|
6
schemars/tests/expected/rust_decimal.json
Normal file
6
schemars/tests/expected/rust_decimal.json
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
|
"title": "Decimal",
|
||||||
|
"type": "string",
|
||||||
|
"pattern": "^-?[0-9]+(\\.[0-9]+)?$"
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue