OpenAPI

The OpenAPI Specification is a specification for a machine-readable interface definition language for describing, producing, consuming and visualizing web services.

Usage with Rust

The rust crate utoipa allows auto-generating the OpenAPI specification.

Models

use utoipa::ToSchema;
 
#[derive(ToSchema)]
struct Book {
    id: u32,
    title: String,
    author: String,
}

API Path

use utoipa::OpenApi;
 
#[utoipa::path(
    get,
    path = "/book/{id}",
    responses(
        (status = 200, description = "Book found", body = Book),
        (status = 404, description = "Book not found")
    ),
    params(
        ("id" = u32, Path, description = "Book ID")
    )
)]
async fn get_book(id: u32) -> Option<Book> {
    // implementation here
    // returning Some or None
}

Full Spec

#[derive(OpenApi)]
#[openapi(paths(get_book), components(schemas(Book)))]
struct ApiDoc;
 
fn main() {
    let openapi = ApiDoc::openapi();
    println!("{}", serde_json::to_string_pretty(&openapi).unwrap());
}

Tools

A comprehensive list of tools can be found here.