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

@ -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
}