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

Route layers for route functions #86

Closed
pasha-vuiko opened this issue Nov 14, 2023 · 4 comments · Fixed by #90
Closed

Route layers for route functions #86

pasha-vuiko opened this issue Nov 14, 2023 · 4 comments · Fixed by #90

Comments

@pasha-vuiko
Copy link

First of all, thank you so much for this great library! I enjoy the code first experience with auto generated docs.

Currently for aide::axum::routing::{delete, get, patch, put, post} and other routing functions it is impossible to set route layers.
For example this code is valid for axum::routing::{delete, get, patch, put, post} :

let routes = Router::new()
        .route(
            "/",
            get(expenses_handlers::find_many)
                .route_layer(cache_layer.clone())
                .route_layer(auth_layer.verify(vec![Roles::Admin, Roles::Customer])),
        )
        .route(
            "/:id",
            get(expenses_handlers::find_one)
                .route_layer(cache_layer)
                .route_layer(auth_layer.verify(vec![Roles::Admin, Roles::Customer])),
        )

but with aide::axum::routing functions there is no .route_layer() function, only .layer() function is available, but it is impossible to use multiple .layer() function for a specific route. So it would be great if this code become valid:

let routes = ApiRouter::new()
        .api_route(
            "/",
            get(expenses_handlers::find_many)
                .route_layer(cache_layer.clone()) // TODO Find out how to add multiple layers
                .route_layer(auth_layer.verify(vec![Roles::Admin, Roles::Customer])),
        )
        .api_route(
            "/:id",
            get(expenses_handlers::find_one)
                .route_layer(cache_layer) // TODO Find out how to add multiple layers
                .route_layer(auth_layer.verify(vec![Roles::Admin, Roles::Customer])),
        )

But currently following errors is received:

error[E0599]: no method named `route_layer` found for struct `ApiMethodRouter` in the current scope
  --> src/api/expenses/mod.rs:47:18
   |
46 | /             get(expenses_handlers::find_many)
47 | |                 .route_layer(cache_layer.clone())
   | |                 -^^^^^^^^^^^ method not found in `ApiMethodRouter<_, _>`
   | |_________________|
   | 

error[E0599]: no method named `route_layer` found for struct `ApiMethodRouter` in the current scope
  --> src/api/expenses/mod.rs:53:18
   |
52 | /             get(expenses_handlers::find_one)
53 | |                 .route_layer(cache_layer)
   | |                 -^^^^^^^^^^^ method not found in `ApiMethodRouter<_, _>`
   | |_________________|

@pasha-vuiko
Copy link
Author

pasha-vuiko commented Nov 16, 2023

All you need to do is to add following method right next to layer() method in ApiMethodRouter

    /// This method wraps a route_layer around the [`ApiMethodRouter`]
    /// For further information see [`axum::routing::method_routing::MethodRouter::route_layer`]
    pub fn route_layer<L>(self, layer: L) -> ApiMethodRouter<S, B, Infallible>
        where
            L: Layer<Route<B, Infallible>> + Clone + Send + 'static,
            L::Service: Service<Request<B>, Error = Infallible> + Clone + Send + 'static,
            <L::Service as Service<Request<B>>>::Response: IntoResponse + 'static,
            <L::Service as Service<Request<B>>>::Future: Send + 'static,
    {
        ApiMethodRouter {
            router: self.router.route_layer(layer),
            operations: self.operations,
        }
    }

@pasha-vuiko
Copy link
Author

What do you think?

@tamasfe
Copy link
Owner

tamasfe commented Nov 29, 2023

I've added a PR that aligns the routing interface with axum's that adds this method as well.

@pasha-vuiko
Copy link
Author

thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants