Okapi

Okapi: Download API Docs

Rocket-Okapi: Download API Docs

unsafe forbidden

Automated OpenAPI (AKA Swagger) document generation for Rust/Rocket projects.

Never have outdated documentation again. Okapi will generate documentation for you while setting up the server. It uses a combination of Rust Doc comments and programming logic to document your API.

The generated OpenAPI files can then be used by various programs to visualize the documentation. Rocket-okapi currently includes RapiDoc and Swagger UI, but others can be used too.

Supported OpenAPI Spec: 3.0.0
Supported Rocket version (for rocket_okapi): 0.5.0-rc.1

Example of generated documentation using Okapi:

Basic Usage

```rust use rocket::{get, post, serde::json::Json}; use revoltrocketokapi::{openapi, openapigetroutes, swagger_ui::*}; use serde::{Deserialize, Serialize}; use schemars::JsonSchema;

// Derive JsonSchema for and request/response models

[derive(Serialize, Deserialize, JsonSchema)]

[serde(rename_all = "camelCase")]

struct User { user_id: u64, username: String, #[serde(default)] email: Option, }

// Add #[openapi] attribute to your routes

[openapi]

[get("/user/")]

fn getuser(id: u64) -> Option> { Some(Json(User { userid: id, username: "bob".to_owned(), email: None, })) }

// You can tag your routes to group them together

[openapi(tag = "Users")]

[post("/user", data = "")]

fn create_user(user: Json) -> Json { user }

// You can skip routes that you don't want to include in the openapi doc

[openapi(skip)]

[get("/hidden")]

fn hidden() -> Json<&'static str> { Json("Hidden from swagger!") }

pub fn makerocket() -> rocket::Rocket { rocket::build() // openapigetroutes![...] will host the openapi document at openapi.json .mount( "/", openapigetroutes![getuser, createuser, hidden], ) // You can optionally host swagger-ui too .mount( "/swagger-ui/", makeswaggerui(&SwaggerUIConfig { url: "../openapi.json".toowned(), ..Default::default() }), ) } ```

More examples

FAQ

```rust use revoltrocketokapi::request::{OpenApiFromRequest, RequestHeaderInput}; use revoltrocketokapi::gen::OpenApiGenerator; use rocketsyncdb_pools::{diesel, database};

[database("sqlite_logs")]

pub struct MyDB;

impl<'r> OpenApiFromRequest<'r> for MyDB { fn fromrequestinput( gen: &mut OpenApiGenerator, _name: String, _required: bool, ) -> revoltrocket_okapi::Result { Ok(RequestHeaderInput::None) } } ```

Feature Flags

Okapi:

Rocket-Okapi:

Note that not all feature flags from Schemars are re-exported or enabled. So if you have objects for which the JsonSchema trait is not implemented, you might need to enable a feature flag in Schemars. For an example see the "uuid" example. (Make sure crate versions match)

How it works

This crate automatically generates an OpenAPI file when the Rocket server starts. The way this is done is shortly described here.

The Schemars crate provides us with the schemas for all the different structures and enums. Okapi does not implement any schemas directly, this is all handled by Schemars.

The Okapi crate just contains all the structures needed to create an OpenAPI file. This crate does not contain any code for the creation of them, just the structure and code to merge two OpenAPI structured together. This crate can be reused to create OpenAPI support in other web framework.

Rocket-Okapi crate contains all the code for generating the OpenAPI file and serve it once created. This code is usually executed using macro's like: mount_endpoints_and_merged_docs!{...}, openapi_get_routes![...], openapi_get_routes_spec![...] and openapi_get_spec![...] .

When the Rocket server is started (or wherever macro is placed) the OpenAPI file is generated once. This file/structure is then stored in memory and will be served when requested.

The Rocket-Okapi-codegen crate contains code for derive macros. #[openapi], rocket_okapi::openapi_spec![...], rocket_okapi::openapi_routes![...] and #[derive(OpenApiFromRequest)] in our case. This needs to be in a separate crate because of Rust restrictions. Note: derive or codegen crates are usually a bit hard to work with then other crates. So it is recommended to get some experience with how derive macros work before you change things in here.

TODO

License

This project is licensed under the MIT license.

All contributions to this project will be similarly licensed.