Add example for custom schema setings
This commit is contained in:
parent
f387a0ec56
commit
601fc3aaad
5 changed files with 201 additions and 0 deletions
27
docs/_includes/examples/custom_settings.rs
Normal file
27
docs/_includes/examples/custom_settings.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
use schemars::{gen::SchemaSettings, JsonSchema};
|
||||
|
||||
#[derive(JsonSchema)]
|
||||
pub struct MyStruct {
|
||||
pub my_int: i32,
|
||||
pub my_bool: bool,
|
||||
pub my_nullable_enum: Option<MyEnum>,
|
||||
}
|
||||
|
||||
#[derive(JsonSchema)]
|
||||
pub enum MyEnum {
|
||||
StringNewType(String),
|
||||
StructVariant {
|
||||
floats: Vec<f32>,
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let settings = SchemaSettings {
|
||||
option_nullable: true,
|
||||
option_add_null_type: false,
|
||||
..SchemaSettings::draft07()
|
||||
};
|
||||
let gen = settings.into_generator();
|
||||
let schema = gen.into_root_schema_for::<MyStruct>();;
|
||||
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue