Simplify schema for Options

This commit is contained in:
Graham Esau 2019-09-14 22:44:05 +01:00
parent d7a214526f
commit 13267d37d7
3 changed files with 144 additions and 192 deletions

View file

@ -20,6 +20,12 @@ impl<T: JsonSchema> JsonSchema for Option<T> {
schema = match schema {
Schema::Bool(true) => Schema::Bool(true),
Schema::Bool(false) => <()>::json_schema(gen)?,
Schema::Object(
obj @ SchemaObject {
instance_type: Some(_),
..
},
) => Schema::Object(with_null_type(obj)),
schema => SchemaObject {
any_of: Some(vec![schema, <()>::json_schema(gen)?]),
..Default::default()
@ -36,6 +42,20 @@ impl<T: JsonSchema> JsonSchema for Option<T> {
}
}
fn with_null_type(mut obj: SchemaObject) -> SchemaObject {
match obj
.instance_type
.as_mut()
.expect("checked in calling function")
{
SingleOrVec::Single(ty) if **ty == InstanceType::Null => {}
SingleOrVec::Vec(ty) if ty.contains(&InstanceType::Null) => {}
SingleOrVec::Single(ty) => obj.instance_type = Some(vec![**ty, InstanceType::Null].into()),
SingleOrVec::Vec(ref mut ty) => ty.push(InstanceType::Null),
};
obj
}
impl<T: ?Sized> JsonSchema for std::marker::PhantomData<T> {
no_ref_schema!();
@ -70,12 +90,32 @@ mod tests {
#[test]
fn schema_for_option() {
let schema = schema_object_for::<Option<i32>>();
assert_eq!(
schema.instance_type,
Some(vec![InstanceType::Integer, InstanceType::Null].into())
);
assert_eq!(schema.extensions.get("nullable"), None);
assert_eq!(schema.any_of.is_none(), true);
}
#[test]
fn schema_for_option_with_ref() {
use crate as schemars;
#[derive(JsonSchema)]
struct Foo;
let schema = schema_object_for::<Option<Foo>>();
assert_eq!(schema.instance_type, None);
assert_eq!(schema.extensions.get("nullable"), None);
assert_eq!(schema.any_of.is_some(), true);
let any_of = schema.any_of.unwrap();
assert_eq!(any_of.len(), 2);
assert_eq!(any_of[0], schema_for::<i32>());
assert_eq!(
any_of[0],
Schema::Ref(Ref {
reference: "#/definitions/Foo".to_string()
})
);
assert_eq!(any_of[1], schema_for::<()>());
}

View file

@ -144,7 +144,7 @@ pub struct ObjectValidation {
pub property_names: Option<Box<Schema>>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub enum InstanceType {
Null,

View file

@ -52,23 +52,15 @@
"type": "object",
"properties": {
"$id": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
"type": [
"string",
"null"
]
},
"$schema": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
"type": [
"string",
"null"
]
},
"additionalItems": {
@ -92,31 +84,23 @@
]
},
"allOf": {
"anyOf": [
{
"type": "array",
"type": [
"array",
"null"
],
"items": {
"$ref": "#/definitions/Schema"
}
},
{
"type": "null"
}
]
},
"anyOf": {
"anyOf": [
{
"type": "array",
"type": [
"array",
"null"
],
"items": {
"$ref": "#/definitions/Schema"
}
},
{
"type": "null"
}
]
},
"const": true,
"contains": {
"anyOf": [
@ -135,13 +119,9 @@
}
},
"description": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
"type": [
"string",
"null"
]
},
"else": {
@ -155,46 +135,30 @@
]
},
"enum": {
"anyOf": [
{
"type": "array",
"type": [
"array",
"null"
],
"items": true
},
{
"type": "null"
}
]
},
"exclusiveMaximum": {
"anyOf": [
{
"type": "number",
"type": [
"number",
"null"
],
"format": "double"
},
{
"type": "null"
}
]
},
"exclusiveMinimum": {
"anyOf": [
{
"type": "number",
"type": [
"number",
"null"
],
"format": "double"
},
{
"type": "null"
}
]
},
"format": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
"type": [
"string",
"null"
]
},
"if": {
@ -218,104 +182,68 @@
]
},
"maxItems": {
"anyOf": [
{
"type": "integer",
"type": [
"integer",
"null"
],
"format": "uint32"
},
{
"type": "null"
}
]
},
"maxLength": {
"anyOf": [
{
"type": "integer",
"type": [
"integer",
"null"
],
"format": "uint32"
},
{
"type": "null"
}
]
},
"maxProperties": {
"anyOf": [
{
"type": "integer",
"type": [
"integer",
"null"
],
"format": "uint32"
},
{
"type": "null"
}
]
},
"maximum": {
"anyOf": [
{
"type": "number",
"type": [
"number",
"null"
],
"format": "double"
},
{
"type": "null"
}
]
},
"minItems": {
"anyOf": [
{
"type": "integer",
"type": [
"integer",
"null"
],
"format": "uint32"
},
{
"type": "null"
}
]
},
"minLength": {
"anyOf": [
{
"type": "integer",
"type": [
"integer",
"null"
],
"format": "uint32"
},
{
"type": "null"
}
]
},
"minProperties": {
"anyOf": [
{
"type": "integer",
"type": [
"integer",
"null"
],
"format": "uint32"
},
{
"type": "null"
}
]
},
"minimum": {
"anyOf": [
{
"type": "number",
"type": [
"number",
"null"
],
"format": "double"
},
{
"type": "null"
}
]
},
"multipleOf": {
"anyOf": [
{
"type": "number",
"type": [
"number",
"null"
],
"format": "double"
},
{
"type": "null"
}
]
},
"not": {
"anyOf": [
{
@ -327,26 +255,18 @@
]
},
"oneOf": {
"anyOf": [
{
"type": "array",
"type": [
"array",
"null"
],
"items": {
"$ref": "#/definitions/Schema"
}
},
{
"type": "null"
}
]
},
"pattern": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
"type": [
"string",
"null"
]
},
"patternProperties": {
@ -388,13 +308,9 @@
]
},
"title": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
"type": [
"string",
"null"
]
},
"type": {
@ -408,13 +324,9 @@
]
},
"uniqueItems": {
"anyOf": [
{
"type": "boolean"
},
{
"type": "null"
}
"type": [
"boolean",
"null"
]
}
},