'Custom openapi schema with rust, rocket and okapi

I am developing an API with Rust, using Rocket as main framework. To create the Swagger docs I use Okapi, which allows me to create the docs automatically.

use rocket_okapi::swagger_ui::*;
extern crate dotenv;

use rocket_okapi::{openapi, openapi_get_routes};

#[openapi] // Let okapi know that we want to document this endpoing
#[get("/test_route")]
fn test_route() -> &'static str {
  "test_route"
}

#[rocket::main]
pub async fn main() -> () {
  rocket::build()
  // Mount routes with `openapi_get_routes` instead of rocket `routes``
    .mount("/", openapi_get_routes![test_route])
    // Mount swagger-ui on thisroute
    .mount(
      "/docs",
      make_swagger_ui(&SwaggerUIConfig {
         // The automatically generated openapi.json will be here
        url: "../openapi.json".to_owned(),
        ..Default::default()
      }),
    )
    .launch()
    .await
    .unwrap();

  ()
}

Thats good. But I would like to provide okapi the settings for the API, and I wasnt able to do that. I know there is an example at https://github.com/GREsau/okapi/blob/master/examples/custom_schema/src/main.rs, but I couldnt load a custom OpenApi schema in my api.

Also, I would like to load a custom openapi.json; but I don't know how to achieve that.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source