mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 13:22:09 +08:00
fix context.Value-engine is nil panic
This commit is contained in:
parent
b04917c53e
commit
e25867d2b4
@ -1205,7 +1205,7 @@ func (c *Context) Value(key any) any {
|
||||
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 c.Request.Context().Value(key)
|
||||
|
@ -2354,3 +2354,11 @@ func TestContextAddParam(t *testing.T) {
|
||||
assert.Equal(t, ok, true)
|
||||
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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user