Fix Option<T> schema generation sometimes producing a schema that is not referenced

This commit is contained in:
Graham Esau 2019-09-03 19:47:39 +01:00
parent 717e3baed9
commit c2e8389e93
2 changed files with 16 additions and 40 deletions

View file

@ -272,9 +272,11 @@ impl<T: JsonSchema> JsonSchema for Option<T> {
}
fn json_schema(gen: &mut SchemaGenerator) -> Result {
// FIXME this may produce a subschema that is not referenced in the final schema,
// e.g. SingleOrVec_For_InstanceType in schema-openapi3.json
let mut schema = gen.subschema_for::<T>()?;
let mut schema = if gen.settings().option_nullable {
T::json_schema(gen)?
} else {
gen.subschema_for::<T>()?
};
if gen.settings().option_add_null_type {
schema = match schema {
Schema::Bool(true) => Schema::Bool(true),

View file

@ -24,6 +24,17 @@
"integer"
]
},
"Ref": {
"type": "object",
"properties": {
"$ref": {
"type": "string"
}
},
"required": [
"$ref"
]
},
"Schema": {
"anyOf": [
{
@ -144,43 +155,6 @@
}
},
"additionalProperties": true
},
"Ref": {
"type": "object",
"properties": {
"$ref": {
"type": "string"
}
},
"required": [
"$ref"
]
},
"SingleOrVec_For_InstanceType": {
"anyOf": [
{
"$ref": "#/components/schemas/InstanceType"
},
{
"type": "array",
"items": {
"$ref": "#/components/schemas/InstanceType"
}
}
]
},
"SingleOrVec_For_Schema": {
"anyOf": [
{
"$ref": "#/components/schemas/Schema"
},
{
"type": "array",
"items": {
"$ref": "#/components/schemas/Schema"
}
}
]
}
}
}