Skip to content

Fix SupressNotFound making subrouters return 404 and add tests for SupressNotFound#940

Open
ezraisw wants to merge 6 commits into
go-chi:masterfrom
ezraisw:master
Open

Fix SupressNotFound making subrouters return 404 and add tests for SupressNotFound#940
ezraisw wants to merge 6 commits into
go-chi:masterfrom
ezraisw:master

Conversation

@ezraisw

@ezraisw ezraisw commented Jul 26, 2024

Copy link
Copy Markdown

This fixes #939 by using NewRouteContext() to create a new route context before calling the Match function.

Because of Match's behavior of modifying route context, it is necessary to for the route context to be isolated.

chi/mux.go

Lines 361 to 362 in 23c395f

// Note: the *Context state is updated during execution, so manage
// the state carefully or make a NewRouteContext().

Also added tests to enforce this logic.

@ezraisw ezraisw changed the title Fix SupressNotFound making subrouters 404 Fix SupressNotFound making subrouters return 404 and add tests for SupressNotFound Jul 26, 2024
@johnwook

johnwook commented Aug 5, 2024

Copy link
Copy Markdown

wow, I was wondering why. Looks great!

@devhaozi

Copy link
Copy Markdown

I'm having the same problem, @pkieltyka could you take a look at this?

@devhaozi

devhaozi commented Dec 14, 2024

Copy link
Copy Markdown

@VojtechVitek could you take a look at this? It has been around for a long time.

@devhaozi

devhaozi commented Jul 7, 2025

Copy link
Copy Markdown

@VojtechVitek why hasn't this PR been merged yet?

@jay-babu

Copy link
Copy Markdown

This is a pretty glaring bug. I unfortunately spent a couple of hours before realizing this issue

Comment thread middleware/supress_notfound.go Outdated
@ezraisw

ezraisw commented Jul 27, 2025

Copy link
Copy Markdown
Author

FYI, this middleware returns 404 instead of 405 for requests with valid paths that have incorrect HTTP methods. Supporting 405 response would require a bigger change within the chi.Mux implementation.

@ezraisw ezraisw requested a review from VojtechVitek July 27, 2025 13:32
Comment thread middleware/supress_notfound.go Outdated
@ezraisw ezraisw requested a review from VojtechVitek July 28, 2025 13:02

@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.

LGTM. Thank you!

Can someone please help me test this PR and verify its behavior? 👀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sub-routers returning 404 when using middleware.SupressNotFound on parent router

5 participants