Skip to content

Fix Walk for routes with handlers and subrouters - #1146

Closed
deepakganesh78 wants to merge 1 commit into
go-chi:masterfrom
deepakganesh78:fix/issue830-walk-route-handler-subrouter
Closed

Fix Walk for routes with handlers and subrouters#1146
deepakganesh78 wants to merge 1 commit into
go-chi:masterfrom
deepakganesh78:fix/issue830-walk-route-handler-subrouter

Conversation

@deepakganesh78

Copy link
Copy Markdown

Fixes #830

Reproduction

Register a handler and a subrouter on the same Route() pattern:

r.Route("/foo", func(r chi.Router) {
    r.Route("/bar", func(r chi.Router) {
        r.Get("/{id}", handler)
    })
    r.Get("/bar", handler)
})

GET /foo/bar is routable, but chi.Walk only reported GET /foo/bar/{id} and omitted GET /foo/bar.

Root cause

walk returned immediately after descending into route.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/Routes report an already-routable handler that was previously omitted; synthetic mount connector routes remain hidden.

Validation

  • Regression test go test . -run TestWalkRouteWithHandlerAndSubrouter -count=1 fails 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).

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 VojtechVitek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. hs["*"] = mh[mALL].handler isn't run through the sameHandler check just below it, so every Mount()/Route() now leaks two extra Route entries (the exact pattern + its trailing slash) pointing at the internal mount handler. Invisible via chi.Walk only because walk() always skips "*" — but visible to anything calling Routes() directly.
  2. The walk() change (dropping continue, forking subrouteMw) 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.

@VojtechVitek

VojtechVitek commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Superseded by #1148.

Thank you for your contribution!

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.

chi.Walk missing routes

2 participants