Trait utoipa::PartialSchema
source · pub trait PartialSchema {
// Required method
fn schema() -> RefOr<Schema>;
}
Expand description
Trait used to implement only Schema
part of the OpenAPI doc.
This trait is by default implemented for Rust primitive
types and some well known types like
Vec
, Option
, HashMap
and BTreeMap
. The default implementation adds schema()
method to the implementing type allowing simple conversion of the type to the OpenAPI Schema
object. Moreover this allows handy way of constructing schema objects manually if ever so
wished.
The trait can be implemented manually easily on any type. This trait comes especially handy
with schema
macro that can be used to generate schema for arbitrary types.
struct MyType;
impl PartialSchema for MyType {
fn schema() -> RefOr<Schema> {
// ... impl schema generation here
RefOr::T(Schema::Object(ObjectBuilder::new().build()))
}
}
Examples
Create number schema from u64.
let number: RefOr<Schema> = u64::schema().into();
// would be equal to manual implementation
let number2 = RefOr::T(
Schema::Object(
ObjectBuilder::new()
.schema_type(SchemaType::Integer)
.format(Some(SchemaFormat::KnownFormat(KnownFormat::Int64)))
.minimum(Some(0.0))
.build()
)
);
Construct a Pet object schema manually.
struct Pet {
id: i32,
name: String,
}
let pet_schema = ObjectBuilder::new()
.property("id", i32::schema())
.property("name", String::schema())
.required("id").required("name")
.build();