fix context.Value-engine is nil panic

This commit is contained in:
闫梦飞 2022-08-16 09:50:36 +08:00
parent b04917c53e
commit e25867d2b4
2 changed files with 9 additions and 1 deletions

View File

@ -1205,7 +1205,7 @@ func (c *Context) Value(key any) any {
return val return val
} }
} }
if !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil { if c.engine == nil || !c.engine.ContextWithFallback || c.Request == nil || c.Request.Context() == nil {
return nil return nil
} }
return c.Request.Context().Value(key) return c.Request.Context().Value(key)

View File

@ -2354,3 +2354,11 @@ func TestContextAddParam(t *testing.T) {
assert.Equal(t, ok, true) assert.Equal(t, ok, true)
assert.Equal(t, value, v) assert.Equal(t, value, v)
} }
func TestContextValue(t *testing.T) {
c := &Context{}
id := "id"
// gin@v1.8.1 will panic
value := c.Value(id)
assert.Nil(t, value)
}