From 7cdb8065f4809bab29a3a6d55bbd98058671f1d0 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Le Duigou Date: Wed, 8 Jun 2022 14:24:23 +0200 Subject: [PATCH] fix(context): add check to avoid nil pointe --- context.go | 2 +- context_test.go | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/context.go b/context.go index b1ad95e6..e358badc 100644 --- a/context.go +++ b/context.go @@ -1195,7 +1195,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 6c0be544..7aeac45a 100644 --- a/context_test.go +++ b/context_test.go @@ -2294,3 +2294,9 @@ func TestContextAddParam(t *testing.T) { assert.Equal(t, ok, true) assert.Equal(t, value, v) } + +func TestRetrieveValueNoEngineInContext(t *testing.T) { + c := &Context{} + v := c.Value("foo") + assert.Nil(t, v) +}