diff --git a/config/clients/java/template/client-OpenFgaClient.java.mustache b/config/clients/java/template/client-OpenFgaClient.java.mustache index 928f7606..aa86a494 100644 --- a/config/clients/java/template/client-OpenFgaClient.java.mustache +++ b/config/clients/java/template/client-OpenFgaClient.java.mustache @@ -397,6 +397,12 @@ public class OpenFgaClient { 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++) { diff --git a/config/clients/java/template/client-OpenFgaClientTest.java.mustache b/config/clients/java/template/client-OpenFgaClientTest.java.mustache index 71567a7a..2f95beb5 100644 --- a/config/clients/java/template/client-OpenFgaClientTest.java.mustache +++ b/config/clients/java/template/client-OpenFgaClientTest.java.mustache @@ -6,7 +6,6 @@ import static org.hamcrest.Matchers.*; import static org.hamcrest.core.StringContains.containsString; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; - import com.fasterxml.jackson.databind.ObjectMapper; import com.pgssoft.httpclient.HttpClientMock; import {{clientPackage}}.model.*; @@ -16,6 +15,7 @@ import {{invokerPackage}}.*; import {{modelPackage}}.*; 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; @@ -1361,6 +1361,23 @@ public class OpenFgaClientTest { 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