From e209fa6c07d2a4f5a8296d042ed46c8dee5b30a3 Mon Sep 17 00:00:00 2001 From: Andrew Sisley Date: Fri, 4 Oct 2024 10:34:49 -0400 Subject: [PATCH] Handle explicit nil on secondary side relations --- internal/db/collection.go | 5 +++ tests/integration/events.go | 6 ++++ .../one_to_one/with_null_value_test.go | 31 ++++++++++++++++--- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/internal/db/collection.go b/internal/db/collection.go index dd1a413946..785c96641e 100644 --- a/internal/db/collection.go +++ b/internal/db/collection.go @@ -657,6 +657,11 @@ func (c *collection) save( relationFieldDescription, isSecondaryRelationID := fieldDescription.GetSecondaryRelationField(c.Definition()) if isSecondaryRelationID { + if val.Value() == nil { + // If the value (relation) is nil, we don't need to check for any documents already linked to it + continue + } + primaryId := val.Value().(string) err = c.patchPrimaryDoc( diff --git a/tests/integration/events.go b/tests/integration/events.go index bf004b99aa..5e57a97294 100644 --- a/tests/integration/events.go +++ b/tests/integration/events.go @@ -335,6 +335,12 @@ func getEventsForCreateDoc(s *state, action CreateDoc) map[string]struct{} { // check for any secondary relation fields that could publish an event for f, v := range doc.Values() { + if v.Value() == nil { + // If the new relation value is nil there will be no related document + // to get an event for + continue + } + field, ok := def.GetFieldByName(f.Name()) if !ok { continue // ignore unknown field diff --git a/tests/integration/mutation/create/field_kinds/one_to_one/with_null_value_test.go b/tests/integration/mutation/create/field_kinds/one_to_one/with_null_value_test.go index a428f76681..a6421aec5c 100644 --- a/tests/integration/mutation/create/field_kinds/one_to_one/with_null_value_test.go +++ b/tests/integration/mutation/create/field_kinds/one_to_one/with_null_value_test.go @@ -13,8 +13,6 @@ package one_to_one import ( "testing" - "github.com/stretchr/testify/require" - testUtils "github.com/sourcenetwork/defradb/tests/integration" ) @@ -119,10 +117,33 @@ func TestMutationCreateOneToOne_WithExplicitNullOnSecondarySide(t *testing.T) { "published": testUtils.NewDocIndex(0, 0), }, }, + testUtils.Request{ + Request: ` + query { + Book { + name + author { + name + } + } + }`, + Results: map[string]any{ + "Book": []map[string]any{ + { + "name": "Secrets at Maple Syrup Farm", + "author": nil, + }, + { + "name": "How to Be a Canadian", + "author": map[string]any{ + "name": "Will Ferguson", + }, + }, + }, + }, + }, }, } - require.Panics(t, func() { - testUtils.ExecuteTestCase(t, test) - }) + testUtils.ExecuteTestCase(t, test) }