diff --git a/schemars/src/gen.rs b/schemars/src/gen.rs index f8d7b8b..7370d70 100644 --- a/schemars/src/gen.rs +++ b/schemars/src/gen.rs @@ -7,7 +7,7 @@ use std::iter::FromIterator; #[derive(Debug, PartialEq, Clone)] pub struct SchemaSettings { pub option_nullable: bool, - pub option_any_of_null: bool, + pub option_add_null_type: bool, pub bool_schemas: BoolSchemas, pub definitions_path: String, } @@ -23,7 +23,7 @@ impl Default for SchemaSettings { fn default() -> SchemaSettings { SchemaSettings { option_nullable: false, - option_any_of_null: true, + option_add_null_type: true, bool_schemas: BoolSchemas::Enable, definitions_path: "#/definitions/".to_owned(), } @@ -39,7 +39,7 @@ impl SchemaSettings { pub fn openapi3() -> SchemaSettings { SchemaSettings { option_nullable: true, - option_any_of_null: false, + option_add_null_type: false, bool_schemas: BoolSchemas::AdditionalPropertiesOnly, definitions_path: "#/components/schemas/".to_owned(), } diff --git a/schemars/src/make_schema.rs b/schemars/src/make_schema.rs index 68ad085..4bb6137 100644 --- a/schemars/src/make_schema.rs +++ b/schemars/src/make_schema.rs @@ -266,30 +266,23 @@ impl MakeSchema for Option { } fn make_schema(gen: &mut SchemaGenerator) -> Schema { - let settings = gen.settings(); - let make_any_of = settings.option_any_of_null; - let set_nullable = settings.option_nullable; - let schema = match gen.subschema_for::() { - Schema::Bool(true) => true.into(), - Schema::Bool(false) => <()>::make_schema(gen), - schema => { - if make_any_of { - SchemaObject { - any_of: Some(vec![schema, <()>::make_schema(gen)]), - ..Default::default() - } - .into() - } else { - schema + let mut schema = gen.subschema_for::(); + if gen.settings().option_add_null_type { + schema = match schema { + Schema::Bool(true) => Schema::Bool(true), + Schema::Bool(false) => <()>::make_schema(gen), + schema => SchemaObject { + any_of: Some(vec![schema, <()>::make_schema(gen)]), + ..Default::default() } + .into(), } - }; - if set_nullable { - let deref = gen.try_get_schema_object(&schema); + } + if gen.settings().option_nullable { + let mut deref = gen.try_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)); - return Schema::Object(schema); } }; schema