schema!() { /* proc-macro */ }
Expand description
Create OpenAPI Schema from arbitrary type.
This macro provides a quick way to render arbitrary types as OpenAPI Schema Objects. It supports two call formats.
- With type only
- With
#[inline]
attribute to inline the referenced schemas.
By default the macro will create references ($ref)
for non primitive types like Pet
.
However when used with #[inline]
the non primitive
type schemas will
be inlined to the schema output.
let schema = utoipa::schema!(Vec<Pet>);
// with inline
let schema = utoipa::schema!(#[inline] Vec<Pet>);
Examples
Create vec of pets schema.
#[derive(utoipa::ToSchema)]
struct Pet {
id: i32,
name: String,
}
let schema: RefOr<Schema> = utoipa::schema!(#[inline] Vec<Pet>).into();
// will output
let generated = RefOr::T(Schema::Array(
Array::new(
ObjectBuilder::new()
.property("id", ObjectBuilder::new()
.schema_type(SchemaType::Integer)
.format(Some(SchemaFormat::KnownFormat(KnownFormat::Int32)))
.build())
.required("id")
.property("name", Object::with_type(SchemaType::String))
.required("name")
)
));