87 lines
No EOL
2.9 KiB
Markdown
87 lines
No EOL
2.9 KiB
Markdown
---
|
|
layout: default
|
|
title: Attributes
|
|
parent: Deriving JsonSchema
|
|
nav_order: 1
|
|
---
|
|
|
|
<style>
|
|
h3 code {
|
|
font-weight: bold;
|
|
}
|
|
</style>
|
|
|
|
# Attributes
|
|
|
|
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 |