From e25867d2b4e5be7283a7cb2c0ef8418c621a5b0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=97=AB=E6=A2=A6=E9=A3=9E?= Date: Tue, 16 Aug 2022 09:50:36 +0800 Subject: [PATCH] fix context.Value-engine is nil panic --- context.go | 2 +- context_test.go | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/context.go b/context.go index f9489a77..8f4ba0b8 100644 --- a/context.go +++ b/context.go @@ -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) diff --git a/context_test.go b/context_test.go index b3e81c14..a0caaa6d 100644 --- a/context_test.go +++ b/context_test.go @@ -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) +}