Skip to content

Commit

Permalink
fix: make write with no writes or deletes a no-op
Browse files Browse the repository at this point in the history
  • Loading branch information
ewanharris committed Feb 8, 2024
1 parent 42b7291 commit d143227
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1318,6 +1318,10 @@ func (client *OpenFgaClient) WriteExecute(request SdkClientWriteRequestInterface
return nil, err
}

if len(request.GetBody().Deletes) == 0 && len(request.GetBody().Writes) == 0 {
return &response, nil
}

// Unless explicitly disabled, transaction mode is enabled
// In transaction mode, the client will send the request to the server as is
if request.GetOptions() == nil || request.GetOptions().Transaction == nil || !request.GetOptions().Transaction.Disable {
Expand Down
22 changes: 22 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,28 @@ func TestOpenFgaClient(t *testing.T) {
}
})

t.Run("Write with no deletes or writes should not make a request", func(t *testing.T) {
requestBody := ClientWriteRequest{
Writes: []ClientTupleKey{},
}
options := ClientWriteOptions{
AuthorizationModelId: openfga.PtrString("01GAHCE4YVKPQEKZQHT2R89MQV"),
}

data, err := fgaClient.Write(context.Background()).Body(requestBody).Options(options).Execute()
if err != nil {
t.Fatalf("%v", err)
}

if len(data.Writes) != 0 {
t.Fatalf("OpenFgaClient.Write() - expected %v Writes, got %v", 0, len(data.Writes))
}

if len(data.Deletes) != 0 {
t.Fatalf("OpenFgaClient.Write() - expected %v Deletes, got %v", 0, len(data.Deletes))
}
})

t.Run("WriteTuples", func(t *testing.T) {
test := TestDefinition{
Name: "Write",
Expand Down

0 comments on commit d143227

Please sign in to comment.