Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested routes? #137

Closed
maximeborges opened this issue Dec 17, 2023 · 3 comments
Closed

Nested routes? #137

maximeborges opened this issue Dec 17, 2023 · 3 comments
Labels
enhancement New feature or request rocket-okapi This affects the rocket-okapi crate rocket-okapi-codegen This affects the rocket-okapi-codegen crate

Comments

@maximeborges
Copy link
Contributor

Hello, I've been trying to nest routes in a somewhat clean way, but I couldn't manage to do that with the proposed macros.
The idea is to include the routes from a module, adding a sub-path in the URI, and this module get more nested routes adding even more sub-path. All of that without having to specify all the routes at the top level (or second level by using mount_endpoints_and_merged_docs).

It's a bit late here, so here is some code which might makes my explanation a bit more clear (hopefully), based on the custom_schema example:
https://github.com/maximeborges/okapi/blob/ac9e23b6383591d8e9d69d742112679d163fa899/examples/nested/src/api/mod.rs

Basically in the main:

mount_endpoints_and_merged_docs! {
        building_rocket, "/v1".to_owned(), openapi_settings,
        "/external" => custom_route_spec,
        "/api" => api::get_routes_and_docs(&openapi_settings),
    };

and in the api module, which has post and message as sub-module:

pub fn get_routes_and_docs(settings: &OpenApiSettings) -> (Vec<rocket::Route>, OpenApi) {
    let mut routes = vec![];
    let mut openapi_list: Vec<(_, rocket_okapi::okapi::openapi3::OpenApi)> = Vec::new();

    [
        ("/posts", post::get_routes_and_docs(settings)),
        ("/message", message::get_routes_and_docs(settings)),
    ]
    .into_iter()
    .for_each(|(path, (new_routes, openapi))| {
        let new_routes = new_routes
            .into_iter()
            .map(|r: Route| r.map_base(|base| format!("{}{}", path, base)).unwrap())
            .collect::<Vec<_>>();
        routes.extend(new_routes);
        openapi_list.push((path, openapi));
    });

    let openapi_docs = match rocket_okapi::okapi::merge::marge_spec_list(&openapi_list) {
        Ok(docs) => docs,
        Err(err) => panic!("Could not merge OpenAPI spec: {}", err),
    };

    (routes, openapi_docs)
}

Is there a better way of doing that, or should I propose a PR to have some function/macro for that?

@ralpha ralpha added enhancement New feature or request rocket-okapi This affects the rocket-okapi crate rocket-okapi-codegen This affects the rocket-okapi-codegen crate labels Dec 17, 2023
@ralpha
Copy link
Collaborator

ralpha commented Dec 17, 2023

Hey, in short, no, we do not have a very good solution for this at the moment.
A solution for this would be welcome.

Not sure how we should best go about implementing this. Ether we extend mount_endpoints_and_merged_docs and/or openapi_get_routes. But they might not be the right place to do this.
So yes, maybe creating a new macro that does the combining is the best solution.

I see you already took inspiration from both macro's. Feel free to create a MR.
If the MR is created soon I might be able to add before the next release.

@ralpha
Copy link
Collaborator

ralpha commented Jan 14, 2024

Closed because #138 was merged

@ralpha ralpha closed this as completed Jan 14, 2024
@ralpha
Copy link
Collaborator

ralpha commented Jan 14, 2024

@maximeborges Thanks for the MR. ❤️ (and sorry for the delay)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request rocket-okapi This affects the rocket-okapi crate rocket-okapi-codegen This affects the rocket-okapi-codegen crate
Projects
None yet
Development

No branches or pull requests

2 participants