utoipa-swagger-ui
This crate implements necessary boilerplate code to serve Swagger UI via web server. It works as a bridge for serving the OpenAPI documentation created with utoipa library in the Swagger UI.
Currently implemented boilerplate for:
- actix-web
version >= 4 - rocket
version >=0.5 - axum
version >=0.7
Serving Swagger UI is framework independent thus this crate also supports serving the Swagger UI with other frameworks as well. With other frameworks, there is a bit more manual implementation to be done. See more details at serve or examples.
Crate Features
actix-webEnables actix-web integration with pre-configured SwaggerUI service factory allowing users to use the Swagger UI without a hassle.rocketEnables rocket integration with pre-configured routes for serving the Swagger UI and api doc without a hassle.axumEnablesaxumintegration with pre-configured Router serving Swagger UI and OpenAPI specs hassle free.debug-embedEnablesdebug-embedfeature onrust_embedcrate to allow embedding files in debug builds as well.reqwestUsereqwestfor downloading Swagger UI according to theSWAGGER_UI_DOWNLOAD_URLenvironment variable. This is only enabled by default on Windows.urlEnabled by default for parsing and encoding the download URL.vendoredEnables vendored Swagger UI viautoipa-swagger-ui-vendoredcrate.cacheEnables caching of the Swagger UI download inutoipa-swagger-uiduring the build process.debug: Implement debug trait for SwaggerUi and other types.
Install
Use only the raw types without any boilerplate implementation.
[]
= "9"
Enable actix-web framework with Swagger UI you could define the dependency as follows.
[]
= { = "9", = ["actix-web"] }
Note! Also remember that you already have defined utoipa dependency in your Cargo.toml
Build Config
[!IMPORTANT]
utoipa-swagger-uicrate will by default try to use systemcurlpackage for downloading the Swagger UI. It can optionally be downloaded withreqwestby enablingreqwestfeature. Reqwest can be useful for platform independent builds however bringing quite a few unnecessary dependencies just to download a file. If theSWAGGER_UI_DOWNLOAD_URLis a file path then no downloading will happen.
[!TIP] Use
vendoredfeature flag to use vendored Swagger UI. This is especially useful for no network environments.
The following configuration env variables are available at build time:
-
SWAGGER_UI_DOWNLOAD_URL: Defines the url from where to download the swagger-ui zip file.- Current Swagger UI version: https://github.com/swagger-api/swagger-ui/archive/refs/tags/v5.17.14.zip
- All available Swagger UI versions
-
SWAGGER_UI_OVERWRITE_FOLDER: Defines an optional absolute path to a directory containing files to overwrite the Swagger UI files. Typically you might want to overwriteindex.html.
Examples
Serve Swagger UI with api doc via actix-web. See full example from examples.
new
.bind.unwrap
.run;
Serve Swagger UI with api doc via rocket. See full example from examples.
Setup Router to serve Swagger UI with axum framework. See full implementation of how to serve
Swagger UI with axum from examples.
let app = new
.merge;
License
Licensed under either of Apache 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, shall be dual licensed, without any additional terms or conditions.