Crate openapi_type
source · [−]Expand description
This crate gives static type information for primitives and commonly used types from the standard
library and other commonly used libraries chrono
, indexmap
, linked-hash-map
, time
and
uuid
when the according feature is enabled. Also, it provides a derive macro for structs and
enums to gain access to their static type information at runtime.
The core of this crate is the OpenapiType
trait. It has one static function,
schema
, which returns an OpenapiSchema
. This assembles the static
type information in a way that is convenient to use for a generated OpenAPI specification, but
can also be utilized in other use cases as well.
Custom Types
To gain access to the static type information of your custom types at runtime, the easiest way is to use the derive macro:
#[derive(OpenapiType)]
struct FooBar {
foo: String,
bar: u64
}
OpenAPI specification
Using above type, running FooBar::schema().into_schema()
yields
type: object
title: FooBar
properties:
foo:
type: string
bar:
type: integer
format: int64
minimum: 0
required:
- foo
- bar
Note, however, that this is not sufficient for more complex types. If one of your structs fields
is a type that has a name (that is, Type::schema().name
is not None
), above schema will contain
a reference to that schema. Therefore, always remember to put the
dependencies
into the specification alongside the type you are
interested in.
Re-exports
Structs
This struct is used to generate the OpenAPI specification for a particular type. It is already made available for all primitives and some other types from the rust standard library, and you can also make your own types provide one through the OpenapiType trait and derive macro.
Traits
This trait needs to be implemented by every type that is being used in the OpenAPI Spec. It gives access to the OpenapiSchema of this type. It is provided for primitive types, String and the like. For use on your own types, there is a derive macro:
Derive Macros
The derive macro for OpenapiType.