Skip to content

keep matching other patterns when the last segment is a bare verb - #7539

Merged
johanbrandhorst merged 1 commit into
grpc-ecosystem:mainfrom
cvas22:fix-bare-verb-routing
Oct 19, 2026
Merged

johanbrandhorst merged 1 commit into
grpc-ecosystem:mainfrom
cvas22:fix-bare-verb-routing

Conversation

@cvas22

@cvas22 cvas22 commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

References to other Issues or PRs

None. This is a follow-up to #1947, which introduced the current check.

Have you read the Contributing Guidelines?

Yes

Brief description of what is fixed or changed

When the last path segment is just a pattern's verb with nothing before it (e.g. GET /v1/:cancel), ServeMux.ServeHTTP returned 404 as soon as it reached a handler whose pattern has that verb. It didn't go on to check the remaining handlers. Handlers are checked newest-first, so the outcome depended on registration order:

mux.HandlePath("GET", "/v1/{id}", ...)
mux.HandlePath("GET", "/v1/{name}:cancel", ...)
// GET /v1/:cancel -> 404

mux.HandlePath("GET", "/v1/{name}:cancel", ...)
mux.HandlePath("GET", "/v1/{id}", ...)
// GET /v1/:cancel -> served by /v1/{id} with id=":cancel"

Before #1947 this was a global up-front check: any last segment starting with : got a 404. #1947 moved it into the per-handler loop. Since then it only fires for some patterns, and only if one of them is reached first. The method-fallback loop below it has no such check at all.

A pattern with a verb can't match an empty segment before the verb, so this PR skips that handler (continue) instead of ending the search. The result no longer depends on registration order, and the two loops behave the same. When no other pattern matches, the request still ends in a 404.

TestServeMux_BareVerbDoesNotDependOnRegistrationOrder covers both registration orders and the only-verb-pattern case. The order-dependent case fails on main.

Other comments

@cvas22
cvas22 force-pushed the fix-bare-verb-routing branch from 8283e84 to b9b1e53 Compare October 19, 2026 04:31
@cvas22

cvas22 commented Oct 19, 2026

Copy link
Copy Markdown
Contributor Author

Rebased and resolved merge conflict.

@johanbrandhorst
johanbrandhorst merged commit 882771e into grpc-ecosystem:main Oct 19, 2026
14 checks passed
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 this pull request may close these issues.

2 participants