Trait salvo_oapi::ToSchema
source · pub trait ToSchema {
// Required method
fn to_schema(components: &mut Components) -> RefOr<Schema>;
}
Expand description
Trait for implementing OpenAPI Schema object.
Generated schemas can be referenced or reused in path operations.
This trait is derivable and can be used with [#derive]
attribute. For a details of
#[derive(ToSchema)]
refer to derive documentation.
§Examples
Use #[derive]
to implement ToSchema
trait.
use salvo_oapi::ToSchema;
#[derive(ToSchema)]
#[salvo(schema(example = json!({"name": "bob the cat", "id": 1})))]
struct Pet {
id: u64,
name: String,
age: Option<i32>,
}
Following manual implementation is equal to above derive one.
use salvo_oapi::{Components, ToSchema, RefOr, Schema, SchemaFormat, SchemaType, KnownFormat, Object};
impl ToSchema for Pet {
fn to_schema(components: &mut Components) -> RefOr<Schema> {
Object::new()
.property(
"id",
Object::new()
.schema_type(SchemaType::Integer)
.format(SchemaFormat::KnownFormat(
KnownFormat::Int64,
)),
)
.required("id")
.property(
"name",
Object::new()
.schema_type(SchemaType::String),
)
.required("name")
.property(
"age",
Object::new()
.schema_type(SchemaType::Integer)
.format(SchemaFormat::KnownFormat(
KnownFormat::Int32,
)),
)
.example(serde_json::json!({
"name":"bob the cat","id":1
}))
.into()
}
}
Required Methods§
sourcefn to_schema(components: &mut Components) -> RefOr<Schema>
fn to_schema(components: &mut Components) -> RefOr<Schema>
Returns a tuple of name and schema or reference to a schema that can be referenced by the name or inlined directly to responses, request bodies or parameters.
Object Safety§
This trait is not object safe.