From 9605826e1b332111c5e93553584a8157ed47c9c5 Mon Sep 17 00:00:00 2001 From: Lucian Jones Date: Fri, 17 Jun 2022 09:21:21 +1200 Subject: [PATCH] Ensure the destination map for merging is never nil --- execution.go | 9 +++++++++ execution_test.go | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/execution.go b/execution.go index b2923623..40591606 100644 --- a/execution.go +++ b/execution.go @@ -490,6 +490,15 @@ func mergeExecutionResults(results []executionResult) (map[string]interface{}, e } data := results[0].Data + switch ptr := data.(type) { + case nil: + data = make(map[string]interface{}) + case map[string]interface{}: + if ptr == nil { + data = make(map[string]interface{}) + } + } + for _, result := range results[1:] { if err := mergeExecutionResultsRec(result.Data, data, result.InsertionPoint); err != nil { return nil, err diff --git a/execution_test.go b/execution_test.go index 71575997..3d64706a 100644 --- a/execution_test.go +++ b/execution_test.go @@ -5794,6 +5794,27 @@ func TestQueryWithArrayBoundaryFields(t *testing.T) { f.checkSuccess(t) } +func TestMergeWithNull(t *testing.T) { + nullMap := make(map[string]interface{}) + dataMap := map[string]interface{}{ + "data": "foo", + } + + require.NoError(t, json.Unmarshal([]byte(`null`), &nullMap)) + + merged, err := mergeExecutionResults([]executionResult{ + { + Data: nullMap, + }, + { + Data: dataMap, + }, + }) + + require.NoError(t, err) + require.Equal(t, dataMap, merged) +} + func TestSchemaUpdate_serviceError(t *testing.T) { schemaA := `directive @boundary on OBJECT type Service {