diff --git a/docs/_includes/examples/custom_settings.rs b/docs/_includes/examples/custom_settings.rs new file mode 100644 index 0000000..629c867 --- /dev/null +++ b/docs/_includes/examples/custom_settings.rs @@ -0,0 +1,27 @@ +use schemars::{gen::SchemaSettings, JsonSchema}; + +#[derive(JsonSchema)] +pub struct MyStruct { + pub my_int: i32, + pub my_bool: bool, + pub my_nullable_enum: Option, +} + +#[derive(JsonSchema)] +pub enum MyEnum { + StringNewType(String), + StructVariant { + floats: Vec, + } +} + +fn main() { + let settings = SchemaSettings { + option_nullable: true, + option_add_null_type: false, + ..SchemaSettings::draft07() + }; + let gen = settings.into_generator(); + let schema = gen.into_root_schema_for::();; + println!("{}", serde_json::to_string_pretty(&schema).unwrap()); +} diff --git a/docs/_includes/examples/custom_settings.schema.json b/docs/_includes/examples/custom_settings.schema.json new file mode 100644 index 0000000..0bc689c --- /dev/null +++ b/docs/_includes/examples/custom_settings.schema.json @@ -0,0 +1,67 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "MyStruct", + "type": "object", + "required": [ + "my_bool", + "my_int", + "my_nullable_enum" + ], + "properties": { + "my_bool": { + "type": "boolean" + }, + "my_int": { + "type": "integer", + "format": "int32" + }, + "my_nullable_enum": { + "allOf": [ + { + "$ref": "#/definitions/MyEnum" + } + ], + "nullable": true + } + }, + "definitions": { + "MyEnum": { + "anyOf": [ + { + "type": "object", + "required": [ + "StringNewType" + ], + "properties": { + "StringNewType": { + "type": "string" + } + } + }, + { + "type": "object", + "required": [ + "StructVariant" + ], + "properties": { + "StructVariant": { + "type": "object", + "required": [ + "floats" + ], + "properties": { + "floats": { + "type": "array", + "items": { + "type": "number", + "format": "float" + } + } + } + } + } + } + ] + } + } +} diff --git a/docs/examples/3-custom_settings.md b/docs/examples/3-custom_settings.md new file mode 100644 index 0000000..6358c7d --- /dev/null +++ b/docs/examples/3-custom_settings.md @@ -0,0 +1,13 @@ +--- +layout: default +title: Custom Schema Settings +parent: Examples +nav_order: 3 +summary: Generating a schema using custom settings which changes how Option is handled. +--- + +# Custom Schema Settings + +The `gen` module allows you to customise how schemas are generated. For example, the default behaviour for `Option` is to include `null` in the schema's `type`s, but we can instead add a `nullable` property to its schema: + +{% include example.md name="custom_settings" %} diff --git a/schemars/examples/custom_settings.rs b/schemars/examples/custom_settings.rs new file mode 100644 index 0000000..629c867 --- /dev/null +++ b/schemars/examples/custom_settings.rs @@ -0,0 +1,27 @@ +use schemars::{gen::SchemaSettings, JsonSchema}; + +#[derive(JsonSchema)] +pub struct MyStruct { + pub my_int: i32, + pub my_bool: bool, + pub my_nullable_enum: Option, +} + +#[derive(JsonSchema)] +pub enum MyEnum { + StringNewType(String), + StructVariant { + floats: Vec, + } +} + +fn main() { + let settings = SchemaSettings { + option_nullable: true, + option_add_null_type: false, + ..SchemaSettings::draft07() + }; + let gen = settings.into_generator(); + let schema = gen.into_root_schema_for::();; + println!("{}", serde_json::to_string_pretty(&schema).unwrap()); +} diff --git a/schemars/examples/custom_settings.schema.json b/schemars/examples/custom_settings.schema.json new file mode 100644 index 0000000..0bc689c --- /dev/null +++ b/schemars/examples/custom_settings.schema.json @@ -0,0 +1,67 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "MyStruct", + "type": "object", + "required": [ + "my_bool", + "my_int", + "my_nullable_enum" + ], + "properties": { + "my_bool": { + "type": "boolean" + }, + "my_int": { + "type": "integer", + "format": "int32" + }, + "my_nullable_enum": { + "allOf": [ + { + "$ref": "#/definitions/MyEnum" + } + ], + "nullable": true + } + }, + "definitions": { + "MyEnum": { + "anyOf": [ + { + "type": "object", + "required": [ + "StringNewType" + ], + "properties": { + "StringNewType": { + "type": "string" + } + } + }, + { + "type": "object", + "required": [ + "StructVariant" + ], + "properties": { + "StructVariant": { + "type": "object", + "required": [ + "floats" + ], + "properties": { + "floats": { + "type": "array", + "items": { + "type": "number", + "format": "float" + } + } + } + } + } + } + ] + } + } +}