fiber
fiber copied to clipboard
🐛 [Bug]: Custom context not carried forward with middleware.
Bug Description
Issue 1
I had to use the following signature for custom context
type MyContext struct {
fiber.DefaultCtx
}
app.NewCtxFunc(func(app *fiber.App) fiber.CustomCtx { /* correction: fiber.Ctx is documented */
return &MyContext{
DefaultCtx: *fiber.NewDefaultCtx(app), /* correction: Ctx is documented */
}
})
Issue 2
app := fiber.New()
app.NewCtxFunc(func(app *fiber.App) fiber.CustomCtx {
return &MyContext{
DefaultCtx: *fiber.NewDefaultCtx(app),
}
})
app.Use(logger.New()) /* when used MyContext is not carried forward */
app.Get("/", func(c fiber.Ctx) error {
myCtx := c.(*MyContext)
}
app.Listen(":8080")
panic: as c here is *DefaultCtx
How to Reproduce
Run and make request http://localhost:8080/
Expected Behavior
myCtx := c.(*MyContext)
this should work even when middleware is used.
Fiber Version
3.0.0-beta4
Code Snippet (optional)
Checklist:
- [x] I agree to follow Fiber's Code of Conduct.
- [x] I have checked for existing issues that describe my problem prior to opening this one.
- [x] I understand that improperly formatted bug reports may be closed without explanation.
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
we will check this in the https://github.com/gofiber/fiber/pull/3296 PR there we rewrote the custom context initiailisation
What if embedded Defaultcontext keeps a reference to the custom context so it can refer to it
Is there any update on this? It will be very beneficial to have a custom structure that could be use to store known values and avoid the cost of storing and loading the value from fasthttp UserData