Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ezraisw opened this issue Jul 26, 2024 · 0 comments · May be fixed by #940
Open

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

ezraisw opened this issue Jul 26, 2024 · 0 comments · May be fixed by #940

Comments

@ezraisw
Copy link

ezraisw commented Jul 26, 2024

Description

It seems that sub-routers are not properly matched after I applied middleware.SupressNotFound.
I investigated this and apparently rctx.RoutePath is modified without being restored in middleware.SupressNotFound.
This should be reproducible from the snippet I posted below.

Action

Expected Behavior

  • Go to http://localhost:8889/example/sub/hello
    • I should get "Hello World".
    • Console should print "Route path /sub/hello"

Actual

  • Go to http://localhost:8889/example/sub/hello
    • Instead I got "404 page not found" with 404 status. This is returned by the sub router, not the middleware.
    • Console printed "Route path /hello"

Actual result can be obtained by removing middleware.SupressNotFound.

Code

package main

import (
	"fmt"
	"net/http"

	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
)

func main() {
	mr := chi.NewRouter()

	mr.Route("/example", func(r chi.Router) {
		r.Use(middleware.SupressNotFound(mr))
		r.Use(func(h http.Handler) http.Handler {
			return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
				rctx := chi.RouteContext(r.Context())
				fmt.Println("Route path", rctx.RoutePath)
				h.ServeHTTP(w, r)
			})
		})

		r.Get("/other-hello", func(w http.ResponseWriter, r *http.Request) {
			w.Write([]byte("The Other Hello World"))
		})

		r.Route("/sub", func(r chi.Router) {
			r.Get("/hello", func(w http.ResponseWriter, r *http.Request) {
				w.Write([]byte("Hello World"))
			})
		})

	})

	http.ListenAndServe("0.0.0.0:8889", mr)
}
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 a pull request may close this issue.

1 participant