loco-openapi-initializer
This crate adds OpenAPI support to Loco by using a initializer.
The Loco OpenAPI integration is generated using Utoipa
Installation
Cargo.toml
Edit your Cargo.toml file
Add the loco-openapi initializer, with one or multiple of the following features flags:
swaggerredocscalarfull
Example
# Cargo.toml
[]
= { = "*", = [
"full",
], = "https://github.com/loco-rs/loco-openapi-Initializer", = "main" }
Configuration
Add the corresponding OpenAPI visualizer to the config file
# config/*.yaml
#...
initializers:
openapi:
redoc:
url: /redoc
# spec_json_url: /redoc/openapi.json
# spec_yaml_url: /redoc/openapi.yaml
scalar:
url: /scalar
# spec_json_url: /scalar/openapi.json
# spec_yaml_url: /scalar/openapi.yaml
swagger:
url: /swagger
spec_json_url: /api-docs/openapi.json # spec_json_url is required for swagger-ui
# spec_yaml_url: /api-docs/openapi.yaml
Adding the OpenAPI initializer
In the initializer you can modify the OpenAPI spec before the routes are added, allowing you to edit openapi::info
// src/app.rs
use *;
async
Usage
Generating the OpenAPI spec
Only routes that are annotated with utoipa::path will be included in the OpenAPI spec.
use *;
/// Your Title here
///
/// Your Description here
async
#[derive(ToSchema)]
Make sure to add #[derive(ToSchema)] on any struct that included in utoipa::path.
use *;
Automatically adding routes to the OpenAPI spec visualizer
Swap axum::routing::MethodRouter to openapi(MethodRouter<AppContext>, UtoipaMethodRouter<AppContext>)
+ use loco_openapi::prelude::*;
Routes::new()
.prefix("api/album/")
- .add("/get_album", get(get_album)),
+ .add("/get_album", openapi(get(get_album), routes!(get_album))),
In the initializer, if you are only using automatic schema collection, set the second arg to None, to disable manual schema collection
use *;
async
Note: do not add multiple routes inside the routes! macro
.add,
Manualy adding routes to the OpenAPI spec visualizer
Create a function that returns OpenApiRouter<AppContext>
use *;
Then in the initializer, create a Vec<OpenApiRouter<AppContext>>
use *;
async
Using both manual and automatic schema collection
It's possible to use both manual and automatic schema collection at the same time. But make sure to only add each route once.
Using both of the examples above, at the same time will not work, since the route /get_album will be added twice.
// controllers
use *;
// initializers
use *;
async
Security Documentation
If modifiers(&SecurityAddon) is set in inital_openapi_spec, you can document the per route security in utoipa::path by setting the SecurityRequirement:
security(("jwt_token" = []))security(("api_key" = []))
To remove security from the route:
- remove
securityfromutoipa::path - or leave empty
security(())
Example:
use *;
Available Endpoints
After running cargo loco start the OpenAPI visualizers are available at the following URLs by default:
To customize the OpenAPI visualizers URLs,and endpoint paths for json and yaml, see config/*.yaml.
Testing with loco-openapi-initializer installed
Because of global shared state issues when using automatic schema collection, it's recommended to disable the loco-openapi-initializer when running tests in your application.
async
Alternatively you could use cargo nextest. This issue is not relevant when using the loco-openapi-initializer for normal use.