actix-web icon indicating copy to clipboard operation
actix-web copied to clipboard

URL dispatch depends on route registration order rather than convention

Open vbakc opened this issue 2 years ago • 6 comments

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

  • Rust Version (I.e, output of rustc -V): rustc 1.62.1 (e092d0b6b 2022-07-16)
  • Actix Web Version: 4.2.1

vbakc avatar Oct 02 '22 12:10 vbakc

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

robjtede avatar Oct 02 '22 21:10 robjtede

@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> {}
}

vbakc avatar Oct 12 '22 10:10 vbakc

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...

ilka-schulz avatar Feb 15 '23 13:02 ilka-schulz

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

anhnguyensgu avatar Apr 02 '23 16:04 anhnguyensgu

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.

varshard avatar Nov 28 '23 17:11 varshard

i have same problem

MateSoftware2023 avatar Nov 30 '23 12:11 MateSoftware2023