oasgen
- OpenAPI Spec Generator
oasgen
is a library to generate OpenAPI 3.0 specs from Rust server code (or any async functions). It supports:
actix
- actix-webaxum
- axum- No framework - if you just want to register Rust functions to generate an OpenAPI spec file.
Contributions to support other web frameworks are welcome!
Example
// Actix-web example
use Json;
use ;
use ;
use ;
async
async
// axum example
use ;
use ;
use ;
async
async
To compile the axum example, use the following dependencies:
[]
= ".."
= { = "..", = ["axum"] }
= { = "..", = ["derive"] }
= { = "..", = ["full"] }
Installation
[]
# At minimum, you probably want a server feature installed (axum, actix) to support that framework
= { = "..", = []}
There are several features for activating other libraries:
actix
- actix-webaxum
- axumswagger-ui
- swagger uiuuid
- uuidchrono
- chronotime
- timesqlx
- sqlx
Customizing the generated spec
You can customize the generated spec in many ways.
Direct access to the OpenAPI struct
You have direct access to the OpenAPI struct, so you can customize it however you want.
let mut server = new;
server.openapi.info.title = "My API".to_string;
server.openapi.components.schemas.insert;
server
.get
.freeze;
Note that you must make any changes before calling .freeze()
(which moves the OpenAPI struct into an Arc to be shared between threads).
Customizing a Schema
You can hand-write an implementation of OaSchema instead of using derive to customize any Schema. If you do this, call
register_schema
after the struct definition to add it to the spec.
use ;
register_schema!;
Technically speaking, you don't need to implement OaSchema at all.
You can pass any arbitrary closure that returns a Schema
to the register_schema macro.
You can also customize an operation:
async // You must use the fully qualified path to the function.
// You can simplify this slightly by passing in `concat!(module_path!(), "::my_server_handler")`
register_operation!;
Attributes
oasgen
defines its own attributes, and also respects serde
attributes. It also uses docstrings as descriptions.
You can see all attributes in macro/src/attr.rs
. Look at those structs for relevant documentation, and see the examples below.
async
Write the spec to a file
You have direct access to the OpenAPI
struct. You can use serde
to write it to a file, stdout, and more.
We provide a helper function write_and_exit_if_env_var_set
that integrates well with a basic build process:
let server = new
// your routes
.write_and_exit_if_env_var_set
// .freeze() here, if you're mounting to a server.
If OASGEN_WRITE_SPEC=1
, it will write the spec to the path, then exit.
In your build process, build the executable, run it once with the env var set to output the spec, then run it again without the env var to start the server normally.
Route that displays the spec
[!NOTE]
Requires theswagger-ui
feature
There are built-in functions to create routes that display the raw spec, or display a Swagger UI page for the spec.
let mut server = axum
.post // example route
.route_yaml_spec // the spec will be available at /openapi.yaml
.route_json_spec // the spec will be available at /openapi.json
.swagger_ui; // the swagger UI will be available at /openapi/.
// NOTE: The trailing slash is required, as is calling either `route_yaml_spec()` or `route_json_spec()` before `swagger_ui()`.
If you need to customize these routes, you have directly use a clone of the OpenAPI struct. It's in an Arc, so it's cheap to clone.
let mut server = axum
.post // example route
.freeze;
let spec = server.openapi.clone;
let router = new
.merge
.route
;