fix(context): update context.Value method to properly support any key type

This commit is contained in:
Kasidej 2026-01-06 17:42:45 +00:00
parent 9914178584
commit b9d6964b7e
2 changed files with 6 additions and 4 deletions

View File

@ -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

View File

@ -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) {