-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Open
Description
Bug Description
CustomCtx no effect when app.newCtxFunc and app.use simultaneously existing
How to Reproduce
Steps to reproduce the behavior:
- create custom ctx
type Ctx struct {
fiber.DefaultCtx
}
func (c *Ctx) JSON(data any, ctype ...string) error {
return c.DefaultCtx.JSON(Response{
Code: http.StatusOK,
Data: data,
Message: http.StatusText(http.StatusOK),
}, ctype...)
}
type Response struct {
Code int `json:"code,omitempty" xml:"code,omitempty" yaml:"code,omitempty"`
Data any `json:"data,omitempty" xml:"data,omitempty" yaml:"data,omitempty"`
Message string `json:"message,omitempty" xml:"message,omitempty" yaml:"message,omitempty"`
}
- create app with newCtxFunc:
app := fiber.New(fiber.Config{
JSONEncoder: sonic.Marshal,
JSONDecoder: sonic.Unmarshal,
})
app.NewCtxFunc(func(app *fiber.App) fiber.CustomCtx {
return &Ctx{
DefaultCtx: *fiber.NewDefaultCtx(app),
}
})
app.Get("/vvv", func(ctx fiber.Ctx) error {
return ctx.JSON(fiber.Map{"a": "b"})
})
_ = app.Listen(":8080")
- request:
curl localhost:8080/vvv
{"code":200,"data":{"a":"b"},"message":"OK"}%
- create app with newCtxFunc and app.use:
app := fiber.New(fiber.Config{
JSONEncoder: sonic.Marshal,
JSONDecoder: sonic.Unmarshal,
})
app.NewCtxFunc(func(app *fiber.App) fiber.CustomCtx {
return &Ctx{
DefaultCtx: *fiber.NewDefaultCtx(app),
}
})
app.Use(func(c fiber.Ctx) error {
return c.Next()
})
app.Get("/vvv", func(ctx fiber.Ctx) error {
return ctx.JSON(fiber.Map{"a": "b"})
})
_ = app.Listen(":8080")
- request:
curl localhost:8080/vvv
{"a":"b"}
Expected Behavior
app.NewCtxFunc and app.Use can coexist
Fiber Version
v3.0.0-beta.4
Code Snippet (optional)
package main
import "github.com/gofiber/fiber/v3"
import "log"
func main() {
app := fiber.New()
// Steps to reproduce
log.Fatal(app.Listen(":3000"))
}
Checklist:
- I agree to follow Fiber's Code of Conduct.
- I have checked for existing issues that describe my problem prior to opening this one.
- I understand that improperly formatted bug reports may be closed without explanation.