From b9d6964b7e2bc3e12e1d06443073d7a114073b3e Mon Sep 17 00:00:00 2001 From: Kasidej Date: Tue, 6 Jan 2026 17:42:45 +0000 Subject: [PATCH] fix(context): update context.Value method to properly support any key type --- context.go | 6 ++---- context_test.go | 4 ++++ 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/context.go b/context.go index c42459ff..60b4712c 100644 --- a/context.go +++ b/context.go @@ -1436,10 +1436,8 @@ func (c *Context) Value(key any) any { if key == ContextKey { return c } - if keyAsString, ok := key.(string); ok { - if val, exists := c.Get(keyAsString); exists { - return val - } + if val, exists := c.Get(key); exists { + return val } if !c.hasRequestContext() { return nil diff --git a/context_test.go b/context_test.go index 3080015c..bbfb10f2 100644 --- a/context_test.go +++ b/context_test.go @@ -2859,6 +2859,10 @@ func TestContextGolangContext(t *testing.T) { c.Set("foo", "bar") assert.Equal(t, "bar", c.Value("foo")) assert.Nil(t, c.Value(1)) + + type contextKey struct{} + c.Set(contextKey{}, "value") + assert.Equal(t, "value", c.Value(contextKey{})) } func TestWebsocketsRequired(t *testing.T) {