#openapi #openapi-v3 #v3

openapiv3-extended

This crate provides data structures that represent the OpenAPI v3.0.x specification easily deserializable with serde

35 stable releases (5 major)

6.0.0 Feb 12, 2024
5.1.1 Jan 9, 2024
4.0.5 Jan 1, 2024
4.0.4 Dec 26, 2023
1.0.4 Jun 13, 2022

#188 in Web programming

Download history 273/week @ 2024-08-20 324/week @ 2024-08-27 273/week @ 2024-09-03 332/week @ 2024-09-10 226/week @ 2024-09-17 286/week @ 2024-09-24 166/week @ 2024-10-01 192/week @ 2024-10-08 191/week @ 2024-10-15 128/week @ 2024-10-22 215/week @ 2024-10-29 193/week @ 2024-11-05 215/week @ 2024-11-12 282/week @ 2024-11-19 171/week @ 2024-11-26 179/week @ 2024-12-03

882 downloads per month
Used in 11 crates (8 directly)

MIT/Apache

385KB
3K SLoC

Crate | Github

OpenAPI v3 Extended

A library to de/serialize OpenAPI specifications. It offers:

  • Convenience methods for creating, modifying, and merging specs
  • A simple API, prioritizing usability without sacrificing correctness
  • Support for v2.0 and v3.0 specs. (v3.1 is not currently supported nor being actively developed. PRs are welcome though.)

Installation

[dependencies]
openapiv3-extended = ".."

Note the library is named openapiv3 and imported as use openapiv3;, despite the package name being openapiv3-extended.

OpenAPI v3 example workflow

This crate provides data structures that represent the OpenAPI v3.0.x specification. Note this does not cover OpenAPI v3.1 (yet) which was an incompatible change.

Usage

Here is a basic example:

use serde_json;
use openapiv3::OpenAPI;

fn main() {
    let data = include_str!("openapi.json");
    let openapi: OpenAPI = serde_json::from_str(data).unwrap();
    println!("{:?}", openapi);
}

You can use this crate to upgrade a Swagger 2.0 spec to OpenAPI 3.0.x. To support v2, enable the v2 feature.

// [dependencies]
// openapiv3-extended = { version = "..", features = ["v2"] }
use openapiv3::VersionedOpenAPI;

fn main() {
    let data = include_str!("swagger.json");
    let openapi: VersionedOpenAPI = serde_json::from_str(data).unwrap();
    println!("{:?}", openapi);
    let openapi: OpenAPI = openapi.upgrade(); // version 3.0
    println!("{:?}", openapi);
}

Acknowledgements

This library started as a fork of https://github.com/glademiller/openapiv3. Both libraries support full de/ser of OpenAPI v3.0 specs. This fork offers:

  • many convenience methods for creating, modifying, and merging specs
  • support for OpenAPI v2.0, and upgrading v2.0 specs to v3.0
  • a simplified API, where we aim to prioritize usability without sacrificing correctness

Goals

  • Provide a deserialization for the specification that maps cleanly to Rust enums etc.

Non Goals

  • Deserialization and subsequent re-serialization are 100% the same.
    • Some defaults show-up when serializing that may not have existed in the input.

Issues

Schemas without a type will end up as any data type as per the specification and can have any parameters of any schema type. Some Open API documents don't include the type parameter it would be nice to try to derive the type but the crate as of right now meets my needs.

Dependencies

~2–3MB
~60K SLoC