mirror of https://github.com/gin-gonic/gin.git
fix(context): add check to avoid nil pointe
This commit is contained in:
parent
f2182de38c
commit
7cdb8065f4
|
@ -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)
|
||||||
|
|
|
@ -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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue