fix(context): add check to avoid nil pointe

This commit is contained in:
Jean-Baptiste Le Duigou 2022-06-08 14:24:23 +02:00 committed by jbleduigou
parent f2182de38c
commit 7cdb8065f4
2 changed files with 7 additions and 1 deletions

View File

@ -1195,7 +1195,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

@ -2294,3 +2294,9 @@ 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 TestRetrieveValueNoEngineInContext(t *testing.T) {
c := &Context{}
v := c.Value("foo")
assert.Nil(t, v)
}