From 6cf9343657388d0d2e1c4dbc1fc901e3ff1e9dde Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Sun, 8 Sep 2019 14:20:57 +0100 Subject: [PATCH] Set `format` property on integers --- schemars/src/json_schema_impls/primitives.rs | 35 +++++++++++-------- schemars/tests/expected/enum-external.json | 6 ++-- schemars/tests/expected/enum-internal.json | 13 +++---- schemars/tests/expected/enum-untagged.json | 6 ++-- .../tests/expected/property-name-struct.json | 9 +++-- .../tests/expected/schema-name-custom.json | 3 +- .../tests/expected/schema-name-default.json | 3 +- schemars/tests/expected/struct-newtype.json | 3 +- schemars/tests/expected/struct-normal.json | 3 +- schemars/tests/expected/struct-tuple.json | 3 +- 10 files changed, 52 insertions(+), 32 deletions(-) diff --git a/schemars/src/json_schema_impls/primitives.rs b/schemars/src/json_schema_impls/primitives.rs index 7f5457d..6576737 100644 --- a/schemars/src/json_schema_impls/primitives.rs +++ b/schemars/src/json_schema_impls/primitives.rs @@ -5,6 +5,12 @@ use serde_json::json; macro_rules! simple_impl { ($type:tt => $instance_type:ident) => { + simple_impl!($type => $instance_type, None); + }; + ($type:tt => $instance_type:ident, $format: literal) => { + simple_impl!($type => $instance_type, Some($format.to_owned())); + }; + ($type:tt => $instance_type:ident, $($format:tt)+) => { impl JsonSchema for $type { no_ref_schema!(); @@ -15,6 +21,7 @@ macro_rules! simple_impl { fn json_schema(_: &mut SchemaGenerator) -> Result { Ok(SchemaObject { instance_type: Some(InstanceType::$instance_type.into()), + format: $($format)+, ..Default::default() } .into()) @@ -26,20 +33,20 @@ macro_rules! simple_impl { simple_impl!(str => String); simple_impl!(String => String); simple_impl!(bool => Boolean); -simple_impl!(f32 => Number); -simple_impl!(f64 => Number); -simple_impl!(i8 => Integer); -simple_impl!(i16 => Integer); -simple_impl!(i32 => Integer); -simple_impl!(i64 => Integer); -simple_impl!(i128 => Integer); -simple_impl!(isize => Integer); -simple_impl!(u8 => Integer); -simple_impl!(u16 => Integer); -simple_impl!(u32 => Integer); -simple_impl!(u64 => Integer); -simple_impl!(u128 => Integer); -simple_impl!(usize => Integer); +simple_impl!(f32 => Number, "float"); +simple_impl!(f64 => Number, "double"); +simple_impl!(i8 => Integer, "int8"); +simple_impl!(i16 => Integer, "int16"); +simple_impl!(i32 => Integer, "int32"); +simple_impl!(i64 => Integer, "int64"); +simple_impl!(i128 => Integer, "int128"); +simple_impl!(isize => Integer, "int"); +simple_impl!(u8 => Integer, "uint8"); +simple_impl!(u16 => Integer, "uint16"); +simple_impl!(u32 => Integer, "uint32"); +simple_impl!(u64 => Integer, "uint64"); +simple_impl!(u128 => Integer, "uint128"); +simple_impl!(usize => Integer, "uint"); simple_impl!(() => Null); impl JsonSchema for char { diff --git a/schemars/tests/expected/enum-external.json b/schemars/tests/expected/enum-external.json index 4ce574e..5a3dbb5 100644 --- a/schemars/tests/expected/enum-external.json +++ b/schemars/tests/expected/enum-external.json @@ -32,7 +32,8 @@ "type": "boolean" }, "foo": { - "type": "integer" + "type": "integer", + "format": "int32" } }, "required": [ @@ -52,7 +53,8 @@ "type": "array", "items": [ { - "type": "integer" + "type": "integer", + "format": "int32" }, { "type": "boolean" diff --git a/schemars/tests/expected/enum-internal.json b/schemars/tests/expected/enum-internal.json index 1f3bf6f..06c6148 100644 --- a/schemars/tests/expected/enum-internal.json +++ b/schemars/tests/expected/enum-internal.json @@ -36,17 +36,18 @@ { "type": "object", "properties": { + "bar": { + "type": "boolean" + }, + "foo": { + "type": "integer", + "format": "int32" + }, "typeProperty": { "type": "string", "enum": [ "Struct" ] - }, - "bar": { - "type": "boolean" - }, - "foo": { - "type": "integer" } }, "required": [ diff --git a/schemars/tests/expected/enum-untagged.json b/schemars/tests/expected/enum-untagged.json index 48cbc47..e93c560 100644 --- a/schemars/tests/expected/enum-untagged.json +++ b/schemars/tests/expected/enum-untagged.json @@ -18,7 +18,8 @@ "type": "boolean" }, "foo": { - "type": "integer" + "type": "integer", + "format": "int32" } }, "required": [ @@ -30,7 +31,8 @@ "type": "array", "items": [ { - "type": "integer" + "type": "integer", + "format": "int32" }, { "type": "boolean" diff --git a/schemars/tests/expected/property-name-struct.json b/schemars/tests/expected/property-name-struct.json index e67d782..1aa6e78 100644 --- a/schemars/tests/expected/property-name-struct.json +++ b/schemars/tests/expected/property-name-struct.json @@ -4,13 +4,16 @@ "type": "object", "properties": { "camelCase": { - "type": "integer" + "type": "integer", + "format": "int32" }, "new_name_1": { - "type": "integer" + "type": "integer", + "format": "int32" }, "new_name_2": { - "type": "integer" + "type": "integer", + "format": "int32" } }, "required": [ diff --git a/schemars/tests/expected/schema-name-custom.json b/schemars/tests/expected/schema-name-custom.json index 37d7547..5298969 100644 --- a/schemars/tests/expected/schema-name-custom.json +++ b/schemars/tests/expected/schema-name-custom.json @@ -7,7 +7,8 @@ "$ref": "#/definitions/another-new-name" }, "t": { - "type": "integer" + "type": "integer", + "format": "int32" }, "u": { "type": "null" diff --git a/schemars/tests/expected/schema-name-default.json b/schemars/tests/expected/schema-name-default.json index 4a109c2..88c95a4 100644 --- a/schemars/tests/expected/schema-name-default.json +++ b/schemars/tests/expected/schema-name-default.json @@ -7,7 +7,8 @@ "$ref": "#/definitions/MySimpleStruct" }, "t": { - "type": "integer" + "type": "integer", + "format": "int32" }, "u": { "type": "null" diff --git a/schemars/tests/expected/struct-newtype.json b/schemars/tests/expected/struct-newtype.json index 60a0273..284e47d 100644 --- a/schemars/tests/expected/struct-newtype.json +++ b/schemars/tests/expected/struct-newtype.json @@ -1,5 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", "title": "Newtype", - "type": "integer" + "type": "integer", + "format": "int32" } \ No newline at end of file diff --git a/schemars/tests/expected/struct-normal.json b/schemars/tests/expected/struct-normal.json index f3fdf58..9275eaa 100644 --- a/schemars/tests/expected/struct-normal.json +++ b/schemars/tests/expected/struct-normal.json @@ -7,7 +7,8 @@ "type": "boolean" }, "foo": { - "type": "integer" + "type": "integer", + "format": "int32" } }, "required": [ diff --git a/schemars/tests/expected/struct-tuple.json b/schemars/tests/expected/struct-tuple.json index e3bb6e2..5c4893d 100644 --- a/schemars/tests/expected/struct-tuple.json +++ b/schemars/tests/expected/struct-tuple.json @@ -4,7 +4,8 @@ "type": "array", "items": [ { - "type": "integer" + "type": "integer", + "format": "int32" }, { "type": "boolean"