GREsau / okapi

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

Add macro to simplify nesting route specs #138

Closed maximeborges closed 10 months ago

maximeborges commented 11 months ago

Proposition for #137

Example of usage:

let settings = OpenApiSettings::default();
let custom_route_spec = (vec![], custom_spec());
mount_endpoints_and_merged_docs! {
    building_rocket, "/v1".to_owned(), settings,
    "/" => custom_route_spec,
    "/api" => api::get_routes_and_docs(),
};

mod api {
    pub fn get_routes_and_docs(settings: &OpenApiSettings) -> (Vec<rocket::Route>, OpenApi) {
        get_nested_endpoints_and_docs! {
            "/posts" => post::get_routes_and_docs(settings),
            "/message" => message::get_routes_and_docs(settings),
        }
    }
    mod posts {
        pub fn get_routes_and_docs(settings: &OpenApiSettings) -> (Vec<rocket::Route>, OpenApi) {
            openapi_get_routes_spec![settings: create_post, get_post]
        }
    }
    mod messages {
        pub fn get_routes_and_docs(settings: &OpenApiSettings) -> (Vec<rocket::Route>, OpenApi) {
            openapi_get_routes_spec![settings: create_message, get_message]
        }
    }
}
ralpha commented 11 months ago

Oh nice, that was quick. Looks good from a quick scan. Will review and merge later.

maximeborges commented 10 months ago

Ping just in case you missed the notification for new commits here. No need to rush merging things ;)