Refactor nullable schema generation
This commit is contained in:
parent
650c434c1a
commit
48465b51cf
2 changed files with 16 additions and 23 deletions
|
@ -7,7 +7,7 @@ use std::iter::FromIterator;
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub struct SchemaSettings {
|
pub struct SchemaSettings {
|
||||||
pub option_nullable: bool,
|
pub option_nullable: bool,
|
||||||
pub option_any_of_null: bool,
|
pub option_add_null_type: bool,
|
||||||
pub bool_schemas: BoolSchemas,
|
pub bool_schemas: BoolSchemas,
|
||||||
pub definitions_path: String,
|
pub definitions_path: String,
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ impl Default for SchemaSettings {
|
||||||
fn default() -> SchemaSettings {
|
fn default() -> SchemaSettings {
|
||||||
SchemaSettings {
|
SchemaSettings {
|
||||||
option_nullable: false,
|
option_nullable: false,
|
||||||
option_any_of_null: true,
|
option_add_null_type: true,
|
||||||
bool_schemas: BoolSchemas::Enable,
|
bool_schemas: BoolSchemas::Enable,
|
||||||
definitions_path: "#/definitions/".to_owned(),
|
definitions_path: "#/definitions/".to_owned(),
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ impl SchemaSettings {
|
||||||
pub fn openapi3() -> SchemaSettings {
|
pub fn openapi3() -> SchemaSettings {
|
||||||
SchemaSettings {
|
SchemaSettings {
|
||||||
option_nullable: true,
|
option_nullable: true,
|
||||||
option_any_of_null: false,
|
option_add_null_type: false,
|
||||||
bool_schemas: BoolSchemas::AdditionalPropertiesOnly,
|
bool_schemas: BoolSchemas::AdditionalPropertiesOnly,
|
||||||
definitions_path: "#/components/schemas/".to_owned(),
|
definitions_path: "#/components/schemas/".to_owned(),
|
||||||
}
|
}
|
||||||
|
|
|
@ -266,30 +266,23 @@ impl<T: MakeSchema> MakeSchema for Option<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn make_schema(gen: &mut SchemaGenerator) -> Schema {
|
fn make_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||||
let settings = gen.settings();
|
let mut schema = gen.subschema_for::<T>();
|
||||||
let make_any_of = settings.option_any_of_null;
|
if gen.settings().option_add_null_type {
|
||||||
let set_nullable = settings.option_nullable;
|
schema = match schema {
|
||||||
let schema = match gen.subschema_for::<T>() {
|
Schema::Bool(true) => Schema::Bool(true),
|
||||||
Schema::Bool(true) => true.into(),
|
Schema::Bool(false) => <()>::make_schema(gen),
|
||||||
Schema::Bool(false) => <()>::make_schema(gen),
|
schema => SchemaObject {
|
||||||
schema => {
|
any_of: Some(vec![schema, <()>::make_schema(gen)]),
|
||||||
if make_any_of {
|
..Default::default()
|
||||||
SchemaObject {
|
|
||||||
any_of: Some(vec![schema, <()>::make_schema(gen)]),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.into()
|
|
||||||
} else {
|
|
||||||
schema
|
|
||||||
}
|
}
|
||||||
|
.into(),
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
if set_nullable {
|
if gen.settings().option_nullable {
|
||||||
let deref = gen.try_get_schema_object(&schema);
|
let mut deref = gen.try_get_schema_object(&schema);
|
||||||
debug_assert!(deref.is_some(), "Could not get schema object: {:?}", schema);
|
debug_assert!(deref.is_some(), "Could not get schema object: {:?}", schema);
|
||||||
if let Some(mut schema) = deref {
|
if let Some(ref mut schema) = deref {
|
||||||
schema.extensions.insert("nullable".to_owned(), json!(true));
|
schema.extensions.insert("nullable".to_owned(), json!(true));
|
||||||
return Schema::Object(schema);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
schema
|
schema
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue