Skip to content

Commit b3e8486

Browse files
sixcolorsgaby
andauthored
Revert "🔥 feat: Add Context Support to RequestID Middleware" (#3365)
Revert "🔥 feat: Add Context Support to RequestID Middleware (#3200)" This reverts commit f725ded. Co-authored-by: Juan Calderon-Perez <[email protected]>
1 parent 417dffe commit b3e8486

File tree

3 files changed

+20
-82
lines changed

3 files changed

+20
-82
lines changed

docs/middleware/requestid.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,6 @@ func handler(c fiber.Ctx) error {
4949
}
5050
```
5151

52-
In version v3, Fiber will inject `requestID` into the built-in `Context` of Go.
53-
54-
```go
55-
func handler(c fiber.Ctx) error {
56-
id := requestid.FromContext(c.Context())
57-
log.Printf("Request ID: %s", id)
58-
return c.SendString("Hello, World!")
59-
}
60-
```
61-
6252
## Config
6353

6454
| Property | Type | Description | Default |

middleware/requestid/requestid.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package requestid
22

33
import (
4-
"context"
5-
64
"github.com/gofiber/fiber/v3"
7-
"github.com/gofiber/fiber/v3/log"
85
)
96

107
// The contextKey type is unexported to prevent collisions with context keys defined in
@@ -39,32 +36,16 @@ func New(config ...Config) fiber.Handler {
3936
// Add the request ID to locals
4037
c.Locals(requestIDKey, rid)
4138

42-
// Add the request ID to UserContext
43-
ctx := context.WithValue(c.Context(), requestIDKey, rid)
44-
c.SetContext(ctx)
45-
4639
// Continue stack
4740
return c.Next()
4841
}
4942
}
5043

5144
// FromContext returns the request ID from context.
5245
// If there is no request ID, an empty string is returned.
53-
// Supported context types:
54-
// - fiber.Ctx: Retrieves request ID from Locals
55-
// - context.Context: Retrieves request ID from context values
56-
func FromContext(c any) string {
57-
switch ctx := c.(type) {
58-
case fiber.Ctx:
59-
if rid, ok := ctx.Locals(requestIDKey).(string); ok {
60-
return rid
61-
}
62-
case context.Context:
63-
if rid, ok := ctx.Value(requestIDKey).(string); ok {
64-
return rid
65-
}
66-
default:
67-
log.Errorf("Unsupported context type: %T. Expected fiber.Ctx or context.Context", c)
46+
func FromContext(c fiber.Ctx) string {
47+
if rid, ok := c.Locals(requestIDKey).(string); ok {
48+
return rid
6849
}
6950
return ""
7051
}

middleware/requestid/requestid_test.go

Lines changed: 17 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -51,59 +51,26 @@ func Test_RequestID_Next(t *testing.T) {
5151
require.Equal(t, fiber.StatusNotFound, resp.StatusCode)
5252
}
5353

54-
// go test -run Test_RequestID_FromContext
54+
// go test -run Test_RequestID_Locals
5555
func Test_RequestID_FromContext(t *testing.T) {
5656
t.Parallel()
57-
5857
reqID := "ThisIsARequestId"
5958

60-
type args struct {
61-
inputFunc func(c fiber.Ctx) any
62-
}
63-
64-
tests := []struct {
65-
args args
66-
name string
67-
}{
68-
{
69-
name: "From fiber.Ctx",
70-
args: args{
71-
inputFunc: func(c fiber.Ctx) any {
72-
return c
73-
},
74-
},
75-
},
76-
{
77-
name: "From context.Context",
78-
args: args{
79-
inputFunc: func(c fiber.Ctx) any {
80-
return c.Context()
81-
},
82-
},
59+
app := fiber.New()
60+
app.Use(New(Config{
61+
Generator: func() string {
62+
return reqID
8363
},
84-
}
85-
86-
for _, tt := range tests {
87-
t.Run(tt.name, func(t *testing.T) {
88-
t.Parallel()
89-
90-
app := fiber.New()
91-
app.Use(New(Config{
92-
Generator: func() string {
93-
return reqID
94-
},
95-
}))
96-
97-
var ctxVal string
98-
99-
app.Use(func(c fiber.Ctx) error {
100-
ctxVal = FromContext(tt.args.inputFunc(c))
101-
return c.Next()
102-
})
103-
104-
_, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
105-
require.NoError(t, err)
106-
require.Equal(t, reqID, ctxVal)
107-
})
108-
}
64+
}))
65+
66+
var ctxVal string
67+
68+
app.Use(func(c fiber.Ctx) error {
69+
ctxVal = FromContext(c)
70+
return c.Next()
71+
})
72+
73+
_, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
74+
require.NoError(t, err)
75+
require.Equal(t, reqID, ctxVal)
10976
}

0 commit comments

Comments
 (0)