diff --git a/docs/_includes/examples/schemars_attrs.rs b/docs/_includes/examples/schemars_attrs.rs new file mode 100644 index 0000000..2d8201a --- /dev/null +++ b/docs/_includes/examples/schemars_attrs.rs @@ -0,0 +1,27 @@ +use schemars::{schema_for, JsonSchema}; +use serde::{Deserialize, Serialize}; + +#[derive(Deserialize, Serialize, JsonSchema)] +#[schemars(rename_all = "camelCase")] +pub struct MyStruct { + #[serde(rename = "thisIsOverridden")] + #[schemars(rename = "myNumber")] + pub my_int: i32, + pub my_bool: bool, + #[schemars(default)] + pub my_nullable_enum: Option, +} + +#[derive(Deserialize, Serialize, JsonSchema)] +#[schemars(untagged)] +pub enum MyEnum { + StringNewType(String), + StructVariant { + floats: Vec, + } +} + +fn main() { + let schema = schema_for!(MyStruct); + println!("{}", serde_json::to_string_pretty(&schema).unwrap()); +} diff --git a/docs/_includes/examples/schemars_attrs.schema.json b/docs/_includes/examples/schemars_attrs.schema.json new file mode 100644 index 0000000..f7e6ac9 --- /dev/null +++ b/docs/_includes/examples/schemars_attrs.schema.json @@ -0,0 +1,53 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "MyStruct", + "type": "object", + "required": [ + "myBool", + "myNumber" + ], + "properties": { + "myBool": { + "type": "boolean" + }, + "myNullableEnum": { + "default": null, + "anyOf": [ + { + "$ref": "#/definitions/MyEnum" + }, + { + "type": "null" + } + ] + }, + "myNumber": { + "type": "integer", + "format": "int32" + } + }, + "definitions": { + "MyEnum": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "object", + "required": [ + "floats" + ], + "properties": { + "floats": { + "type": "array", + "items": { + "type": "number", + "format": "float" + } + } + } + } + ] + } + } +} diff --git a/docs/examples/2-serde_attrs.md b/docs/examples/2-serde_attrs.md index 7fcc379..0044a6e 100644 --- a/docs/examples/2-serde_attrs.md +++ b/docs/examples/2-serde_attrs.md @@ -10,6 +10,6 @@ summary: 'Deriving JsonSchema on types that use #[serde] attributes to customise One of the main aims of this library is compatibility with [Serde](https://github.com/serde-rs/serde). Any generated schema *should* match how [serde_json](https://github.com/serde-rs/json) would serialize/deserialize to/from JSON. To support this, Schemars will check for any `#[serde(...)]` attributes on types that derive `JsonSchema`, and adjust the generated schema accordingly. -The list of supported `#[serde]` attributes are [documented here]({{ site.baseurl }}/deriving/attributes/#supported-serde-attributes). +The list of supported `#[serde]` attributes are [documented here]({{ site.baseurl }}{% link 1.1-attributes.md %}#supported-serde-attributes). {% include example.md name="serde_attrs" %} diff --git a/docs/examples/3-schemars_attrs.md b/docs/examples/3-schemars_attrs.md new file mode 100644 index 0000000..5956a25 --- /dev/null +++ b/docs/examples/3-schemars_attrs.md @@ -0,0 +1,13 @@ +--- +layout: default +title: Using Schemars Attributes +parent: Examples +nav_order: 3 +summary: 'Deriving JsonSchema on types that use #[schemars] attributes to customise serialization behaviour.' +--- + +# Using Serde Attributes + +`#[serde(...)]` attributes can be overriden (or replaced) with `#[schemars(...)]` attributes, which behave identically. You may find this useful if you want to change the generated schema without affecting Serde's behaviour, or if you're just not using Serde. + +{% include example.md name="schemars_attrs" %} diff --git a/docs/examples/3-custom_settings.md b/docs/examples/4-custom_settings.md similarity index 97% rename from docs/examples/3-custom_settings.md rename to docs/examples/4-custom_settings.md index 6358c7d..e9482cc 100644 --- a/docs/examples/3-custom_settings.md +++ b/docs/examples/4-custom_settings.md @@ -2,7 +2,7 @@ layout: default title: Custom Schema Settings parent: Examples -nav_order: 3 +nav_order: 4 summary: Generating a schema using custom settings which changes how Option is handled. --- diff --git a/schemars/examples/schemars_attrs.rs b/schemars/examples/schemars_attrs.rs new file mode 100644 index 0000000..2d8201a --- /dev/null +++ b/schemars/examples/schemars_attrs.rs @@ -0,0 +1,27 @@ +use schemars::{schema_for, JsonSchema}; +use serde::{Deserialize, Serialize}; + +#[derive(Deserialize, Serialize, JsonSchema)] +#[schemars(rename_all = "camelCase")] +pub struct MyStruct { + #[serde(rename = "thisIsOverridden")] + #[schemars(rename = "myNumber")] + pub my_int: i32, + pub my_bool: bool, + #[schemars(default)] + pub my_nullable_enum: Option, +} + +#[derive(Deserialize, Serialize, JsonSchema)] +#[schemars(untagged)] +pub enum MyEnum { + StringNewType(String), + StructVariant { + floats: Vec, + } +} + +fn main() { + let schema = schema_for!(MyStruct); + println!("{}", serde_json::to_string_pretty(&schema).unwrap()); +} diff --git a/schemars/examples/schemars_attrs.schema.json b/schemars/examples/schemars_attrs.schema.json new file mode 100644 index 0000000..f7e6ac9 --- /dev/null +++ b/schemars/examples/schemars_attrs.schema.json @@ -0,0 +1,53 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "MyStruct", + "type": "object", + "required": [ + "myBool", + "myNumber" + ], + "properties": { + "myBool": { + "type": "boolean" + }, + "myNullableEnum": { + "default": null, + "anyOf": [ + { + "$ref": "#/definitions/MyEnum" + }, + { + "type": "null" + } + ] + }, + "myNumber": { + "type": "integer", + "format": "int32" + } + }, + "definitions": { + "MyEnum": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "object", + "required": [ + "floats" + ], + "properties": { + "floats": { + "type": "array", + "items": { + "type": "number", + "format": "float" + } + } + } + } + ] + } + } +}