14 releases

0.4.5 Sep 12, 2023
0.4.4 May 29, 2023
0.4.3 Mar 20, 2023
0.4.2 Apr 10, 2022
0.1.0 Mar 21, 2021

#181 in #openapi

Download history 219/week @ 2023-12-22 28/week @ 2023-12-29 138/week @ 2024-01-05 89/week @ 2024-01-12 265/week @ 2024-01-19 129/week @ 2024-01-26 65/week @ 2024-02-02 67/week @ 2024-02-09 111/week @ 2024-02-16 157/week @ 2024-02-23 157/week @ 2024-03-01 314/week @ 2024-03-08 136/week @ 2024-03-15 99/week @ 2024-03-22 147/week @ 2024-03-29 116/week @ 2024-04-05

510 downloads per month
Used in 3 crates (via openapi_type)

Apache-2.0

25KB
732 lines



This repository contains the following crates:

  • openapi_type openapi_type on crates.io openapi_type on docs.rs
  • openapi_type_derive openapi_type_derive on crates.io openapi_type_derive on docs.rs

openapi_type

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.

Versioning

Like all rust crates, this crate will follow semantic versioning guidelines. However, changing the MSRV (minimum supported rust version) is not considered a breaking change.

License

Copyright (C) 2021-2023 Dominic Meiser and contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

lib.rs:

This crate defines the macros for #[derive(OpenapiType)].

Dependencies

~0.5–1MB
~22K SLoC