Actix-web: If two routes are defined via codegen with the same path but different methods only one works

Created on 30 Apr 2019  路  9Comments  路  Source: actix/actix-web

For example if I have a function annotated with

[delete("/thepath")]

and another

[get("/thepath")]

Only the first one registered as a service will resolve and the other returns 404.

P-codegen

Most helpful comment

Yes, this is clear. It is a bug. I鈥檒l fix it later

All 9 comments

This is when adding the routes to a ServiceConfig ie

cfg.service(handler::delete).service(handler::get)

I tried to work around this using the guard option from codegen but apparently it doesn't try the next matching request if the guard fails because the result is the same.

Since attributes are parsed independently they are not really aware of each other.

Since path attribute overwrite code and remove next attribute, it is not going to be parsed(otherwise it would cause error)
The fix would require for attribute to parse all other attributes that follow it.

I see how that might be complicated but from a usability standpoint I don't think this can be ignored. If your working on a RESTful API then you are going to have a resource that has different handlers for each HTTP method.

Just to make sure we have maximum understanding I'm going to give a more realistic example of two handlers.

#[get("/thething")]
fn get_thing(req: HttpRequest) -> HttpResponse {
// Retrieves a thing
}

#[post("/thething")]
fn create_thing(req: HttpRequest) -> HttpResponse {
// Creates a thing
}

// Meanwhile in the App configuration
.service(create_thing).service(get_thing)

// Today only one of those routes works.

Yes, this is clear. It is a bug. I鈥檒l fix it later

please provide PR with test case if you still see the problem

Now that I look back I'm pretty sure I was using a manually defined resource as the post or put and the get using codegen. In those cases I can't really use the codegen until I can provide a global JSON Config.

Was this page helpful?
0 / 5 - 0 ratings