Add Contract for generating separate serialize/deserialize schemas (#335)

This commit is contained in:
Graham Esau 2024-09-04 19:41:34 +01:00 committed by GitHub
parent 497333e91b
commit 05325d2b7c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
36 changed files with 1224 additions and 225 deletions

View file

@ -0,0 +1,29 @@
use schemars::{generate::SchemaSettings, JsonSchema};
use serde::{Deserialize, Serialize};
#[derive(JsonSchema, Deserialize, Serialize)]
// The schema effectively ignores this `rename_all`, since it doesn't apply to serialization
#[serde(rename_all(deserialize = "PascalCase"))]
pub struct MyStruct {
pub my_int: i32,
#[serde(skip_deserializing)]
pub my_read_only_bool: bool,
// This property is excluded from the schema
#[serde(skip_serializing)]
pub my_write_only_bool: bool,
// This property is excluded from the "required" properties of the schema, because it may be
// be skipped during serialization
#[serde(skip_serializing_if = "str::is_empty")]
pub maybe_string: String,
pub definitely_string: String,
}
fn main() {
// By default, generated schemas describe how types are deserialized.
// So we modify the settings here to instead generate schemas describing how it's serialized:
let settings = SchemaSettings::default().for_serialize();
let generator = settings.into_generator();
let schema = generator.into_root_schema_for::<MyStruct>();
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
}