go-chi / chi

lightweight, idiomatic and composable router for building Go HTTP services
https://go-chi.io
MIT License
18.54k stars 988 forks source link

Not all middlewares are present in `chi.Walk` #931

Open dils2k opened 4 months ago

dils2k commented 4 months ago

Consider this example:

mux := chi.NewMux()
mux.With(middleware).Group(func(r chi.Router) {
    r.Route("/foo", func(r chi.Router) {
        r.With(middleware).Post("/bar", func(w http.ResponseWriter, r *http.Request) {})
    })
})

/foo/bar route is expected to have two middlewares: one inherited from the group, second defined for the route itself.

But when I use chi.Walk function to get all routes with corresponding middlewares /foo/bar route has only one middleware:

chi.Walk(mux, func(method, route string, handler http.Handler, middlewares ...func(http.Handler) http.Handler) error {
    fmt.Println(middlewares) // it prints [0x1047fcba0], array with only one element, but we expected two elements
    return nil
})
VojtechVitek commented 1 month ago

Interesting, thank you. Mind investigating further, @dils2k?

dils2k commented 1 month ago

Interesting, thank you. Mind investigating further, @dils2k?

yup, will try my best