Add example for schemars attributes
This commit is contained in:
parent
601fc3aaad
commit
b11536e527
7 changed files with 175 additions and 2 deletions
27
docs/_includes/examples/schemars_attrs.rs
Normal file
27
docs/_includes/examples/schemars_attrs.rs
Normal file
|
@ -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<MyEnum>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, JsonSchema)]
|
||||
#[schemars(untagged)]
|
||||
pub enum MyEnum {
|
||||
StringNewType(String),
|
||||
StructVariant {
|
||||
floats: Vec<f32>,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let schema = schema_for!(MyStruct);
|
||||
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||
}
|
53
docs/_includes/examples/schemars_attrs.schema.json
Normal file
53
docs/_includes/examples/schemars_attrs.schema.json
Normal file
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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" %}
|
||||
|
|
13
docs/examples/3-schemars_attrs.md
Normal file
13
docs/examples/3-schemars_attrs.md
Normal file
|
@ -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" %}
|
|
@ -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<T> is handled.
|
||||
---
|
||||
|
27
schemars/examples/schemars_attrs.rs
Normal file
27
schemars/examples/schemars_attrs.rs
Normal file
|
@ -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<MyEnum>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, JsonSchema)]
|
||||
#[schemars(untagged)]
|
||||
pub enum MyEnum {
|
||||
StringNewType(String),
|
||||
StructVariant {
|
||||
floats: Vec<f32>,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let schema = schema_for!(MyStruct);
|
||||
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||
}
|
53
schemars/examples/schemars_attrs.schema.json
Normal file
53
schemars/examples/schemars_attrs.schema.json
Normal file
|
@ -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"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue