Document schema_with attribute

This commit is contained in:
Graham Esau 2020-05-15 17:42:37 +01:00
parent 3fd316063a
commit 4c501990b1
6 changed files with 39 additions and 9 deletions

View file

@ -30,6 +30,7 @@ Serde also allows setting `#[serde(...)]` attributes which change how types are
- [`flatten`](#flatten)
- [`with`](#with)
1. [Other Attributes](#other-attributes)
- [`schema_with`](#schema_with)
- [Doc Comments (`doc`)](#doc)
## Supported Serde Attributes
@ -117,16 +118,23 @@ Serde docs: [field](https://serde.rs/field-attrs.html#flatten)
`#[serde(with = "Type")]` / `#[schemars(with = "Type")]`
</h3>
Set on a field to generate this field's schema as the given type instead of the field's actual type. Serde allows the `with` attribute to refer to any module path, but Schemars requires this to be an actual type which implements `JsonSchema`.
Set on a variant or field to generate its schema as the given type instead of its actual type. Serde allows the `with` attribute to refer to any module path, but Schemars requires this to be an actual type which implements `JsonSchema`.
If the given type has any required generic type parameters, then they must all be explicitly specified in this attribute. Serde frequently allows you to omit them as it can make use of type inference, but unfortunately this is not possible with Schemars. For example, `with = "Vec::<i32>"` will work, but `with = "Vec"` and `with = "Vec::<_>"` will not.
Serde docs: [field](https://serde.rs/field-attrs.html#with)
Serde docs: [variant](https://serde.rs/variant-attrs.html#with) / [field](https://serde.rs/field-attrs.html#with)
</div>
## Other Attributes
<h3 id="schema_with">
`#[schemars(schema_with = "some::function")]`
</h3>
Set on a variant or field to generate this field's schema using the given function. This function must be callable as `fn(&mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema`.
<h3 id="doc">
Doc Comments (`#[doc = "..."]`)

View file

@ -1,4 +1,5 @@
use schemars::{schema_for, JsonSchema};
use schemars::schema::{Schema, SchemaObject};
use schemars::{gen::SchemaGenerator, schema_for, JsonSchema};
use serde::{Deserialize, Serialize};
// `int_as_string` and `bool_as_string` use the schema for `String`.
@ -7,15 +8,24 @@ pub struct MyStruct {
#[serde(default = "eight", with = "as_string")]
#[schemars(with = "String")]
pub int_as_string: i32,
#[serde(default = "eight")]
pub int_normal: i32,
#[serde(default, with = "as_string")]
#[schemars(with = "String")]
#[schemars(schema_with = "make_custom_schema")]
pub bool_as_string: bool,
#[serde(default)]
pub bool_normal: bool,
}
fn make_custom_schema(gen: &mut SchemaGenerator) -> Schema {
let mut schema: SchemaObject = <String>::json_schema(gen).into();
schema.format = Some("boolean".to_owned());
schema.into()
}
fn eight() -> i32 {
8
}

View file

@ -5,7 +5,8 @@
"properties": {
"bool_as_string": {
"default": "false",
"type": "string"
"type": "string",
"format": "boolean"
},
"bool_normal": {
"default": false,

View file

@ -13,7 +13,7 @@ summary: >-
Serde allows you to change how a field is (de)serialized by setting a [`#[serde(with = "path")]`](https://serde.rs/field-attrs.html#with) attribute, where `$path::serialize` and `$path::deserialize` must be functions with the correct signature. Schemars supports the same attribute, but `path` must be a type implementing `JsonSchema`.
In order to derive `JsonSchema` on a type which includes a `#[serde(with = "path")]` attribute where `path` is not a type implementing `JsonSchema`, you'll need to override it with a suitable `#[schemars(with = "Type")]` attribute.
In order to derive `JsonSchema` on a type which includes a `#[serde(with = "path")]` attribute where `path` is not a type implementing `JsonSchema`, you'll need to override it with a suitable `#[schemars(with = "Type")]` or `#[schemars(schema_with = "path")]` attribute.
{% include example.md name="custom_serialization" %}

View file

@ -1,4 +1,5 @@
use schemars::{schema_for, JsonSchema};
use schemars::schema::{Schema, SchemaObject};
use schemars::{gen::SchemaGenerator, schema_for, JsonSchema};
use serde::{Deserialize, Serialize};
// `int_as_string` and `bool_as_string` use the schema for `String`.
@ -7,15 +8,24 @@ pub struct MyStruct {
#[serde(default = "eight", with = "as_string")]
#[schemars(with = "String")]
pub int_as_string: i32,
#[serde(default = "eight")]
pub int_normal: i32,
#[serde(default, with = "as_string")]
#[schemars(with = "String")]
#[schemars(schema_with = "make_custom_schema")]
pub bool_as_string: bool,
#[serde(default)]
pub bool_normal: bool,
}
fn make_custom_schema(gen: &mut SchemaGenerator) -> Schema {
let mut schema: SchemaObject = <String>::json_schema(gen).into();
schema.format = Some("boolean".to_owned());
schema.into()
}
fn eight() -> i32 {
8
}

View file

@ -5,7 +5,8 @@
"properties": {
"bool_as_string": {
"default": "false",
"type": "string"
"type": "string",
"format": "boolean"
},
"bool_normal": {
"default": false,