Update examples

This commit is contained in:
Graham Esau 2019-12-26 20:39:18 +00:00
parent 26346612b5
commit d67abbdbb1
7 changed files with 311 additions and 107 deletions

207
README.md
View file

@ -23,8 +23,10 @@ pub struct MyStruct {
#[derive(JsonSchema)] #[derive(JsonSchema)]
pub enum MyEnum { pub enum MyEnum {
Unit, StringNewType(String),
StringNewType(String) StructVariant {
floats: Vec<f32>,
}
} }
fn main() { fn main() {
@ -38,55 +40,73 @@ fn main() {
```json ```json
{ {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
"title": "MyStruct", "title": "MyStruct",
"type": "object", "type": "object",
"required": [ "required": [
"my_bool", "my_bool",
"my_int", "my_int",
"my_nullable_enum" "my_nullable_enum"
], ],
"properties": { "properties": {
"my_bool": { "my_bool": {
"type": "boolean" "type": "boolean"
},
"my_int": {
"type": "integer",
"format": "int32"
},
"my_nullable_enum": {
"anyOf": [
{
"$ref": "#/definitions/MyEnum"
},
{
"type": "null"
}
]
}
}, },
"definitions": { "my_int": {
"MyEnum": { "type": "integer",
"anyOf": [ "format": "int32"
{ },
"enum": [ "my_nullable_enum": {
"Unit" "anyOf": [
] {
}, "$ref": "#/definitions/MyEnum"
{ },
"type": "object", {
"required": [ "type": "null"
"StringNewType"
],
"properties": {
"StringNewType": {
"type": "string"
}
}
}
]
} }
]
} }
},
"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"
}
}
}
}
}
}
]
}
}
} }
``` ```
</details> </details>
@ -112,15 +132,16 @@ pub struct MyStruct {
#[derive(Deserialize, Serialize, JsonSchema)] #[derive(Deserialize, Serialize, JsonSchema)]
#[serde(untagged)] #[serde(untagged)]
pub enum MyEnum { pub enum MyEnum {
Unit, StringNewType(String),
StringNewType(String) StructVariant {
floats: Vec<f32>,
}
} }
fn main() { fn main() {
let schema = schema_for!(MyStruct); let schema = schema_for!(MyStruct);
println!("{}", serde_json::to_string_pretty(&schema).unwrap()); println!("{}", serde_json::to_string_pretty(&schema).unwrap());
} }
``` ```
<details> <details>
@ -128,45 +149,57 @@ fn main() {
```json ```json
{ {
"$schema": "http://json-schema.org/draft-07/schema#", "$schema": "http://json-schema.org/draft-07/schema#",
"title": "MyStruct", "title": "MyStruct",
"type": "object", "type": "object",
"required": [ "required": [
"myBool", "myBool",
"myNumber" "myNumber"
], ],
"properties": { "properties": {
"myBool": { "myBool": {
"type": "boolean" "type": "boolean"
},
"myNullableEnum": {
"default": null,
"anyOf": [
{
"$ref": "#/definitions/MyEnum"
},
{
"type": "null"
}
]
},
"myNumber": {
"type": "integer",
"format": "int32"
}
}, },
"definitions": { "myNullableEnum": {
"MyEnum": { "default": null,
"anyOf": [ "anyOf": [
{ {
"type": "null" "$ref": "#/definitions/MyEnum"
}, },
{ {
"type": "string" "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"
}
}
}
}
]
}
}
} }
``` ```
</details> </details>

View file

@ -9,8 +9,10 @@ pub struct MyStruct {
#[derive(JsonSchema)] #[derive(JsonSchema)]
pub enum MyEnum { pub enum MyEnum {
Unit,
StringNewType(String), StringNewType(String),
StructVariant {
floats: Vec<f32>,
}
} }
fn main() { fn main() {

View file

@ -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"
}
}
}
}
}
}
]
}
}
}

View file

@ -14,8 +14,10 @@ pub struct MyStruct {
#[derive(Deserialize, Serialize, JsonSchema)] #[derive(Deserialize, Serialize, JsonSchema)]
#[serde(untagged)] #[serde(untagged)]
pub enum MyEnum { pub enum MyEnum {
Unit,
StringNewType(String), StringNewType(String),
StructVariant {
floats: Vec<f32>,
}
} }
fn main() { fn main() {

View file

@ -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"
}
}
}
}
]
}
}
}

View file

@ -17,8 +17,10 @@ pub struct MyStruct {
#[derive(JsonSchema)] #[derive(JsonSchema)]
pub enum MyEnum { pub enum MyEnum {
Unit, StringNewType(String),
StringNewType(String) StructVariant {
floats: Vec<f32>,
}
} }
fn main() { fn main() {
@ -62,11 +64,6 @@ fn main() {
"definitions": { "definitions": {
"MyEnum": { "MyEnum": {
"anyOf": [ "anyOf": [
{
"enum": [
"Unit"
]
},
{ {
"type": "object", "type": "object",
"required": [ "required": [
@ -77,6 +74,29 @@ fn main() {
"type": "string" "type": "string"
} }
} }
},
{
"type": "object",
"required": [
"StructVariant"
],
"properties": {
"StructVariant": {
"type": "object",
"required": [
"floats"
],
"properties": {
"floats": {
"type": "array",
"items": {
"type": "number",
"format": "float"
}
}
}
}
}
} }
] ]
} }
@ -106,15 +126,16 @@ pub struct MyStruct {
#[derive(Deserialize, Serialize, JsonSchema)] #[derive(Deserialize, Serialize, JsonSchema)]
#[serde(untagged)] #[serde(untagged)]
pub enum MyEnum { pub enum MyEnum {
Unit, StringNewType(String),
StringNewType(String) StructVariant {
floats: Vec<f32>,
}
} }
fn main() { fn main() {
let schema = schema_for!(MyStruct); let schema = schema_for!(MyStruct);
println!("{}", serde_json::to_string_pretty(&schema).unwrap()); println!("{}", serde_json::to_string_pretty(&schema).unwrap());
} }
``` ```
<details> <details>
@ -153,10 +174,22 @@ fn main() {
"MyEnum": { "MyEnum": {
"anyOf": [ "anyOf": [
{ {
"type": "null" "type": "string"
}, },
{ {
"type": "string" "type": "object",
"required": [
"floats"
],
"properties": {
"floats": {
"type": "array",
"items": {
"type": "number",
"format": "float"
}
}
}
} }
] ]
} }

12
update-examples.sh Normal file
View file

@ -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