From 7fd1da800715ac9537974ab82540d5b49debed8c Mon Sep 17 00:00:00 2001 From: Jason McNeil Date: Wed, 19 Mar 2025 12:06:59 -0300 Subject: [PATCH] =?UTF-8?q?Revert=20"=F0=9F=94=A5=20feat:=20Add=20Context?= =?UTF-8?q?=20Support=20to=20RequestID=20Middleware=20(#3200)"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit f725ded92bac13e773f92ff478e1a461c160abd3. --- docs/middleware/requestid.md | 10 ----- middleware/requestid/requestid.go | 25 ++--------- middleware/requestid/requestid_test.go | 61 ++++++-------------------- 3 files changed, 17 insertions(+), 79 deletions(-) diff --git a/docs/middleware/requestid.md b/docs/middleware/requestid.md index 01ec569e..739a4a61 100644 --- a/docs/middleware/requestid.md +++ b/docs/middleware/requestid.md @@ -49,16 +49,6 @@ func handler(c fiber.Ctx) error { } ``` -In version v3, Fiber will inject `requestID` into the built-in `Context` of Go. - -```go -func handler(c fiber.Ctx) error { - id := requestid.FromContext(c.Context()) - log.Printf("Request ID: %s", id) - return c.SendString("Hello, World!") -} -``` - ## Config | Property | Type | Description | Default | diff --git a/middleware/requestid/requestid.go b/middleware/requestid/requestid.go index ef67e6f2..8e521dc6 100644 --- a/middleware/requestid/requestid.go +++ b/middleware/requestid/requestid.go @@ -1,10 +1,7 @@ package requestid import ( - "context" - "github.com/gofiber/fiber/v3" - "github.com/gofiber/fiber/v3/log" ) // The contextKey type is unexported to prevent collisions with context keys defined in @@ -39,10 +36,6 @@ func New(config ...Config) fiber.Handler { // Add the request ID to locals c.Locals(requestIDKey, rid) - // Add the request ID to UserContext - ctx := context.WithValue(c.Context(), requestIDKey, rid) - c.SetContext(ctx) - // Continue stack return c.Next() } @@ -50,21 +43,9 @@ func New(config ...Config) fiber.Handler { // FromContext returns the request ID from context. // If there is no request ID, an empty string is returned. -// Supported context types: -// - fiber.Ctx: Retrieves request ID from Locals -// - context.Context: Retrieves request ID from context values -func FromContext(c any) string { - switch ctx := c.(type) { - case fiber.Ctx: - if rid, ok := ctx.Locals(requestIDKey).(string); ok { - return rid - } - case context.Context: - if rid, ok := ctx.Value(requestIDKey).(string); ok { - return rid - } - default: - log.Errorf("Unsupported context type: %T. Expected fiber.Ctx or context.Context", c) +func FromContext(c fiber.Ctx) string { + if rid, ok := c.Locals(requestIDKey).(string); ok { + return rid } return "" } diff --git a/middleware/requestid/requestid_test.go b/middleware/requestid/requestid_test.go index ad36884a..c739407b 100644 --- a/middleware/requestid/requestid_test.go +++ b/middleware/requestid/requestid_test.go @@ -51,59 +51,26 @@ func Test_RequestID_Next(t *testing.T) { require.Equal(t, fiber.StatusNotFound, resp.StatusCode) } -// go test -run Test_RequestID_FromContext +// go test -run Test_RequestID_Locals func Test_RequestID_FromContext(t *testing.T) { t.Parallel() - reqID := "ThisIsARequestId" - type args struct { - inputFunc func(c fiber.Ctx) any - } - - tests := []struct { - args args - name string - }{ - { - name: "From fiber.Ctx", - args: args{ - inputFunc: func(c fiber.Ctx) any { - return c - }, - }, + app := fiber.New() + app.Use(New(Config{ + Generator: func() string { + return reqID }, - { - name: "From context.Context", - args: args{ - inputFunc: func(c fiber.Ctx) any { - return c.Context() - }, - }, - }, - } + })) - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - t.Parallel() + var ctxVal string - app := fiber.New() - app.Use(New(Config{ - Generator: func() string { - return reqID - }, - })) + app.Use(func(c fiber.Ctx) error { + ctxVal = FromContext(c) + return c.Next() + }) - var ctxVal string - - app.Use(func(c fiber.Ctx) error { - ctxVal = FromContext(tt.args.inputFunc(c)) - return c.Next() - }) - - _, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil)) - require.NoError(t, err) - require.Equal(t, reqID, ctxVal) - }) - } + _, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil)) + require.NoError(t, err) + require.Equal(t, reqID, ctxVal) }