openapi schema serialization for rust

Overview

open api Build Status Software License crates.io

Rust crate for serializing and deserializing open api documents

Documentation

install

add the following to your Cargo.toml file

[dependencies]
openapi = "0.1"

usage

extern crate openapi;

fn main() {
  match openapi::from_path("path/to/openapi.yaml") {
    Ok(spec) => println!("spec: {:?}", spec),
    Err(err) => println!("error: {}", err)
  }
}

Doug Tangren (softprops) 2017

Comments
  • Support references in document

    Support references in document

    Support intra document references https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#reference-object

    I think most people uses this, even the petstore example uses this.

    opened by JordiPolo 11
  • Improve error handling

    Improve error handling

    The failure crate appears to be deprecated these days and has drawbacks including incompatibilities with std::error::Error. For users, it might be inconvenient to have to deal with error types that don’t implement std::error::Error.

    For this reason, this replaces the failure crate entirely. In the library part, thiserror is used instead, which also makes the generation of the error type short and convenient (even taking care of the From implementations), without exposing any thiserror specifics to the user.

    In the example, anyhow now takes care of printing nicely formatted error messages, including error causes and (if provided with RUST_BACKTRACE=1) an error backtrace.

    Also, the error-chain dependency isn’t used anymore, so let’s remove it.

    opened by pluehne 1
  • feature: schema collecting

    feature: schema collecting

    Added way to collect referenced schemas in another files (local).

    This is used by my other project which leverages this to generate models from openapi spec. In case this feature does not fit this lib, I'll migrate the code to my project instead

    opened by alamminsalo 1
  • externalDocs is an object, not a vec

    externalDocs is an object, not a vec

    • https://swagger.io/specification/v2/#externalDocumentationObject
    • https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v2.0/json/petstore-with-external-docs.json#L18-L21
    • https://idratherbewriting.com/learnapidoc/pubapis_openapi_step8_externaldocs_object.html
    opened by ctaggart 1
  • OpenAPI v3.0 implementation

    OpenAPI v3.0 implementation

    This PR adds the required implementation for OpenAPI v3.0.1. Should fix #7, or at least partially.

    In addition to v3.0, this PR adds two new tests (one for v2 and one for v3.0).

    Those two tests will deserialize and re-serialize input files to a JSON string through two different paths, comparing the result.

    1. File -> String -> serde_yaml::Value -> serde_json::Value -> String
    2. File -> Spec -> serde_json::Value -> String Both conversions of serde_json::Value -> String are done using serde_json::to_string_pretty.

    Since the first conversion is independant of the current crate (and only uses serde's json and yaml support), no information should be lost in the final JSON string.

    The second conversion goes through the OpenApi enum, so the final JSON string is a representation of the crate implementation.

    By comparing those two JSON conversions, we can validate the implementation. JSON files will be located in:

    target/tests/can_deserialize_and_reserialize_v3/{yaml_to_json,yaml_to_spec_to_json}/
    

    Note that the official examples for https://github.com/OAI/OpenAPI-Specification/tree/29ea26df7dfd8def1605521f5f32c692a642de79/examples/v3.0 are added to the data/v3.0 directory.

    Those two tests exposes issues not only with the new v3.0 implementation but also with the previous v2 one.

    Missing from v2:

    • k8s.json:
      • additionalProperties
      • x-*
      • operationId
      • Probably others? File is 1.6 MB
    • petstore-simple.json:
      • allOf
      • termsOfService
      • operationId
    • petstore_minimal.json:
      • termsOfService
    • rocks.json:
      • termsOfService
      • operationId
      • default
    • uber.json:
      • security

    Missing from v3.0:

    • api-with-examples.json:
      • examples
    • callback-example.json:
      • callbacks
    • link-example.json:
      • operationId
      • parameters
    • petstore-expanded.json:
      • allOf
      • termsOfService
      • style
    • petstore.json:
      • Nothing!
    • uspto.json:
      • example
      • default
      • server variables

    Note that CI will fail because of those tests. Let me know if you want me to disable the assertion to let the CI pass.

    Note that the first commit in this PR (acf8463) is part of PR #19 and ed759ee is PR #20. As such it should either be merged after those two others or be the only one merged.

    opened by nbigaouette 1
  • Fix compilation issue from a33e084 (`Operations` -> `PathItem`)

    Fix compilation issue from a33e084 (`Operations` -> `PathItem`)

    Last commit on master (a33e084) breaks compilation:

       Compiling openapi v0.1.5 (file:///${HOME}/openapi.git)
    error[E0412]: cannot find type `Operations` in this scope
      --> src/schema.rs:47:33
       |
    47 |     pub paths: BTreeMap<String, Operations>,
       |                                 ^^^^^^^^^^ did you mean `Operation`?
    
    error[E0412]: cannot find type `Operations` in this scope
    

    Looking at a33e084 it seems the Operations was renamed to PathItem? This commit simply rename Operations to PathItem for Spec::paths.

    opened by nbigaouette 1
  • Deriving Clone for all the structs

    Deriving Clone for all the structs

    Allows to do spec.clone() . Maybe I should not need to do this in my code so not sure if this is a good addition or not.

    Also, please release 0.1.6 when you can.

    opened by JordiPolo 1
  • Adds a basic test to ensure the deserialization does not blow up

    Adds a basic test to ensure the deserialization does not blow up

    • Added some files from https://github.com/OAI/OpenAPI-Specification/tree/master/examples/v2.0/yaml
    • A test that just deserializes them.

    If the deserializer is broken, it will panic (hopefully) and the test will fail.

    opened by JordiPolo 1
  • Add schema validation fields

    Add schema validation fields

    Some schema validation fields are missing or not implemented according to the JSON schema validation specification (which is used in several parts of the OpenAPI 3.0 specification). This adds the missing fields and fixes the type of minimum, which may only take integers and not arbitrary schemas.


    Notes

    opened by pluehne 0
  • Allow additionalProperties to take Boolean values

    Allow additionalProperties to take Boolean values

    The OpenAPI specification defines the additionalProperties field according to the JSON schema specification. In the version of the JSON schema specification referenced by the OpenAPI specification, the value of additionalProperties may be either Boolean or a schema (though it appears that newer versions of the JSON schema specification no longer allow Boolean values).

    However, to comply with OpenAPI 3.0, this adjusts the representation of the additionalProperties field in this crate accordingly

    opened by pluehne 0
  • Enhancements for openapi spec support

    Enhancements for openapi spec support

    Added to schema (from openapi 3 spec)

    • oneOf, anyOf, not
    • maxLength, minLength
    • extensions map: this map will include all additional fields that are defined in the model. This field is flattened so serialization/deserialization works as intended

    Fixed failing travis build job

    opened by alamminsalo 0
  • Add Builder pattern to structs

    Add Builder pattern to structs

    This PR uses the derive_builder crate to derive builder classes for all structs. This enables writing an OpenApi Spec in a much more readable and straight forward fashion (No need to explicitly set parts of the struct to None or creating a mutable struct via default and then just editing the desired fileds). This is useful when using this crate to generate an openapi spec instead of parsing it from a file.

    Example with Builders:

    use openapi::OpenApi;
    use openapi::v3_0::{ComponentsBuilder, ContactBuilder, InfoBuilder, OperationBuilder, ParameterBuilder, PathItemBuilder, ServerBuilder, SpecBuilder};
    
    let spec = SpecBuilder::default()
    	.openapi("3.0.1".to_string())
    	.info(InfoBuilder::default()
    			.title("Example Api".to_string())
    			.version(clap::crate_version!().to_string())
    			.contact(ContactBuilder::default()
    					.name("Support".to_string())
    					.email("[email protected]".to_string())
    					.build().unwrap()
    			)
    			.build().unwrap()
    	)
    	.servers(vec![ ServerBuilder::default()
    					.url("https://api.example.com")
    					.build().unwrap()
    			]
    	)
    	.paths(api_paths)
    	.build().unwrap();
    
    OpenApi::V3_0(spec)
    
    opened by ju6ge 0
  • Remove `failure` crate dependency

    Remove `failure` crate dependency

    Commit e2d506c ("Replace failure dependency with thiserror/anyhow") already replaced the use of obsolete failure crate, but let's also remove it from dependencies.

    opened by penberg 0
  • More support for deprecation

    More support for deprecation

    Added:

    • Support for deprecated operations in V2
      • see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#fixed-fields-5
    • Support for deprecated parameters in V3
      • see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#fixed-fields-10
    • Support for deprecated schemas in V3
      • see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#fixed-fields-20
    opened by jeevcat 1
  • Fixed the data structure for OpenApi 3

    Fixed the data structure for OpenApi 3

    1. Enums can hold any value: boolean, strings, numbers...so just use JsonValue
    2. Items can point to Ref so wrap Schema inside ObjectOrReference
    3. Reorder ObjectOrReference because they are untagged, its better to check for Ref first

    Signed-off-by: Hanif Ariffin [email protected]

    opened by hbina 1
Owner
Doug Tangren
rusting at sea
Doug Tangren
Rust GraphQL server using simple type-only schema

SimpleGQL This library allows a simplified GraphQL schema to be given and will run a server with a backend store (currently only SQLite) and a set of

Daniel Cocks 5 May 10, 2023
Rust Macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev.

Rust Embed Rust Custom Derive Macro which loads files into the rust binary at compile time during release and loads the file from the fs during dev. Y

Peter 1k Jan 5, 2023
Rust I18n is use Rust codegen for load YAML file storage translations on compile time, and give you a t! macro for simply get translation texts.

Rust I18n Rust I18n is use Rust codegen for load YAML file storage translations on compile time, and give you a t! macro for simply get translation te

Longbridge 73 Dec 27, 2022
lispr is a Rust macro that tries to implement a small subset of LISPs syntax in Rust

lispr lispr is a Rust macro that tries to implement a small subset of LISPs syntax in Rust. It is neither especially beautiful or efficient since it i

Jan Vaorin 0 Feb 4, 2022
Rust/Axum server implementation with PCR(Prisma Client Rust)

Realworld Rust Axum Prisma This project utilizes Rust with the Axum v0.7 framework along with the Prisma Client Rust to build a realworld application.

Neo 3 Dec 9, 2023
A Rust web framework

cargonauts - a Rust web framework Documentation cargonauts is a Rust web framework intended for building maintainable, well-factored web apps. This pr

null 179 Dec 25, 2022
A Rust library to extract useful data from HTML documents, suitable for web scraping.

select.rs A library to extract useful data from HTML documents, suitable for web scraping. NOTE: The following example only works in the upcoming rele

Utkarsh Kukreti 829 Dec 28, 2022
📮 An elegant Telegram bots framework for Rust

teloxide A full-featured framework that empowers you to easily build Telegram bots using the async/.await syntax in Rust. It handles all the difficult

teloxide 1.6k Jan 3, 2023
Sōzu HTTP reverse proxy, configurable at runtime, fast and safe, built in Rust. It is awesome! Ping us on gitter to know more

Sōzu · Sōzu is a lightweight, fast, always-up reverse proxy server. Why use Sōzu? Hot configurable: Sozu can receive configuration changes at runtime

sōzu 2k Dec 30, 2022
A Rust application which funnels external webhook event data to an Urbit chat.

Urbit Webhook Funnel This is a simple Rust application which funnels external webhook event data to an Urbit chat. This application is intended to be

Robert Kornacki 15 Jan 2, 2022
A html document syntax and operation library written in Rust, use APIs similar to jQuery.

Visdom A server-side html document syntax and operation library written in Rust, it uses apis similar to jQuery, left off the parts thoes only worked

轩子 80 Dec 21, 2022
Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.

Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.

Actix 16.2k Jan 2, 2023
Starlight is a JS engine in Rust which focuses on performance rather than ensuring 100% safety of JS runtime.

starlight Starlight is a JS engine in Rust which focuses on performance rather than ensuring 100% safety of JS runtime. Features Bytecode interpreter

null 453 Dec 31, 2022
A rust web framework with safety and speed in mind.

darpi A web api framework with speed and safety in mind. One of the big goals is to catch all errors at compile time, if possible. The framework uses

null 32 Apr 11, 2022
A web framework for Rust.

Rocket Rocket is an async web framework for Rust with a focus on usability, security, extensibility, and speed. #[macro_use] extern crate rocket; #[g

Sergio Benitez 19.4k Jan 4, 2023
Rust / Wasm framework for building client web apps

Yew Rust / Wasm client web app framework Documentation (stable) | Documentation (latest) | Examples | Changelog | Roadmap | 简体中文文档 | 繁體中文文檔 | ドキュメント A

Yew Stack 25.8k Jan 2, 2023
问卷反馈收集, 前端脚手架安装向导, rust, gtk3, win32, dll

scaffold-wizard 这是一款加持了【图形用户界面】的npm - inquirer(名曰:问卷)。即,根据【问卷】配置文件,以人-机交互的形式,收集终端用户的【回答结果】。这里提到的【问卷配置】与【回答结果】都是*.json格式的字符串(或文件)。 【问卷】既能够作为.exe文件被双击运行

Stuart Zhang 40 Dec 7, 2022
Moodle CMS Notifications in Rust

Moodle CMS Notifications View unread Moodle CMS notifications. Mark all notifications as read. Lightweight with no dependencies. Cross platform. Authe

Divyanshu Agrawal 4 Sep 29, 2022
Diana is a GraphQL system for Rust that's designed to work as simply as possible out of the box

Diana is a GraphQL system for Rust that's designed to work as simply as possible out of the box, without sacrificing configuration ability. Unlike other GraphQL systems, Diana fully supports serverless functions and automatically integrates them with a serverful subscriptions system as needed, and over an authenticated channel. GraphQL subscriptions are stateful, and so have to be run in a serverful way. Diana makes this process as simple as possible.

null 0 Aug 3, 2021