Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert revert requestid contextkey #2750

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/api/middleware/requestid.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ app.Use(requestid.New(requestid.Config{
| Next | `func(*fiber.Ctx) bool` | Next defines a function to skip this middleware when returned true. | `nil` |
| Header | `string` | Header is the header key where to get/set the unique request ID. | "X-Request-ID" |
| Generator | `func() string` | Generator defines a function to generate the unique identifier. | utils.UUID |
| ContextKey | `string` | ContextKey defines the key used when storing the request ID in the locals for a specific request. | "requestid" |
| ContextKey | `interface{}` | ContextKey defines the key used when storing the request ID in the locals for a specific request. | "requestid" |

## Default Config
The default config uses a fast UUID generator which will expose the number of
Expand Down
8 changes: 5 additions & 3 deletions middleware/requestid/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ type Config struct {

// ContextKey defines the key used when storing the request ID in
// the locals for a specific request.
// Should be a private type instead of string, but too many apps probably
// rely on this exact value.
//
// Optional. Default: requestid
ContextKey string
// Optional. Default: "requestid"
ContextKey interface{}
}

// ConfigDefault is the default config
Expand Down Expand Up @@ -57,7 +59,7 @@ func configDefault(config ...Config) Config {
if cfg.Generator == nil {
cfg.Generator = ConfigDefault.Generator
}
if cfg.ContextKey == "" {
if cfg.ContextKey == nil {
cfg.ContextKey = ConfigDefault.ContextKey
}
return cfg
Expand Down
31 changes: 28 additions & 3 deletions middleware/requestid/requestid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,45 @@ func Test_RequestID_Next(t *testing.T) {
func Test_RequestID_Locals(t *testing.T) {
t.Parallel()
reqID := "ThisIsARequestId"
ctxKey := "ThisIsAContextKey"
type ContextKey int
const requestContextKey ContextKey = iota

app := fiber.New()
app.Use(New(Config{
Generator: func() string {
return reqID
},
ContextKey: ctxKey,
ContextKey: requestContextKey,
}))

var ctxVal string

app.Use(func(c *fiber.Ctx) error {
ctxVal = c.Locals(ctxKey).(string) //nolint:forcetypeassert,errcheck // We always store a string in here
ctxVal = c.Locals(requestContextKey).(string) //nolint:forcetypeassert,errcheck // We always store a string in here
return c.Next()
})

_, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, reqID, ctxVal)
}

// go test -run Test_RequestID_DefaultKey
func Test_RequestID_DefaultKey(t *testing.T) {
t.Parallel()
reqID := "ThisIsARequestId"

app := fiber.New()
app.Use(New(Config{
Generator: func() string {
return reqID
},
}))

var ctxVal string

app.Use(func(c *fiber.Ctx) error {
ctxVal = c.Locals("requestid").(string) //nolint:forcetypeassert,errcheck // We always store a string in here
return c.Next()
})

Expand Down