Skip to content

Commit

Permalink
fix: update should allow setting null
Browse files Browse the repository at this point in the history
  • Loading branch information
himank committed Aug 11, 2022
1 parent e782bb6 commit 685b82c
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
9 changes: 9 additions & 0 deletions server/services/v1/query_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,15 @@ func (runner *UpdateQueryRunner) Run(ctx context.Context, tx transaction.Tx, ten
return nil, ctx, err
}

if vMap, ok := v.(map[string]interface{}); ok {
for k, v := range vMap {
// remove fields that are set as null as we don't need them for the validation
if v == nil {
delete(vMap, k)
}
}
}

if err = collection.Validate(v); err != nil {
// schema validation failed
return nil, ctx, err
Expand Down
72 changes: 72 additions & 0 deletions test/v1/server/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,78 @@ func TestUpdate_SingleRow(t *testing.T) {
})
}

func TestUpdate_NullField(t *testing.T) {
db, coll := setupTests(t)
defer cleanupTests(t, db)

inputDocument := []Doc{
{
"pkey_int": 100,
"int_value": 100,
"string_value": "simple_insert1_update",
"bool_value": true,
"double_value": 100.00001,
"bytes_value": []byte(`"simple_insert1_update"`),
},
}

insertDocuments(t, db, coll, inputDocument, false).
Status(http.StatusOK)

readAndValidate(t,
db,
coll,
Map{
"pkey_int": 100,
},
nil,
inputDocument)

tstart := time.Now().UTC()
updateByFilter(t,
db,
coll,
Map{
"filter": Map{
"pkey_int": 100,
},
},
Map{
"fields": Map{
"$set": Map{
"int_value": nil,
"string_value": "simple_insert1_update_modified",
"bool_value": false,
"double_value": 200.00001,
"bytes_value": []byte(`"simple_insert1_update_modified"`),
},
},
}).Status(http.StatusOK).
JSON().
Object().
ValueEqual("modified_count", 1).
Path("$.metadata").Object().
Value("updated_at").String().DateTime(time.RFC3339Nano).InRange(tstart, time.Now().UTC().Add(1*time.Second))

readAndValidate(t,
db,
coll,
Map{
"pkey_int": 100,
},
nil,
[]Doc{
{
"pkey_int": 100,
"int_value": nil,
"string_value": "simple_insert1_update_modified",
"bool_value": false,
"double_value": 200.00001,
"bytes_value": []byte(`"simple_insert1_update_modified"`),
},
})
}

func TestUpdate_SchemaValidationError(t *testing.T) {
db, coll := setupTests(t)
defer cleanupTests(t, db)
Expand Down

0 comments on commit 685b82c

Please sign in to comment.