Skip to content

Commit

Permalink
Document schema_with attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau committed May 15, 2020
1 parent 3fd3160 commit 4c50199
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 9 deletions.
12 changes: 10 additions & 2 deletions docs/1.1-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = "..."]`)
Expand Down
14 changes: 12 additions & 2 deletions docs/_includes/examples/custom_serialization.rs
Original file line number Diff line number Diff line change
@@ -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`.
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion docs/_includes/examples/custom_serialization.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"properties": {
"bool_as_string": {
"default": "false",
"type": "string"
"type": "string",
"format": "boolean"
},
"bool_normal": {
"default": false,
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/7-custom_serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" %}

Expand Down
14 changes: 12 additions & 2 deletions schemars/examples/custom_serialization.rs
Original file line number Diff line number Diff line change
@@ -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`.
Expand All @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion schemars/examples/custom_serialization.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"properties": {
"bool_as_string": {
"default": "false",
"type": "string"
"type": "string",
"format": "boolean"
},
"bool_normal": {
"default": false,
Expand Down

0 comments on commit 4c50199

Please sign in to comment.