Make Option<T> fields optional in generated schemas (#16)

This commit is contained in:
Graham Esau 2020-02-29 19:37:20 +00:00 committed by GitHub
parent 60284fdf93
commit 4ad5000232
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 269 additions and 148 deletions

View file

@ -19,9 +19,6 @@
"properties": {
"Complex": {
"type": "object",
"required": [
"my_nullable_string"
],
"properties": {
"my_nullable_string": {
"title": "A nullable string",

View file

@ -0,0 +1,34 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Flat",
"type": "object",
"required": [
"b",
"f",
"s",
"v"
],
"properties": {
"b": {
"type": "boolean"
},
"f": {
"type": "number",
"format": "float"
},
"os": {
"default": "",
"type": "string"
},
"s": {
"type": "string"
},
"v": {
"type": "array",
"items": {
"type": "integer",
"format": "int32"
}
}
}
}

View file

@ -10,6 +10,12 @@
"bar": {
"type": "boolean"
},
"baz": {
"type": [
"string",
"null"
]
},
"foo": {
"type": "integer",
"format": "int32"

View file

@ -9,8 +9,14 @@
},
{
"type": "boolean"
},
{
"type": [
"string",
"null"
]
}
],
"maxItems": 2,
"minItems": 2
"maxItems": 3,
"minItems": 3
}

View file

@ -1,6 +1,6 @@
mod util;
use pretty_assertions::assert_eq;
use schemars::{schema_for, JsonSchema};
use schemars::JsonSchema;
use util::*;
#[derive(Debug, JsonSchema)]
struct Flat {
@ -43,8 +43,11 @@ struct Deep4 {
}
#[test]
fn flatten_schema() {
let flat = schema_for!(Flat);
let deep = schema_for!(Deep1);
assert_eq!(flat, deep);
fn test_flat_schema() -> TestResult {
test_default_generated_schema::<Flat>("flatten")
}
#[test]
fn test_flattened_schema() -> TestResult {
test_default_generated_schema::<Deep1>("flatten")
}

View file

@ -6,6 +6,7 @@ use util::*;
pub struct Struct {
foo: i32,
bar: bool,
baz: Option<String>,
}
#[test]
@ -14,7 +15,7 @@ fn struct_normal() -> TestResult {
}
#[derive(Debug, JsonSchema)]
pub struct Tuple(i32, bool);
pub struct Tuple(i32, bool, Option<String>);
#[test]
fn struct_tuple() -> TestResult {