Expand description
Component trait for implementing swagger specification schema object for a type.
This trait is deriveable and can be used with [#derive]
attribute. For a details of
#[derive(Component)]
refer to derive documentation.
Examples
Use #[derive]
to implement Component
trait.
#[derive(Component)]
#[component(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.
impl utoipa::Component for Pet {
fn component() -> utoipa::openapi::schema::Component {
use utoipa::openapi::ToArray;
utoipa::openapi::Object::new()
.with_property(
"id",
utoipa::openapi::Property::new(utoipa::openapi::ComponentType::Integer)
.with_format(utoipa::openapi::ComponentFormat::Int64),
)
.with_required("id")
.with_property(
"name",
utoipa::openapi::Property::new(utoipa::openapi::ComponentType::String),
)
.with_required("name")
.with_property(
"age",
utoipa::openapi::Property::new(utoipa::openapi::ComponentType::Integer)
.with_format(utoipa::openapi::ComponentFormat::Int32),
)
.with_example(serde_json::json!({
"name":"bob the cat","id":1
}))
.into()
}
}