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, BasicType, SchemaType, KnownFormat, Object};
impl ToSchema for Pet {
fn to_schema(components: &mut Components) -> RefOr<Schema> {
Object::new()
.property(
"id",
Object::new()
.schema_type(BasicType::Integer)
.format(SchemaFormat::KnownFormat(
KnownFormat::Int64,
)),
)
.required("id")
.property(
"name",
Object::new()
.schema_type(BasicType::String),
)
.required("name")
.property(
"age",
Object::new()
.schema_type(BasicType::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.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl ToSchema for NaiveDateTime
impl ToSchema for NaiveDateTime
Source§impl ToSchema for CompactString
impl ToSchema for CompactString
Source§impl ToSchema for StatusError
impl ToSchema for StatusError
Source§impl ToSchema for OffsetDateTime
impl ToSchema for OffsetDateTime
Source§impl ToSchema for PrimitiveDateTime
impl ToSchema for PrimitiveDateTime
Source§impl<K: ToSchema, V: ToSchema> ToSchema for IndexMap<K, V>
Available on crate feature indexmap
only.
impl<K: ToSchema, V: ToSchema> ToSchema for IndexMap<K, V>
Available on crate feature
indexmap
only.