Crate utoipa_scalar

source ·
Expand description

This crate works as a bridge between utoipa and Scalar OpenAPI visualizer.

Utoipa-scalar provides simple mechanism to transform OpenAPI spec resource to a servable HTML file which can be served via predefined framework integration or used standalone and served manually.

You may find fullsize examples from utoipa’s Github repository.

§Crate Features

  • actix-web Allows serving Scalar via actix-web.
  • rocket Allows serving Scalar via rocket.
  • axum Allows serving Scalar via axum.

§Install

Use Scalar only without any boiler plate implementation.

[dependencies]
utoipa-scalar = "0.1"

Enable actix-web integration with Scalar.

[dependencies]
utoipa-scalar = { version = "0.1", features = ["actix-web"] }

§Using standalone

Utoipa-scalar can be used standalone as simply as creating a new Scalar instance and then serving it by what ever means available as text/html from http handler in your favourite web framework.

Scalar::to_html method can be used to convert the Scalar instance to a servable html file.

let scalar = Scalar::new(ApiDoc::openapi());

// Then somewhere in your application that handles http operation.
// Make sure you return correct content type `text/html`.
let scalar_handler = move || {
    scalar.to_html()
};

§Examples

Serve Scalar via actix-web framework.

use actix_web::App;
use utoipa_scalar::{Scalar, Servable};

App::new().service(Scalar::with_url("/scalar", ApiDoc::openapi()));

Serve Scalar via rocket framework.

use utoipa_scalar::{Scalar, Servable};

rocket::build()
    .mount(
        "/",
        Scalar::with_url("/scalar", ApiDoc::openapi()),
    );

Serve Scalar via axum framework.

use axum::Router;
use utoipa_scalar::{Scalar, Servable};

let app = Router::<S>::new()
    .merge(Scalar::with_url("/scalar", ApiDoc::openapi()));

Use Scalar to serve custom OpenAPI spec using serde’s json!() macro.

Scalar::new(json!({"openapi": "3.1.0"}));

Structs§

Traits§

  • Servableactix-web or rocket or axum
    Trait makes Scalar to accept an URL the Scalar will be served via predefined web server.
  • Trait defines OpenAPI spec resource types supported by Scalar.