diff --git a/docs/1-deriving.md b/docs/1-deriving.md index 7937333..90cee07 100644 --- a/docs/1-deriving.md +++ b/docs/1-deriving.md @@ -8,4 +8,27 @@ has_toc: false # Deriving JsonSchema -How to use `#[derive(JsonSchema)]`... \ No newline at end of file +The most important trait in Schemars is `JsonSchema`, and the most important function of that trait is `json_schema(...)` which returns a JSON schema describing the type. Implementing this manually on many types would be slow and error-prone, so Schemars includes a derive macro which can implement that trait for you. Any derived implementation of `JsonSchema` should create a schema that describes the JSON representation of the type if it were to be serialized by serde_json. + +Usually, all you need to do to use it is to add a `#[derive(JsonSchema)]` attribute to your type: +```rust +use schemars::{JsonSchema, schema_for}; + +#[derive(JsonSchema, Debug)] +struct Point { + x: i32, + y: i32, +} + +fn main() { + let schema = schema_for!(Point); + + let serialized = serde_json::to_string(&schema).unwrap(); + println!("{}", serialized); +} +``` + diff --git a/docs/1.1-attributes.md b/docs/1.1-attributes.md index 53808a4..e5c3f20 100644 --- a/docs/1.1-attributes.md +++ b/docs/1.1-attributes.md @@ -5,6 +5,83 @@ parent: Deriving JsonSchema nav_order: 1 --- + + # Attributes -Attributes that affect deriving JsonSchema... \ No newline at end of file +You can add attributes to your types to customize Schemars's derived `JsonSchema` implementation. + +Serde also allows setting `#[serde(...)]` attributes which change how types are serialized, and Schemars will generally respect these attributes to ensure that generated schemas will match how the type is serialized by serde_json. `#[serde(...)]` attributes can be overriden using `#[schemars(...)]` attributes, which behave identically (e.g. `#[schemars(rename_all = "camelCase")]`). You may find this useful if you want to change the generated schema without affecting Serde's behaviour, or if you're just not using Serde. + +## Supported Serde Attributes + +
+ +

+ +`#[serde(rename = "name")]` / `#[schemars(rename = "name")]` +

+ +Set on a struct, enum, field or variant to use the given name in the generated schema instead of the Rust name. When used on a struct or enum, the given name will be used as the title for root schemas, and the schema identifier when the schema is referenced via a `$ref` schema property. + +Serde docs: [container](https://serde.rs/container-attrs.html#rename) / [variant](https://serde.rs/variant-attrs.html#rename) / [field](https://serde.rs/field-attrs.html#rename) + +

+ +`#[serde(rename_all = "...")]` / `#[schemars(rename_all = "...")]` +

+ +Set on a struct, enum or variant to rename all fields according to the given case convention. + +Serde docs: [container](https://serde.rs/container-attrs.html#rename_all) / [variant](https://serde.rs/variant-attrs.html#rename_all) + +

+ +`#[serde(tag = "type")]` / `#[schemars(tag = "type")]` / `#[serde(untagged)]` / `#[schemars(untagged)]` +

+ +Set on an enum to generate the schema for the [internally tagged](https://serde.rs/enum-representations.html#internally-tagged) or [untagged](https://serde.rs/enum-representations.html#untagged) representation of this enum. Schemars does not currently support the adjacently tagged representation ([#4](https://github.com/GREsau/schemars/issues/4)). + +Serde docs: [`tag`](https://serde.rs/container-attrs.html#tag) / [`untagged`](https://serde.rs/container-attrs.html#untagged) + +

+ +`#[serde(default)]` / `#[schemars(default)]` / `#[serde(default = "path")]` / `#[schemars(default = "path")]` +

+ +- skip_serializing_if - only used for serializing defaults +- serialize_with - only used for serializing defaults + +

+ +`#[serde(skip)]` / `#[schemars(skip)]` +

+ +

+ +`#[serde(skip_serializing)]` / `#[schemars(skip_serializing)]` +

+ +

+ +`#[serde(skip_deserializing)]` / `#[schemars(skip_deserializing)]` +

+ +

+ +`#[serde(flatten)]` / `#[schemars(flatten)]` +

+ +

+ +`#[serde(with = "module")]` / `#[schemars(with = "module")]` +

+ +
+ +## Other Attributes +- doc \ No newline at end of file diff --git a/docs/_sass/overrides.scss b/docs/_sass/overrides.scss index 29ff4b0..a7e42cc 100644 --- a/docs/_sass/overrides.scss +++ b/docs/_sass/overrides.scss @@ -39,4 +39,12 @@ code { .navigation-list-link:hover { text-decoration: underline; text-decoration-color: rgba($link-color, 0.6); +} + +.indented { + margin-left: 20px; + + h1, h2, h3, h4, h5, h6 { + margin-left: -20px; + } } \ No newline at end of file