utoipa_gen

Macro schema

Source
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.

  1. With type only
  2. 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: RefOr<Schema> = utoipa::schema!(Vec<Pet>).into();

// with inline
let schema: RefOr<Schema> = utoipa::schema!(#[inline] Vec<Pet>).into();

ยง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(Type::Integer)
                .format(Some(SchemaFormat::KnownFormat(KnownFormat::Int32)))
                .build())
            .required("id")
            .property("name", Object::with_type(Type::String))
            .required("name")
    )
));