schemars/docs/1.1-attributes.md
2019-12-23 22:07:21 +00:00

2.9 KiB

layout title parent nav_order
default Attributes Deriving JsonSchema 1

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

#[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 / variant / field

#[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 / variant

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

Set on an enum to generate the schema for the internally tagged or untagged representation of this enum. Schemars does not currently support the adjacently tagged representation (#4).

Serde docs: tag / 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