This projects serves to enable automatic rendering of openapi.json files, and provides
facilities to also serve the documentation alongside your api.
Usage
First, add the following lines to your Cargo.toml
[]
= { = "0.5.0-rc.1", = false, = ["json"] }
= "0.8"
= { = "0.6.0-alpha-1" }
= { = "0.8.0-alpha-1", = ["swagger"] }
To add documentation to a set of endpoints, a couple of steps are required. The request and
response types of the endpoint must implement JsonSchema. Secondly, the function must be
marked with #[openapi]. After that, you can simply replace routes! with
openapi_get_routes!. This will append an additional route to the resulting Vec<Route>,
which contains the openapi.json file that is required by swagger. Now that we have the json
file that we need, we can serve the swagger web interface at another endpoint, and we should be
able to load the example in the browser!
Example
use get;
use Json;
use ;
use ;
This crate exposes a few macros that can be used to generate and serve routes and OpenApi objects.
mount_endpoints_and_merged_docs!{...}: Mount endpoints and mount merged OpenAPI documentation.openapi_get_routes![...]: To generate and add theopenapi.jsonroute.openapi_get_routes_spec![...]: To generate and return a list of routes and the openapi spec.openapi_get_spec![...]: To generate and return the openapi spec.
The last 3 macros have very similar behavior, but differ in what they return. Here is a list of the marcos and what they return:
openapi_get_routes![...]:Vec<rocket::Route>(adds route foropenapi.json)openapi_get_routes_spec![...]:(Vec<rocket::Route>, okapi::openapi3::OpenApi)openapi_get_spec![...]:okapi::openapi3::OpenApi