fiber icon indicating copy to clipboard operation
fiber copied to clipboard

🐛 [Bug]: Custom context not carried forward with middleware.

Open prasad83 opened this issue 8 months ago • 3 comments

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.

prasad83 avatar Apr 08 '25 10:04 prasad83

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

welcome[bot] avatar Apr 08 '25 10:04 welcome[bot]

we will check this in the https://github.com/gofiber/fiber/pull/3296 PR there we rewrote the custom context initiailisation

ReneWerner87 avatar Apr 08 '25 10:04 ReneWerner87

What if embedded Defaultcontext keeps a reference to the custom context so it can refer to it

pjebs avatar Apr 11 '25 23:04 pjebs

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

Doout avatar Nov 05 '25 15:11 Doout