Skip to content

Commit

Permalink
fix(client): don't throw IndexOutOfBoundsException when null writes a…
Browse files Browse the repository at this point in the history
…nd reads are sent

from openfga/sdk-generator#307
  • Loading branch information
jimmyjames committed Feb 27, 2024
1 parent 1d0ce7b commit ab7f37b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,12 @@ private CompletableFuture<ClientWriteResponse> writeTransactions(
var deleteTransactions = chunksOf(chunkSize, request.getDeletes()).map(ClientWriteRequest::ofDeletes);

var transactions = Stream.concat(writeTransactions, deleteTransactions).collect(Collectors.toList());

if (transactions.isEmpty()) {
var emptyTransaction = new ClientWriteRequest().writes(null).deletes(null);
return this.writeNonTransaction(storeId, emptyTransaction, writeOptions);
}

var futureResponse = this.writeNonTransaction(storeId, transactions.get(0), options);

for (int i = 1; i < transactions.size(); i++) {
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/dev/openfga/sdk/api/client/OpenFgaClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import dev.openfga.sdk.errors.*;
import java.net.http.HttpClient;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -1370,6 +1371,25 @@ public void deleteTuplesTest() throws Exception {
assertEquals(200, response.getStatusCode());
}

@Test
public void write_nothingSentWhenWritesAndDeletesAreEmpty()
throws FgaInvalidParameterException, ExecutionException, InterruptedException {
// Given
String postPath = String.format("https://localhost/stores/%s/write", DEFAULT_STORE_ID);
String expectedBody = String.format(
"{\"writes\":null,\"deletes\":null,\"authorization_model_id\":\"%s\"}", DEFAULT_AUTH_MODEL_ID);
mockHttpClient.onPost(postPath).withBody(is(expectedBody)).doReturn(200, EMPTY_RESPONSE_BODY);

// When
var clientWriteRequest =
new ClientWriteRequest().writes(Collections.emptyList()).deletes(Collections.emptyList());
var response = fga.write(clientWriteRequest).get();

// Then
mockHttpClient.verify().post(postPath).called(1);
assertEquals(200, response.getStatusCode());
}

@Test
public void write_storeIdRequired() {
// Given
Expand Down

0 comments on commit ab7f37b

Please sign in to comment.