actix / actix-web

Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.
https://actix.rs
Apache License 2.0
21.21k stars 1.65k forks source link

URL dispatch depends on route registration order rather than convention #2897

Open vbakc opened 1 year ago

vbakc commented 1 year ago

Expected Behavior

URL dispatch works in the same way regardless of configuration order.

Current Behavior

If a handler with Path extractor is registered before the nested endpoint it will throw an error like this: cannot parse "ROUTE_URL" to a i64

Your Environment

robjtede commented 1 year ago

Can you show an example of a route and handler setup that is surprising to you?

vbakc commented 1 year ago

@robjtede sure.

pub fn configure_routes(config: &mut ServiceConfig) {
    config.service(
        scope("resource-name")
            .service(resource_name::index)
            .service(resource_name::nested_route)
    );
}
mod resource_name {
    #[get("/{id}/")]
    pub async fn index(id: Path<i64>) -> Result<impl Responder> {}

    #[get("/nested-route/")]
    pub async fn nested_route() -> Result<impl Responder> {}
}
ilka-schulz commented 1 year ago

I have the same problem. Actix's behavior is quite frustrating because it makes my app show unexpected behavior. I have hundreds of endpoints registered algorithmically so the solution to Actix's unconventional behavior is unfortunately not trivial for me...

anhnguyensgu commented 1 year ago

Hi guys. Any updates on this issue? I have the same problem.

varshard commented 9 months ago

Look like the issue is how actix store routes. It store routes in a vector and it will iterate through the route vector and return the first route that match. Hence the first registered route is matched first.

matchit is a URL router that can support this kind of behavior.

MateSoftware2023 commented 9 months ago

i have same problem