diff --git a/schemars/examples/custom_settings.rs b/schemars/examples/custom_settings.rs index 8c90980..335daf3 100644 --- a/schemars/examples/custom_settings.rs +++ b/schemars/examples/custom_settings.rs @@ -8,19 +8,16 @@ pub struct MyStruct { } #[derive(JsonSchema)] -pub enum MyEnum { +pub enum MyEnum { StringNewType(String), - StructVariant { - floats: Vec, - } + StructVariant { floats: Vec }, } fn main() { - let settings = SchemaSettings { - option_nullable: true, - option_add_null_type: false, - ..SchemaSettings::draft07() - }; + let settings = SchemaSettings::draft07().with(|s| { + s.option_nullable = true; + s.option_add_null_type = false; + }); let gen = settings.into_generator(); let schema = gen.into_root_schema_for::(); println!("{}", serde_json::to_string_pretty(&schema).unwrap()); diff --git a/schemars/src/gen.rs b/schemars/src/gen.rs index 169413b..8902d6e 100644 --- a/schemars/src/gen.rs +++ b/schemars/src/gen.rs @@ -2,7 +2,7 @@ use crate::schema::*; use crate::{JsonSchema, Map}; /// Settings to customize how Schemas are generated. -/// +/// /// The default settings currently conform to [JSON Schema Draft 7](https://json-schema.org/specification-links.html#draft-7), but this is liable to change in a future version of Schemars if support for other JSON Schema versions is added. /// If you require your generated schemas to conform to draft 7, consider using the [`draft07`](#method.draft07) method. #[derive(Debug, PartialEq, Clone)] @@ -29,6 +29,7 @@ pub struct SchemaSettings { /// /// Defaults to `"http://json-schema.org/draft-07/schema#"`. pub meta_schema: Option, + _hidden: (), } /// Controls whether trivial [`Bool`](../schema/enum.Schema.html#variant.Bool) schemas may be generated. @@ -57,6 +58,7 @@ impl SchemaSettings { bool_schemas: BoolSchemas::Enabled, definitions_path: "#/definitions/".to_owned(), meta_schema: Some("http://json-schema.org/draft-07/schema#".to_owned()), + _hidden: (), } } @@ -71,9 +73,27 @@ impl SchemaSettings { "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema" .to_owned(), ), + _hidden: (), } } + /// Modifies the `SchemaSettings` by calling the given function. + /// + /// # Example + /// ``` + /// use schemars::gen::{SchemaGenerator, SchemaSettings}; + /// + /// let settings = SchemaSettings::default().with(|s| { + /// s.option_nullable = true; + /// s.option_add_null_type = false; + /// }); + /// let gen = settings.into_generator(); + /// ``` + pub fn with(mut self, configure_fn: impl FnOnce(&mut Self)) -> Self { + configure_fn(&mut self); + self + } + /// Creates a new [`SchemaGenerator`] using these settings. pub fn into_generator(self) -> SchemaGenerator { SchemaGenerator::new(self) @@ -100,6 +120,12 @@ pub struct SchemaGenerator { definitions: Map, } +impl From for SchemaGenerator { + fn from(settings: SchemaSettings) -> Self { + settings.into_generator() + } +} + impl SchemaGenerator { /// Creates a new `SchemaGenerator` using the given settings. pub fn new(settings: SchemaSettings) -> SchemaGenerator { diff --git a/schemars/src/json_schema_impls/core.rs b/schemars/src/json_schema_impls/core.rs index 209f8ae..c9dd35d 100644 --- a/schemars/src/json_schema_impls/core.rs +++ b/schemars/src/json_schema_impls/core.rs @@ -199,11 +199,10 @@ mod tests { #[test] fn schema_for_option_with_nullable() { - let settings = SchemaSettings { - option_nullable: true, - option_add_null_type: false, - ..Default::default() - }; + let settings = SchemaSettings::default().with(|s| { + s.option_nullable = true; + s.option_add_null_type = false; + }); let schema = custom_schema_object_for::>(settings); assert_eq!( schema.instance_type, diff --git a/schemars/src/json_schema_impls/maps.rs b/schemars/src/json_schema_impls/maps.rs index 73f3313..719cc9e 100644 --- a/schemars/src/json_schema_impls/maps.rs +++ b/schemars/src/json_schema_impls/maps.rs @@ -52,10 +52,7 @@ mod tests { #[test] fn schema_for_map_any_value() { for bool_schemas in &[BoolSchemas::Enabled, BoolSchemas::AdditionalPropertiesOnly] { - let settings = SchemaSettings { - bool_schemas: *bool_schemas, - ..Default::default() - }; + let settings = SchemaSettings::default().with(|s| s.bool_schemas = *bool_schemas); let schema = custom_schema_object_for::>(settings); assert_eq!( schema.instance_type, @@ -72,10 +69,8 @@ mod tests { #[test] fn schema_for_map_any_value_no_bool_schema() { - let settings = SchemaSettings { - bool_schemas: BoolSchemas::Disabled, - ..Default::default() - }; + let settings = + SchemaSettings::default().with(|s| s.bool_schemas = BoolSchemas::Disabled); let schema = custom_schema_object_for::>(settings); assert_eq!( schema.instance_type, @@ -96,10 +91,7 @@ mod tests { BoolSchemas::Disabled, BoolSchemas::AdditionalPropertiesOnly, ] { - let settings = SchemaSettings { - bool_schemas: *bool_schemas, - ..Default::default() - }; + let settings = SchemaSettings::default().with(|s| s.bool_schemas = *bool_schemas); let schema = custom_schema_object_for::>(settings); assert_eq!( schema.instance_type,