Simplify schema for Options
This commit is contained in:
parent
d7a214526f
commit
13267d37d7
3 changed files with 144 additions and 192 deletions
|
@ -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::<()>());
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,30 +84,22 @@
|
|||
]
|
||||
},
|
||||
"allOf": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/Schema"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
"type": [
|
||||
"array",
|
||||
"null"
|
||||
],
|
||||
"items": {
|
||||
"$ref": "#/definitions/Schema"
|
||||
}
|
||||
},
|
||||
"anyOf": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/Schema"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
"type": [
|
||||
"array",
|
||||
"null"
|
||||
],
|
||||
"items": {
|
||||
"$ref": "#/definitions/Schema"
|
||||
}
|
||||
},
|
||||
"const": true,
|
||||
"contains": {
|
||||
|
@ -135,13 +119,9 @@
|
|||
}
|
||||
},
|
||||
"description": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
"type": [
|
||||
"string",
|
||||
"null"
|
||||
]
|
||||
},
|
||||
"else": {
|
||||
|
@ -155,46 +135,30 @@
|
|||
]
|
||||
},
|
||||
"enum": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "array",
|
||||
"items": true
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
"type": [
|
||||
"array",
|
||||
"null"
|
||||
],
|
||||
"items": true
|
||||
},
|
||||
"exclusiveMaximum": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "number",
|
||||
"format": "double"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
"type": [
|
||||
"number",
|
||||
"null"
|
||||
],
|
||||
"format": "double"
|
||||
},
|
||||
"exclusiveMinimum": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "number",
|
||||
"format": "double"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
"type": [
|
||||
"number",
|
||||
"null"
|
||||
],
|
||||
"format": "double"
|
||||
},
|
||||
"format": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
"type": [
|
||||
"string",
|
||||
"null"
|
||||
]
|
||||
},
|
||||
"if": {
|
||||
|
@ -218,103 +182,67 @@
|
|||
]
|
||||
},
|
||||
"maxItems": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "integer",
|
||||
"format": "uint32"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
"type": [
|
||||
"integer",
|
||||
"null"
|
||||
],
|
||||
"format": "uint32"
|
||||
},
|
||||
"maxLength": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "integer",
|
||||
"format": "uint32"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
"type": [
|
||||
"integer",
|
||||
"null"
|
||||
],
|
||||
"format": "uint32"
|
||||
},
|
||||
"maxProperties": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "integer",
|
||||
"format": "uint32"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
"type": [
|
||||
"integer",
|
||||
"null"
|
||||
],
|
||||
"format": "uint32"
|
||||
},
|
||||
"maximum": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "number",
|
||||
"format": "double"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
"type": [
|
||||
"number",
|
||||
"null"
|
||||
],
|
||||
"format": "double"
|
||||
},
|
||||
"minItems": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "integer",
|
||||
"format": "uint32"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
"type": [
|
||||
"integer",
|
||||
"null"
|
||||
],
|
||||
"format": "uint32"
|
||||
},
|
||||
"minLength": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "integer",
|
||||
"format": "uint32"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
"type": [
|
||||
"integer",
|
||||
"null"
|
||||
],
|
||||
"format": "uint32"
|
||||
},
|
||||
"minProperties": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "integer",
|
||||
"format": "uint32"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
"type": [
|
||||
"integer",
|
||||
"null"
|
||||
],
|
||||
"format": "uint32"
|
||||
},
|
||||
"minimum": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "number",
|
||||
"format": "double"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
"type": [
|
||||
"number",
|
||||
"null"
|
||||
],
|
||||
"format": "double"
|
||||
},
|
||||
"multipleOf": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "number",
|
||||
"format": "double"
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
"type": [
|
||||
"number",
|
||||
"null"
|
||||
],
|
||||
"format": "double"
|
||||
},
|
||||
"not": {
|
||||
"anyOf": [
|
||||
|
@ -327,26 +255,18 @@
|
|||
]
|
||||
},
|
||||
"oneOf": {
|
||||
"anyOf": [
|
||||
{
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/Schema"
|
||||
}
|
||||
},
|
||||
{
|
||||
"type": "null"
|
||||
}
|
||||
]
|
||||
"type": [
|
||||
"array",
|
||||
"null"
|
||||
],
|
||||
"items": {
|
||||
"$ref": "#/definitions/Schema"
|
||||
}
|
||||
},
|
||||
"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"
|
||||
]
|
||||
}
|
||||
},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue