-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
🐛 [Bug]: Middleware Not Intercepting Requests When Route Registration Parameter Name Matches Group Name #3269
Comments
Thanks for opening your first issue here! 🎉 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord |
package main
import (
"log"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/recover"
)
var logGrouoHandler = func(c *fiber.Ctx) error {
log.Println("Group: Request URL:", c.OriginalURL(), "Route:", c.Route().Path, "Method:", c.Method())
return c.Next()
}
var logChildHandler = func(c *fiber.Ctx) error {
log.Println("Child: Request URL:", c.OriginalURL(), "Route:", c.Route().Path, "Method:", c.Method())
return c.SendString("Hello, World!")
}
func main() {
// Initialize Fiber router
router := fiber.New()
router.Use(recover.New())
// Apply global middlewares
api := router.Group(
"/api/v1/agency",
logGrouoHandler,
)
// Register Reservation API
registerReservationAPI(api)
// Start the server
log.Fatal(router.Listen(":3000"))
}
func registerReservationAPI(api fiber.Router) {
api.Get("/reservation/:agencyID", logChildHandler)
} Test request to : @mohash25 can you try to prepare my simple example so that it is not too complex, but still shows the error ? i could not recreate it now |
@mohash25 i had to change the structure a bit because i don't have all the resources you used in your example but i think it has to do with the same naming of the variables |
i found that the issue was with my debugger sorry to bother |
Problem
When defining a route registration function with a parameter name identical to the
fiber.Router
group (e.g.,api
), the middleware does not intercept requests. This behavior is unexpected, as the middleware should consistently apply regardless of the parameter name.Code Example
The following function works correctly:
However, renaming the
router
parameter toapi
causes the middleware to stop intercepting requests:Environment
1.23.2
v2.52.5
Windows 10
How to Reproduce
Define a Fiber route group with global middleware:
Create a route registration function with
fiber.Router
as a parameter:Observe that the middleware is no longer triggered for
/api/v1/agency/reservation/:agencyID
.Rename the parameter to something other than
api
, e.g.,router
, and observe that the middleware now works as expected.Expected Behavior
Middleware should intercept requests regardless of the parameter name used in route registration functions.
Fiber Version
v2.52.5
Code Snippet (optional)
The text was updated successfully, but these errors were encountered: