Add doc comments example
This commit is contained in:
parent
51a0966ec1
commit
79155cddf5
5 changed files with 235 additions and 0 deletions
33
docs/_includes/examples/doc_comments.rs
Normal file
33
docs/_includes/examples/doc_comments.rs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
use schemars::{schema_for, JsonSchema};
|
||||||
|
|
||||||
|
/// # My Amazing Struct
|
||||||
|
/// This struct shows off generating a schema with
|
||||||
|
/// a custom title and description.
|
||||||
|
#[derive(JsonSchema)]
|
||||||
|
pub struct MyStruct {
|
||||||
|
/// # My Amazing Integer
|
||||||
|
pub my_int: i32,
|
||||||
|
/// This bool has a description, but no title.
|
||||||
|
pub my_bool: bool,
|
||||||
|
/// # A Nullable Enum
|
||||||
|
/// This enum might be set, or it might not.
|
||||||
|
pub my_nullable_enum: Option<MyEnum>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// # My Amazing Enum
|
||||||
|
#[derive(JsonSchema)]
|
||||||
|
pub enum MyEnum {
|
||||||
|
/// A wrapper around a `String`
|
||||||
|
StringNewType(String),
|
||||||
|
/// A struct-like enum variant which contains
|
||||||
|
/// some floats
|
||||||
|
StructVariant {
|
||||||
|
/// The floats themselves
|
||||||
|
floats: Vec<f32>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let schema = schema_for!(MyStruct);
|
||||||
|
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||||
|
}
|
78
docs/_includes/examples/doc_comments.schema.json
Normal file
78
docs/_includes/examples/doc_comments.schema.json
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
|
"title": "My Amazing Struct",
|
||||||
|
"description": "This struct shows off generating a schema with a custom title and description.",
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"my_bool",
|
||||||
|
"my_int",
|
||||||
|
"my_nullable_enum"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"my_bool": {
|
||||||
|
"description": "This bool has a description, but no title.",
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
|
"my_int": {
|
||||||
|
"title": "My Amazing Integer",
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int32"
|
||||||
|
},
|
||||||
|
"my_nullable_enum": {
|
||||||
|
"title": "A Nullable Enum",
|
||||||
|
"description": "This enum might be set, or it might not.",
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/MyEnum"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"definitions": {
|
||||||
|
"MyEnum": {
|
||||||
|
"title": "My Amazing Enum",
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"description": "A wrapper around a `String`",
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"StringNewType"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"StringNewType": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"description": "A struct-like enum variant which contains some floats",
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"StructVariant"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"StructVariant": {
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"floats"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"floats": {
|
||||||
|
"description": "The floats themselves",
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "number",
|
||||||
|
"format": "float"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
docs/examples/6-doc_comments.md
Normal file
13
docs/examples/6-doc_comments.md
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
---
|
||||||
|
layout: default
|
||||||
|
title: Doc Comments
|
||||||
|
parent: Examples
|
||||||
|
nav_order: 5
|
||||||
|
summary: Giving schemas a custom title and/or description using doc comments.
|
||||||
|
---
|
||||||
|
|
||||||
|
# Setting a Custom Title and/or Description Using Doc Comments
|
||||||
|
|
||||||
|
If a struct, variant or field has any [doc comments](https://doc.rust-lang.org/stable/rust-by-example/meta/doc.html#doc-comments) (or [`doc` attributes](https://doc.rust-lang.org/rustdoc/the-doc-attribute.html)), then these will be used as the generated schema's `description`. If the first line is an ATX-style markdown heading (i.e. it begins with a # character), then it will be used as the schema's `title`, and the remaining lines will be the `description`.
|
||||||
|
|
||||||
|
{% include example.md name="doc_comments" %}
|
33
schemars/examples/doc_comments.rs
Normal file
33
schemars/examples/doc_comments.rs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
use schemars::{schema_for, JsonSchema};
|
||||||
|
|
||||||
|
/// # My Amazing Struct
|
||||||
|
/// This struct shows off generating a schema with
|
||||||
|
/// a custom title and description.
|
||||||
|
#[derive(JsonSchema)]
|
||||||
|
pub struct MyStruct {
|
||||||
|
/// # My Amazing Integer
|
||||||
|
pub my_int: i32,
|
||||||
|
/// This bool has a description, but no title.
|
||||||
|
pub my_bool: bool,
|
||||||
|
/// # A Nullable Enum
|
||||||
|
/// This enum might be set, or it might not.
|
||||||
|
pub my_nullable_enum: Option<MyEnum>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// # My Amazing Enum
|
||||||
|
#[derive(JsonSchema)]
|
||||||
|
pub enum MyEnum {
|
||||||
|
/// A wrapper around a `String`
|
||||||
|
StringNewType(String),
|
||||||
|
/// A struct-like enum variant which contains
|
||||||
|
/// some floats
|
||||||
|
StructVariant {
|
||||||
|
/// The floats themselves
|
||||||
|
floats: Vec<f32>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let schema = schema_for!(MyStruct);
|
||||||
|
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||||
|
}
|
78
schemars/examples/doc_comments.schema.json
Normal file
78
schemars/examples/doc_comments.schema.json
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
|
"title": "My Amazing Struct",
|
||||||
|
"description": "This struct shows off generating a schema with a custom title and description.",
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"my_bool",
|
||||||
|
"my_int",
|
||||||
|
"my_nullable_enum"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"my_bool": {
|
||||||
|
"description": "This bool has a description, but no title.",
|
||||||
|
"type": "boolean"
|
||||||
|
},
|
||||||
|
"my_int": {
|
||||||
|
"title": "My Amazing Integer",
|
||||||
|
"type": "integer",
|
||||||
|
"format": "int32"
|
||||||
|
},
|
||||||
|
"my_nullable_enum": {
|
||||||
|
"title": "A Nullable Enum",
|
||||||
|
"description": "This enum might be set, or it might not.",
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"$ref": "#/definitions/MyEnum"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "null"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"definitions": {
|
||||||
|
"MyEnum": {
|
||||||
|
"title": "My Amazing Enum",
|
||||||
|
"anyOf": [
|
||||||
|
{
|
||||||
|
"description": "A wrapper around a `String`",
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"StringNewType"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"StringNewType": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"description": "A struct-like enum variant which contains some floats",
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"StructVariant"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"StructVariant": {
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"floats"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"floats": {
|
||||||
|
"description": "The floats themselves",
|
||||||
|
"type": "array",
|
||||||
|
"items": {
|
||||||
|
"type": "number",
|
||||||
|
"format": "float"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue