pub trait ToSchema {
    fn schema() -> Schema;

    fn aliases() -> Vec<(&'static str, Schema)> { ... }
}
Expand description

Trait for implementing OpenAPI Schema object.

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.

#[derive(ToSchema)]
#[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.

impl utoipa::ToSchema for Pet {
    fn schema() -> utoipa::openapi::schema::Schema {
        use utoipa::openapi::ToArray;
        utoipa::openapi::ObjectBuilder::new()
            .property(
                "id",
                utoipa::openapi::ObjectBuilder::new()
                    .schema_type(utoipa::openapi::SchemaType::Integer)
                    .format(Some(utoipa::openapi::SchemaFormat::Int64)),
            )
            .required("id")
            .property(
                "name",
                utoipa::openapi::Object::with_type(utoipa::openapi::SchemaType::String),
            )
            .required("name")
            .property(
                "age",
                utoipa::openapi::ObjectBuilder::new()
                    .schema_type(utoipa::openapi::SchemaType::Integer)
                    .format(Some(utoipa::openapi::SchemaFormat::Int32)),
            )
            .example(Some(serde_json::json!({
              "name": "bob the cat", "id": 1
            })))
            .into()
    }
}

Required Methods

Provided Methods

Implementors