Skip to content

Commit

Permalink
Add a few notes regarding nil contexts
Browse files Browse the repository at this point in the history
Signed-off-by: Cyril Cressent <cyril@sensu.io>
  • Loading branch information
ccressent committed Oct 16, 2020
1 parent 2ef7ead commit 06f655d
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
2 changes: 2 additions & 0 deletions api/core/v2/multitenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions backend/apid/graphql/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions backend/apid/graphql/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 06f655d

Please sign in to comment.