Skip to content

🐛 [Bug]: app.Use breaks when using NewWithCustomCtx #3848

@febelery

Description

@febelery

Bug Description

Bug: app.Use breaks when using NewWithCustomCtx

Description

When using fiber.NewWithCustomCtx, any app.Use() middleware causes a panic or context type assertion failure.
Without app.Use, the app works correctly and custom context methods can be used normally.

Example Code

package main

import (
	"log"

	"github.com/gofiber/fiber/v3"
)

type CustomCtx struct {
	fiber.DefaultCtx
}

func (c *CustomCtx) CustomMethod() string {
	return "custom value"
}

func main() {
	app := fiber.NewWithCustomCtx(func(app *fiber.App) fiber.CustomCtx {
		return &CustomCtx{
			DefaultCtx: *fiber.NewDefaultCtx(app),
		}
	})

	app.Use(func(c fiber.Ctx) error {
		return c.Next()
	})

	app.Get("/:id", func(c fiber.Ctx) error {
		customCtx, ok := c.(*CustomCtx)
		if !ok {
			return c.SendString("error")
		}
		return c.SendString(customCtx.CustomMethod())
	})

	log.Fatal(app.Listen(":8080"))
}

How to Reproduce

app.Use(func(c fiber.Ctx) error {
return c.Next()
})

Expected Behavior

package main

import (
	"log"

	"github.com/gofiber/fiber/v3"
)

type CustomCtx struct {
	fiber.DefaultCtx
}

func (c *CustomCtx) CustomMethod() string {
	return "custom value"
}

func main() {
	app := fiber.NewWithCustomCtx(func(app *fiber.App) fiber.CustomCtx {
		return &CustomCtx{
			DefaultCtx: *fiber.NewDefaultCtx(app),
		}
	})

	app.Get("/:id", func(c fiber.Ctx) error {
		customCtx, ok := c.(*CustomCtx)
		if !ok {
			return c.SendString("error")
		}

		return c.SendString(customCtx.CustomMethod())
	})

	log.Fatal(app.Listen(":8080"))
}

Fiber Version

v3.0.0-rc2

Code Snippet (optional)

package main

import (
	"log"

	"github.com/gofiber/fiber/v3"
)

type CustomCtx struct {
	fiber.DefaultCtx
}

func (c *CustomCtx) CustomMethod() string {
	return "custom value"
}

func main() {
	app := fiber.NewWithCustomCtx(func(app *fiber.App) fiber.CustomCtx {
		return &CustomCtx{
			DefaultCtx: *fiber.NewDefaultCtx(app),
		}
	})

	app.Use(func(c fiber.Ctx) error {
		return c.Next()
	})

	app.Get("/:id", func(c fiber.Ctx) error {
		customCtx, ok := c.(*CustomCtx)
		if !ok {
			return c.SendString("error")
		}

		return c.SendString(customCtx.CustomMethod())
	})

	log.Fatal(app.Listen(":8080"))
}

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.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

No status

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions