Skip to main content

Crate openapi_ui

Crate openapi_ui 

Source
Expand description

§openapi-ui

Build Status Crates.io Documentation License: MIT

openapi-ui is a library for generating custom documentation UIs from OpenAPI specifications.

It takes an OpenAPI JSON string and produces a self-contained HTML file with a responsive UI.

🌐 Live Demo

§Web Framework Examples

§Generating OpenAPI JSON with utoipa

The OpenAPI JSON can be generated using utoipa:

use utoipa::OpenApi;
use openapi_ui::{generate_docs, ThemeMode};

#[derive(OpenApi)]
#[openapi(paths(get_users), components(schemas(User)))]
struct ApiDoc;

let openapi_json = ApiDoc::openapi().to_pretty_json().unwrap();
let html = generate_docs(&openapi_json, ThemeMode::System, None, None).unwrap();

§Usage

use openapi_ui::{generate_docs, ThemeMode};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Use the sample Petstore API spec (or your own OpenAPI JSON)
    let openapi_json = include_str!("sample_data.json");

    // Generate HTML with system theme and default favicon
    let html = generate_docs(openapi_json, ThemeMode::System, None, None)?;

    std::fs::write("docs.html", html)?;
    Ok(())
}

For more control, use the UIBuilder:

use openapi_ui::{UIBuilder, OpenAPISpec};
use std::fs;

// Load your OpenAPI spec from file
let spec_json = fs::read_to_string("openapi.json")?;
let spec: OpenAPISpec = serde_json::from_str(&spec_json)?;

let html = UIBuilder::new(spec)
    .theme("dark")
    .base_url("https://api.example.com")
    .build();

fs::write("docs.html", html)?;

Re-exports§

pub use error::Result;
pub use error::UIError;
pub use openapi::HttpScheme;
pub use openapi::OpenAPISpec;
pub use openapi::SchemaType;
pub use theme::get_complete_theme_css;
pub use theme::get_theme_css;
pub use theme::ThemeMode;
pub use ui::generate_base_ui;
pub use ui::generate_docs;
pub use ui::generate_ui;
pub use ui::UIBuilder;
pub use ui::UIConfig;

Modules§

error
Error types for the openapi-ui crate.
openapi
Types matching the OpenAPI 3.x specification.
template
HTML template rendering for the documentation UI.
theme
Built-in light and dark themes.
ui
High-level API for generating documentation HTML.