From d67abbdbb1b4026b0507be0d876e2abc7932cca9 Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Thu, 26 Dec 2019 20:39:18 +0000 Subject: [PATCH] Update examples --- README.md | 211 +++++++++++++--------- schemars/examples/main.rs | 6 +- schemars/examples/main.schema.json | 69 +++++++ schemars/examples/serde_attrs.rs | 6 +- schemars/examples/serde_attrs.schema.json | 53 ++++++ schemars/src/lib.rs | 61 +++++-- update-examples.sh | 12 ++ 7 files changed, 311 insertions(+), 107 deletions(-) create mode 100644 schemars/examples/main.schema.json create mode 100644 schemars/examples/serde_attrs.schema.json create mode 100644 update-examples.sh diff --git a/README.md b/README.md index bb0b0e7..7945910 100644 --- a/README.md +++ b/README.md @@ -22,9 +22,11 @@ pub struct MyStruct { } #[derive(JsonSchema)] -pub enum MyEnum { - Unit, - StringNewType(String) +pub enum MyEnum { + StringNewType(String), + StructVariant { + floats: Vec, + } } fn main() { @@ -38,55 +40,73 @@ fn main() { ```json { - "$schema": "http://json-schema.org/draft-07/schema#", - "title": "MyStruct", - "type": "object", - "required": [ - "my_bool", - "my_int", - "my_nullable_enum" - ], - "properties": { - "my_bool": { - "type": "boolean" - }, - "my_int": { - "type": "integer", - "format": "int32" - }, - "my_nullable_enum": { - "anyOf": [ - { - "$ref": "#/definitions/MyEnum" - }, - { - "type": "null" - } - ] - } + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "MyStruct", + "type": "object", + "required": [ + "my_bool", + "my_int", + "my_nullable_enum" + ], + "properties": { + "my_bool": { + "type": "boolean" }, - "definitions": { - "MyEnum": { - "anyOf": [ - { - "enum": [ - "Unit" - ] - }, - { - "type": "object", - "required": [ - "StringNewType" - ], - "properties": { - "StringNewType": { - "type": "string" - } - } - } - ] + "my_int": { + "type": "integer", + "format": "int32" + }, + "my_nullable_enum": { + "anyOf": [ + { + "$ref": "#/definitions/MyEnum" + }, + { + "type": "null" } + ] } + }, + "definitions": { + "MyEnum": { + "anyOf": [ + { + "type": "object", + "required": [ + "StringNewType" + ], + "properties": { + "StringNewType": { + "type": "string" + } + } + }, + { + "type": "object", + "required": [ + "StructVariant" + ], + "properties": { + "StructVariant": { + "type": "object", + "required": [ + "floats" + ], + "properties": { + "floats": { + "type": "array", + "items": { + "type": "number", + "format": "float" + } + } + } + } + } + } + ] + } + } } ``` @@ -111,16 +131,17 @@ pub struct MyStruct { #[derive(Deserialize, Serialize, JsonSchema)] #[serde(untagged)] -pub enum MyEnum { - Unit, - StringNewType(String) +pub enum MyEnum { + StringNewType(String), + StructVariant { + floats: Vec, + } } fn main() { let schema = schema_for!(MyStruct); println!("{}", serde_json::to_string_pretty(&schema).unwrap()); } - ```
@@ -128,45 +149,57 @@ fn main() { ```json { - "$schema": "http://json-schema.org/draft-07/schema#", - "title": "MyStruct", - "type": "object", - "required": [ - "myBool", - "myNumber" - ], - "properties": { - "myBool": { - "type": "boolean" - }, - "myNullableEnum": { - "default": null, - "anyOf": [ - { - "$ref": "#/definitions/MyEnum" - }, - { - "type": "null" - } - ] - }, - "myNumber": { - "type": "integer", - "format": "int32" - } + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "MyStruct", + "type": "object", + "required": [ + "myBool", + "myNumber" + ], + "properties": { + "myBool": { + "type": "boolean" }, - "definitions": { - "MyEnum": { - "anyOf": [ - { - "type": "null" - }, - { - "type": "string" - } - ] + "myNullableEnum": { + "default": null, + "anyOf": [ + { + "$ref": "#/definitions/MyEnum" + }, + { + "type": "null" } + ] + }, + "myNumber": { + "type": "integer", + "format": "int32" } + }, + "definitions": { + "MyEnum": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "object", + "required": [ + "floats" + ], + "properties": { + "floats": { + "type": "array", + "items": { + "type": "number", + "format": "float" + } + } + } + } + ] + } + } } ```
diff --git a/schemars/examples/main.rs b/schemars/examples/main.rs index 851cf0a..8bcc7ed 100644 --- a/schemars/examples/main.rs +++ b/schemars/examples/main.rs @@ -8,9 +8,11 @@ pub struct MyStruct { } #[derive(JsonSchema)] -pub enum MyEnum { - Unit, +pub enum MyEnum { StringNewType(String), + StructVariant { + floats: Vec, + } } fn main() { diff --git a/schemars/examples/main.schema.json b/schemars/examples/main.schema.json new file mode 100644 index 0000000..ec883fd --- /dev/null +++ b/schemars/examples/main.schema.json @@ -0,0 +1,69 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "MyStruct", + "type": "object", + "required": [ + "my_bool", + "my_int", + "my_nullable_enum" + ], + "properties": { + "my_bool": { + "type": "boolean" + }, + "my_int": { + "type": "integer", + "format": "int32" + }, + "my_nullable_enum": { + "anyOf": [ + { + "$ref": "#/definitions/MyEnum" + }, + { + "type": "null" + } + ] + } + }, + "definitions": { + "MyEnum": { + "anyOf": [ + { + "type": "object", + "required": [ + "StringNewType" + ], + "properties": { + "StringNewType": { + "type": "string" + } + } + }, + { + "type": "object", + "required": [ + "StructVariant" + ], + "properties": { + "StructVariant": { + "type": "object", + "required": [ + "floats" + ], + "properties": { + "floats": { + "type": "array", + "items": { + "type": "number", + "format": "float" + } + } + } + } + } + } + ] + } + } +} diff --git a/schemars/examples/serde_attrs.rs b/schemars/examples/serde_attrs.rs index 20b8d0a..cc612e9 100644 --- a/schemars/examples/serde_attrs.rs +++ b/schemars/examples/serde_attrs.rs @@ -13,9 +13,11 @@ pub struct MyStruct { #[derive(Deserialize, Serialize, JsonSchema)] #[serde(untagged)] -pub enum MyEnum { - Unit, +pub enum MyEnum { StringNewType(String), + StructVariant { + floats: Vec, + } } fn main() { diff --git a/schemars/examples/serde_attrs.schema.json b/schemars/examples/serde_attrs.schema.json new file mode 100644 index 0000000..f7e6ac9 --- /dev/null +++ b/schemars/examples/serde_attrs.schema.json @@ -0,0 +1,53 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "MyStruct", + "type": "object", + "required": [ + "myBool", + "myNumber" + ], + "properties": { + "myBool": { + "type": "boolean" + }, + "myNullableEnum": { + "default": null, + "anyOf": [ + { + "$ref": "#/definitions/MyEnum" + }, + { + "type": "null" + } + ] + }, + "myNumber": { + "type": "integer", + "format": "int32" + } + }, + "definitions": { + "MyEnum": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "object", + "required": [ + "floats" + ], + "properties": { + "floats": { + "type": "array", + "items": { + "type": "number", + "format": "float" + } + } + } + } + ] + } + } +} diff --git a/schemars/src/lib.rs b/schemars/src/lib.rs index 8656137..0a2e4dc 100644 --- a/schemars/src/lib.rs +++ b/schemars/src/lib.rs @@ -16,9 +16,11 @@ pub struct MyStruct { } #[derive(JsonSchema)] -pub enum MyEnum { - Unit, - StringNewType(String) +pub enum MyEnum { + StringNewType(String), + StructVariant { + floats: Vec, + } } fn main() { @@ -62,11 +64,6 @@ fn main() { "definitions": { "MyEnum": { "anyOf": [ - { - "enum": [ - "Unit" - ] - }, { "type": "object", "required": [ @@ -77,6 +74,29 @@ fn main() { "type": "string" } } + }, + { + "type": "object", + "required": [ + "StructVariant" + ], + "properties": { + "StructVariant": { + "type": "object", + "required": [ + "floats" + ], + "properties": { + "floats": { + "type": "array", + "items": { + "type": "number", + "format": "float" + } + } + } + } + } } ] } @@ -105,16 +125,17 @@ pub struct MyStruct { #[derive(Deserialize, Serialize, JsonSchema)] #[serde(untagged)] -pub enum MyEnum { - Unit, - StringNewType(String) +pub enum MyEnum { + StringNewType(String), + StructVariant { + floats: Vec, + } } fn main() { let schema = schema_for!(MyStruct); println!("{}", serde_json::to_string_pretty(&schema).unwrap()); } - ```
@@ -153,10 +174,22 @@ fn main() { "MyEnum": { "anyOf": [ { - "type": "null" + "type": "string" }, { - "type": "string" + "type": "object", + "required": [ + "floats" + ], + "properties": { + "floats": { + "type": "array", + "items": { + "type": "number", + "format": "float" + } + } + } } ] } diff --git a/update-examples.sh b/update-examples.sh new file mode 100644 index 0000000..bd77355 --- /dev/null +++ b/update-examples.sh @@ -0,0 +1,12 @@ +#!/bin/bash +set -euxo pipefail + +cd schemars/examples + +rm -f *.schema.json + +for file in *.rs +do + example=${file%.rs} + cargo run --example "$example" > "$example.schema.json" +done