mux icon indicating copy to clipboard operation
mux copied to clipboard

When I use the subrouter() method, Methods() only works on the last endpoint, and other than the last endpoint, the rest of the endpoints give a 404 instead of a 405 with the unrelated http method.

Open arshamroshannejad opened this issue 1 year ago • 3 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Current Behavior

When I use the subrouter() method, Methods() only works on the last endpoint, and other than the last endpoint, the rest of the endpoints give a 404 instead of a 405 with the unrelated http method.

image

last endpoint : image

other endpoints: image

Expected Behavior

No response

Steps To Reproduce

No response

Anything else?

No response

arshamroshannejad avatar Feb 06 '24 15:02 arshamroshannejad

Does https://github.com/gorilla/mux/pull/748 fix this for you?

bhcleek avatar Feb 27 '24 20:02 bhcleek

Does #748 fix this for you?

no!

arshamroshannejad avatar Mar 07 '24 14:03 arshamroshannejad

This appears to have been broken by https://github.com/gorilla/mux/pull/712, which clears a 405 error when a subsequent route matches in any part. The logic here is tricky: the desire is that if

Route 1: POST "/users/{id}".
Route 2: GET "/users/{id}", parameters: "id": "[0-9]+".

are configured, then GET "/users/-2" returns a 404, not a 405, because while it matches route 1 with a method error, it is a "better" match to route 2 with a parameter validation error.

But this logic then also applies to prefix matching in a sub router: the prefix match is the first matcher in the sequence, and a subsequent route that matches the prefix but is otherwise clearly not a match will clear an existing 405. Unfortunately I don't know the mux internals well enough to suggest a fix, but it seems like if the check from #712 is desired, it shouldn't apply to subroute matchers.

cvermilion avatar Mar 20 '24 15:03 cvermilion