Fix Walk for routes with handlers and subrouters - #1146
Closed
deepakganesh78 wants to merge 1 commit into
Closed
Conversation
Ensure Walk reports handlers registered on the same pattern as a mounted subrouter while still ignoring synthetic mount handlers. Co-authored-by: Copilot <[email protected]>
VojtechVitek
requested changes
Aug 9, 2026
Contributor
There was a problem hiding this comment.
Thanks for digging into #830, @deepakganesh78 — the stub-vs-real-handler diagnosis is right, and sameHandler avoids a real footgun (http.HandlerFunc isn't ==-comparable).
Two gaps, found by checking Routes() directly rather than just Walk:
hs["*"] = mh[mALL].handlerisn't run through thesameHandlercheck just below it, so everyMount()/Route()now leaks two extraRouteentries (the exact pattern + its trailing slash) pointing at the internal mount handler. Invisible viachi.Walkonly becausewalk()always skips"*"— but visible to anything callingRoutes()directly.- The
walk()change (droppingcontinue, forkingsubrouteMw) looks unnecessary — reverting just that half still passes your test and the rest of the suite.
Opened #1148 with both fixed, keeping your test and the sameHandler approach. Its description also credits everyone who touched #830/#750 before this, including the folks who flagged this earlier: @sauerbraten, @ganicus, @lai0xn on #830, and @ebabani, @pkieltyka on #750.
🤖 This message was generated by Claude on behalf of Vojtech.
Contributor
|
Superseded by #1148. Thank you for your contribution! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #830
Reproduction
Register a handler and a subrouter on the same Route() pattern:
GET /foo/baris routable, butchi.Walkonly reportedGET /foo/bar/{id}and omittedGET /foo/bar.Root cause
walkreturned immediately after descending intoroute.SubRoutes, so any real handlers stored on that same route entry were skipped. The route tree also uses stub endpoints for mount connectors; those synthetic handlers need to remain hidden from public route listings.Fix
Continue walking handlers after subroutes, while keeping subrouter middleware propagation separate.
routes()now filters synthetic stub handlers but preserves the internal*handler used to carry inline middleware into subrouters.Compatibility
No API changes and no new dependencies. This only makes
Walk/Routesreport an already-routable handler that was previously omitted; synthetic mount connector routes remain hidden.Validation
go test . -run TestWalkRouteWithHandlerAndSubrouter -count=1fails without the fix:expected Walk to include GET /foo/bar, got map[GET /foo/bar/{id}:true].go build ./...passed.go vet ./...passed.gofmt -l .produced no output.go test ./...passed (github.com/go-chi/chi/v5,github.com/go-chi/chi/v5/middleware).