Expand description
Trait for implementing OpenAPI Schema object.
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::ObjectBuilder::new()
.property(
"id",
utoipa::openapi::PropertyBuilder::new()
.component_type(utoipa::openapi::ComponentType::Integer)
.format(Some(utoipa::openapi::ComponentFormat::Int64)),
)
.required("id")
.property(
"name",
utoipa::openapi::Property::new(utoipa::openapi::ComponentType::String),
)
.required("name")
.property(
"age",
utoipa::openapi::PropertyBuilder::new()
.component_type(utoipa::openapi::ComponentType::Integer)
.format(Some(utoipa::openapi::ComponentFormat::Int32)),
)
.example(Some(serde_json::json!({
"name": "bob the cat", "id": 1
})))
.into()
}
}