pub trait Schema {
// Required method
fn schema() -> impl Into<SchemaRef>;
}Expand description
§OpenAPI Schema trait
§Required
schema() -> impl Into<schema::SchemaRef>- this
impl Into<schema::SchemaRef>mostly meansschema::Schema<{something}>.
- this
§Implementation Notes
Generally, you can implement this trait for your types by #[derive(Schema)].
See it’s documentation for more details.
But of course, you can implement it manually if you want to. In that case, start from base schemas:
string()number()integer()bool()array({items})object()any_of({schemas})all_of({schemas})one_of({schemas})
and, component({name}, {schema}) if you want to name and reuse
the schema in the OpenAPI document.
§Example
ⓘ
use ohkami::openapi;
#[derive(openapi::Schema)]
struct MySchema {
pub id: u32,
pub name: String,
pub age: Option<u8>,
}
/* equivalent to: */
impl openapi::Schema for MySchema {
fn schema() -> impl Into<openapi::schema::SchemaRef> {
openapi::object()
.property("id", openapi::integer().format("uint32"))
.property("name", openapi::string())
.optional("age", openapi::integer().format("uint8"))
}
}
#[derive(openapi::Schema)]
#[openapi(component)]
struct MyComponentSchema {
pub id: u32,
pub name: String,
pub age: Option<u8>,
}
/* equivalent to: */
impl openapi::Schema for MyComponentSchema {
fn schema() -> impl Into<openapi::schema::SchemaRef> {
openapi::component("MyComponentSchema", openapi::object()
.property("id", openapi::integer().format("uint32"))
.property("name", openapi::string())
.optional("age", openapi::integer().format("uint8"))
)
}
}§Default Implementations
str,Stringu8,u16,u32,u64,usizei8,i16,i32,i64,isizef32,f64uuid::UuidVec<S>,[S],[S; N],Cow<'_, S>,Arc<S>,&SwhereS: Schema
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.