Write some more docs
This commit is contained in:
parent
3c0ee3f703
commit
aa8a7a4a80
3 changed files with 110 additions and 2 deletions
|
@ -8,4 +8,27 @@ has_toc: false
|
|||
|
||||
# Deriving JsonSchema
|
||||
|
||||
How to use `#[derive(JsonSchema)]`...
|
||||
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);
|
||||
}
|
||||
```
|
||||
<!-- TODO:
|
||||
show example output
|
||||
requirements - when can/can't it be derived
|
||||
generic params behaviour
|
||||
-->
|
||||
|
|
|
@ -5,6 +5,83 @@ parent: Deriving JsonSchema
|
|||
nav_order: 1
|
||||
---
|
||||
|
||||
<style>
|
||||
h3 code {
|
||||
font-weight: bold;
|
||||
}
|
||||
</style>
|
||||
|
||||
# Attributes
|
||||
|
||||
Attributes that affect deriving JsonSchema...
|
||||
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
|
||||
|
||||
<div class="indented">
|
||||
|
||||
<h3 id="rename">
|
||||
|
||||
`#[serde(rename = "name")]` / `#[schemars(rename = "name")]`
|
||||
</h3>
|
||||
|
||||
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)
|
||||
|
||||
<h3 id="rename_all">
|
||||
|
||||
`#[serde(rename_all = "...")]` / `#[schemars(rename_all = "...")]`
|
||||
</h3>
|
||||
|
||||
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)
|
||||
|
||||
<h3 id="tag">
|
||||
|
||||
`#[serde(tag = "type")]` / `#[schemars(tag = "type")]` / `#[serde(untagged)]` / `#[schemars(untagged)]`
|
||||
</h3>
|
||||
|
||||
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)
|
||||
|
||||
<h3 id="default">
|
||||
|
||||
`#[serde(default)]` / `#[schemars(default)]` / `#[serde(default = "path")]` / `#[schemars(default = "path")]`
|
||||
</h3>
|
||||
|
||||
- skip_serializing_if - only used for serializing defaults
|
||||
- serialize_with - only used for serializing defaults
|
||||
|
||||
<h3 id="skip">
|
||||
|
||||
`#[serde(skip)]` / `#[schemars(skip)]`
|
||||
</h3>
|
||||
|
||||
<h3 id="skip_serializing">
|
||||
|
||||
`#[serde(skip_serializing)]` / `#[schemars(skip_serializing)]`
|
||||
</h3>
|
||||
|
||||
<h3 id="skip_deserializing">
|
||||
|
||||
`#[serde(skip_deserializing)]` / `#[schemars(skip_deserializing)]`
|
||||
</h3>
|
||||
|
||||
<h3 id="flatten">
|
||||
|
||||
`#[serde(flatten)]` / `#[schemars(flatten)]`
|
||||
</h3>
|
||||
|
||||
<h3 id="with">
|
||||
|
||||
`#[serde(with = "module")]` / `#[schemars(with = "module")]`
|
||||
</h3>
|
||||
|
||||
</div>
|
||||
|
||||
## Other Attributes
|
||||
- doc
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue