-
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Contract
for generating separate serialize/deserialize schemas (#…
…335)
- Loading branch information
Showing
36 changed files
with
1,222 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"title": "MyStruct", | ||
"type": "object", | ||
"properties": { | ||
"definitely_string": { | ||
"type": "string" | ||
}, | ||
"maybe_string": { | ||
"type": "string" | ||
}, | ||
"my_int": { | ||
"type": "integer", | ||
"format": "int32" | ||
}, | ||
"my_read_only_bool": { | ||
"type": "boolean", | ||
"default": false, | ||
"readOnly": true | ||
} | ||
}, | ||
"required": [ | ||
"my_int", | ||
"my_read_only_bool", | ||
"definitely_string" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"$schema": "https://json-schema.org/draft/2020-12/schema", | ||
"title": "MyStruct", | ||
"type": "object", | ||
"properties": { | ||
"definitely_string": { | ||
"type": "string" | ||
}, | ||
"maybe_string": { | ||
"type": "string" | ||
}, | ||
"my_int": { | ||
"type": "integer", | ||
"format": "int32" | ||
}, | ||
"my_read_only_bool": { | ||
"type": "boolean", | ||
"default": false, | ||
"readOnly": true | ||
} | ||
}, | ||
"required": [ | ||
"my_int", | ||
"my_read_only_bool", | ||
"definitely_string" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.