Skip to main content

Crate openapi_type

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 (see features) when the according feature is enabled. Please refer to the Cargo.toml for a list of all available feature flags and optional dependencies. 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 three static function, where schema, which returns an OpenapiSchema, is the one you are probably looking for. It 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: Option<String>,
	bar: u64
}

§OpenAPI specification

Using above type, running FooBar::schema().into_schema() yields

properties:
  foo:
    type: string
  bar:
    type: integer
    minimum: 0
    format: int64
type: object
required:
  - bar
title: FooBar

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.

§Path Parameters

We can also treat the FooBar struct from above as a struct containing path parameters, if we construct an Operation for a path like /foo/{foo}/bar/{bar}. Running FooBar::path_params() yields

- name: foo
  in: path
  required: true
  schema:
    type:
      - string
      - "null"
- name: bar
  in: path
  required: true
  schema:
    type: integer
    minimum: 0
    format: int64

§Query Parameters

Likewise, we can treat the FooBar struct from above as a struct containing query parameters. Running FooBar::query_params() yields

- name: foo
  in: query
  required: false
  allowEmptyValue: true
  schema:
    type: string
- name: bar
  in: query
  required: true
  schema:
    type: integer
    minimum: 0
    format: int64

§Features

The following cargo features may be enabled for this crate to unlock additional implementations for larger tuples. For example, the tuples32 feature would unlock the implementation for tuples with sizes up to 32.

  • tuples16 (enabled by default)
  • tuples32
  • tuples48
  • tuples64

The following cargo features may be enabled for this crate to unlock additional implementations for external crates:

  • chrono: Enable all of the below:
  • hashbrown: Enable all of the below:
  • jiff: Enable all of the below:
    • jiff02: Enable implementations for jiff 0.2
  • linked-hash-map: Enable all of the below:
  • time: Enable all of the below:
    • time03: Enable implementations for time 0.3
  • url: Enable all of the below:
    • url2: Enable implementations for url 2.x
  • uuid: Enable all of the below:
    • uuid1: Enable implementations for uuid 1.x

Note that support for indexmap 2.x is enabled by default.

Re-exports§

pub use indexmap::IndexMap;
pub use indexmap::IndexSet;
pub use oas3::Map;
pub use oas3::spec;

Structs§

OpenapiParams
A collection of the OpenAPI specification of parameters.
OpenapiParamsVisitor
A generic visitor for OpenAPI parameters.
OpenapiSchema
PathParamsVisitor
QueryParamsVisitor

Enums§

OpenapiVisitor

Traits§

AlternativesVisitor
ObjectVisitor
OpenapiType
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:
TupleVisitor
Visitor
This trait can be used to visit a type. Call one of the methods on this trait exactly once.

Derive Macros§

OpenapiType
The derive macro for OpenapiType.