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> = i64::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)))
            .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();

Required Methods§

source

fn schema() -> RefOr<Schema>

Return ref or schema of implementing type that can then be used to construct combined schemas.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl PartialSchema for &str

source§

impl PartialSchema for Option<&str>

source§

impl PartialSchema for Option<bool>

source§

impl PartialSchema for Option<char>

source§

impl PartialSchema for Option<f32>

source§

impl PartialSchema for Option<f64>

source§

impl PartialSchema for Option<i8>

source§

impl PartialSchema for Option<i16>

source§

impl PartialSchema for Option<i32>

source§

impl PartialSchema for Option<i64>

source§

impl PartialSchema for Option<i128>

source§

impl PartialSchema for Option<isize>

source§

impl PartialSchema for Option<u8>

source§

impl PartialSchema for Option<u16>

source§

impl PartialSchema for Option<u32>

source§

impl PartialSchema for Option<u64>

source§

impl PartialSchema for Option<u128>

source§

impl PartialSchema for Option<usize>

source§

impl PartialSchema for Option<String>

source§

impl PartialSchema for bool

source§

impl PartialSchema for char

source§

impl PartialSchema for f32

source§

impl PartialSchema for f64

source§

impl PartialSchema for i8

source§

impl PartialSchema for i16

source§

impl PartialSchema for i32

source§

impl PartialSchema for i64

source§

impl PartialSchema for i128

source§

impl PartialSchema for isize

source§

impl PartialSchema for str

source§

impl PartialSchema for u8

source§

impl PartialSchema for u16

source§

impl PartialSchema for u32

source§

impl PartialSchema for u64

source§

impl PartialSchema for u128

source§

impl PartialSchema for usize

source§

impl PartialSchema for String

source§

impl<'__s, K: PartialSchema, V: ToSchema<'__s>> PartialSchema for Option<BTreeMap<K, V>>

source§

impl<'__s, K: PartialSchema, V: ToSchema<'__s>> PartialSchema for Option<HashMap<K, V>>

source§

impl<'__s, K: PartialSchema, V: ToSchema<'__s>> PartialSchema for BTreeMap<K, V>

source§

impl<'__s, K: PartialSchema, V: ToSchema<'__s>> PartialSchema for HashMap<K, V>

source§

impl<'__s, T: ToSchema<'__s>> PartialSchema for &[T]

source§

impl<'__s, T: ToSchema<'__s>> PartialSchema for &mut [T]

source§

impl<'__s, T: ToSchema<'__s>> PartialSchema for Option<&[T]>

source§

impl<'__s, T: ToSchema<'__s>> PartialSchema for Option<&mut [T]>

source§

impl<'__s, T: ToSchema<'__s>> PartialSchema for Option<Vec<T>>

source§

impl<'__s, T: ToSchema<'__s>> PartialSchema for Option<T>

source§

impl<'__s, T: ToSchema<'__s>> PartialSchema for [T]

source§

impl<'__s, T: ToSchema<'__s>> PartialSchema for Vec<T>

Implementors§