GREsau / okapi

OpenAPI (AKA Swagger) document generation for Rust projects
MIT License
578 stars 103 forks source link

Avoid using openapi_get_routes! #101

Closed ryadom closed 2 years ago

ryadom commented 2 years ago

It seems that it's hard to use openapi_get_routes in big projects. In order to use it I need one big list of all methods in the API. My rocket code is structured in the following way:

mod auth {
  pub fn get_routes() -> Vec<Route> {
      routes![login, register]
  }
}

mod apis {
  pub fn get_routes() -> Vec<Route> {
      vec![
          auth::get_routes().prefix_routes("/auth"),
          customers::get_routes(),
          products::get_routes(),
      ]
      .into_iter()
      .flatten()
      .collect()
  }
}

trait RoutePrefixerEx {
    fn prefix_routes(self, prefix: &str) -> Vec<Route>;
}
// impl RoutePrefixerEx for Vec<Route> is skipped.

pub fn rocket() -> rocket::Rocket<rocket::Build> {
    rocket::new()
        // Some code is missing
        .mount("/api2", apis::get_routes())
}

Having this structure it's hard to use openapi_get_routes! Is it possible to rewrite openapi_get_routes! as a runtime function that accepts Vec?

ralpha commented 2 years ago

This example shows how you can device big projects into smaller parts (modules) and combine them into 1 openAPI structure. https://github.com/GREsau/okapi/tree/master/examples/custom_schema

It uses openapi_get_routes_spec! (This is slightly different from openapi_get_routes.) and mount_endpoints_and_merged_docs. There are also a few other macros since a few releases ago. https://docs.rs/rocket_okapi/0.8.0-rc.1/rocket_okapi/index.html#macros

You can find more info about them higher on the page (between "Example" and "Re-exports")

If that does not fill the need, let me know. You can also create your own macro like mount_endpoints_and_merged_docs https://docs.rs/rocket_okapi/0.8.0-rc.1/src/rocket_okapi/lib.rs.html#154-179 But I created it a way that it allows for some flexibility.

ryadom commented 2 years ago

It seems that it's exactly what I was looking for. Totally missed this example. Thanks for help!