From 50f00be97b341af1fc195c8dfcef4464b25b0408 Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Tue, 29 Oct 2019 21:40:50 +0000 Subject: [PATCH] Implement JsonSchema for nonzero signed ints --- schemars/src/json_schema_impls/mod.rs | 2 ++ .../src/json_schema_impls/nonzero_signed.rs | 34 +++++++++++++++++++ .../src/json_schema_impls/nonzero_unsigned.rs | 2 +- schemars/src/json_schema_impls/serdejson.rs | 2 +- schemars/tests/expected/nonzero_ints.json | 33 ++++++++++++++++++ schemars/tests/nonzero_ints.rs | 21 ++++++++++++ 6 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 schemars/src/json_schema_impls/nonzero_signed.rs create mode 100644 schemars/tests/expected/nonzero_ints.json create mode 100644 schemars/tests/nonzero_ints.rs diff --git a/schemars/src/json_schema_impls/mod.rs b/schemars/src/json_schema_impls/mod.rs index a505ecd..8db5fd6 100644 --- a/schemars/src/json_schema_impls/mod.rs +++ b/schemars/src/json_schema_impls/mod.rs @@ -39,6 +39,8 @@ mod chrono; mod core; mod ffi; mod maps; +#[cfg(num_nonzero_signed)] +mod nonzero_signed; mod nonzero_unsigned; mod primitives; mod sequences; diff --git a/schemars/src/json_schema_impls/nonzero_signed.rs b/schemars/src/json_schema_impls/nonzero_signed.rs new file mode 100644 index 0000000..00bb63f --- /dev/null +++ b/schemars/src/json_schema_impls/nonzero_signed.rs @@ -0,0 +1,34 @@ +use crate::gen::SchemaGenerator; +use crate::schema::*; +use crate::JsonSchema; +use std::num::*; + +macro_rules! nonzero_unsigned_impl { + ($type:ty => $primitive:ty) => { + impl JsonSchema for $type { + no_ref_schema!(); + + fn schema_name() -> String { + stringify!($type).to_owned() + } + + fn json_schema(gen: &mut SchemaGenerator) -> Schema { + let zero_schema: Schema = SchemaObject { + const_value: Some(0.into()), + ..Default::default() + } + .into(); + let mut schema: SchemaObject = <$primitive>::json_schema(gen).into(); + schema.subschemas().not = Some(Box::from(zero_schema)); + schema.into() + } + } + }; +} + +nonzero_unsigned_impl!(NonZeroI8 => i8); +nonzero_unsigned_impl!(NonZeroI16 => i16); +nonzero_unsigned_impl!(NonZeroI32 => i32); +nonzero_unsigned_impl!(NonZeroI64 => i64); +nonzero_unsigned_impl!(NonZeroI128 => i128); +nonzero_unsigned_impl!(NonZeroIsize => isize); diff --git a/schemars/src/json_schema_impls/nonzero_unsigned.rs b/schemars/src/json_schema_impls/nonzero_unsigned.rs index ded43f3..284ac8a 100644 --- a/schemars/src/json_schema_impls/nonzero_unsigned.rs +++ b/schemars/src/json_schema_impls/nonzero_unsigned.rs @@ -35,7 +35,7 @@ mod tests { use pretty_assertions::assert_eq; #[test] - fn schema_for_atomics() { + fn schema_for_nonzero_u32() { let schema = schema_object_for::(); assert_eq!(schema.number.unwrap().minimum, Some(1.0)); assert_eq!(schema.instance_type, Some(InstanceType::Integer.into())); diff --git a/schemars/src/json_schema_impls/serdejson.rs b/schemars/src/json_schema_impls/serdejson.rs index e07ee83..17f714c 100644 --- a/schemars/src/json_schema_impls/serdejson.rs +++ b/schemars/src/json_schema_impls/serdejson.rs @@ -16,7 +16,7 @@ impl JsonSchema for Value { } } -forward_impl!(Map => BTreeMap::); +forward_impl!(Map => BTreeMap); impl JsonSchema for Number { no_ref_schema!(); diff --git a/schemars/tests/expected/nonzero_ints.json b/schemars/tests/expected/nonzero_ints.json new file mode 100644 index 0000000..39bd869 --- /dev/null +++ b/schemars/tests/expected/nonzero_ints.json @@ -0,0 +1,33 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "MyStruct", + "type": "object", + "required": [ + "nonzero_signed", + "nonzero_unsigned", + "signed", + "unsigned" + ], + "properties": { + "nonzero_signed": { + "type": "integer", + "format": "int32", + "not": { + "const": 0 + } + }, + "nonzero_unsigned": { + "type": "integer", + "format": "uint32", + "minimum": 1.0 + }, + "signed": { + "type": "integer", + "format": "int32" + }, + "unsigned": { + "type": "integer", + "format": "uint32" + } + } +} \ No newline at end of file diff --git a/schemars/tests/nonzero_ints.rs b/schemars/tests/nonzero_ints.rs new file mode 100644 index 0000000..42460d9 --- /dev/null +++ b/schemars/tests/nonzero_ints.rs @@ -0,0 +1,21 @@ +mod util; + +#[cfg(num_nonzero_signed)] +mod nonzero_ints { + use super::*; + use schemars::JsonSchema; + use util::*; + + #[derive(Debug, JsonSchema)] + struct MyStruct { + unsigned: u32, + nonzero_unsigned: std::num::NonZeroU32, + signed: i32, + nonzero_signed: std::num::NonZeroI32, + } + + #[test] + fn nonzero_ints() -> TestResult { + test_default_generated_schema::("nonzero_ints") + } +}