From 06f655da86d96ff3cc3bd0f6ba1a5c99f110761f Mon Sep 17 00:00:00 2001 From: Cyril Cressent Date: Fri, 16 Oct 2020 10:08:50 -0700 Subject: [PATCH] Add a few notes regarding nil contexts Signed-off-by: Cyril Cressent --- api/core/v2/multitenant.go | 2 ++ backend/apid/graphql/handler.go | 1 + backend/apid/graphql/handler_test.go | 3 +++ 3 files changed, 6 insertions(+) diff --git a/api/core/v2/multitenant.go b/api/core/v2/multitenant.go index 3a03cc008a..504b41db09 100644 --- a/api/core/v2/multitenant.go +++ b/api/core/v2/multitenant.go @@ -10,6 +10,8 @@ type MultitenantResource interface { // SetContextFromResource takes a context and a multi-tenant resource, adds the // namespace to the context, and returns the udpated context func SetContextFromResource(ctx context.Context, r MultitenantResource) context.Context { + // Here ctx can be nil, triggering the panic in Go 1.15. Maybe we can check + // if ctx is nil, and if so replace it with context.Background() ctx = context.WithValue(ctx, NamespaceKey, r.GetNamespace()) return ctx } diff --git a/backend/apid/graphql/handler.go b/backend/apid/graphql/handler.go index d44c1dc81f..edb0c9d0a4 100644 --- a/backend/apid/graphql/handler.go +++ b/backend/apid/graphql/handler.go @@ -33,6 +33,7 @@ func (r *handlerImpl) Mutator(p graphql.ResolveParams) (interface{}, error) { return nil, nil } + // p.Context is nil here ctx := corev2.SetContextFromResource(p.Context, src) res, err := r.client.FetchMutator(ctx, src.Mutator) diff --git a/backend/apid/graphql/handler_test.go b/backend/apid/graphql/handler_test.go index 930181c179..7b88203562 100644 --- a/backend/apid/graphql/handler_test.go +++ b/backend/apid/graphql/handler_test.go @@ -45,6 +45,9 @@ func TestHandlerTypeMutatorField(t *testing.T) { // Success client.On("FetchMutator", mock.Anything, mutator.Name).Return(mutator, nil).Once() + // We could add a Context: context.Background() to ResolveParams, but there + // might be other places in the code that instantiate a + // graphql.ResolveParams{} without a Context field? res, err := impl.Mutator(graphql.ResolveParams{Source: handler}) require.NoError(t, err) assert.NotEmpty(t, res)