Skip to content

Commit

Permalink
fix(java-sdk): fix transactions mode being reversed (openfga#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh authored Feb 29, 2024
2 parents f1199a4 + d62eec2 commit 161c953
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions config/clients/java/template/client-OpenFgaClient.java.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ public class OpenFgaClient {
return writeTransactions(storeId, request, options);
}

private CompletableFuture<ClientWriteResponse> writeNonTransaction(
private CompletableFuture<ClientWriteResponse> writeTransactions(
String storeId, ClientWriteRequest request, ClientWriteOptions options) {
WriteRequest body = new WriteRequest();
Expand All @@ -378,7 +378,7 @@ public class OpenFgaClient {
return call(() -> api.write(storeId, body, overrides)).thenApply(ClientWriteResponse::new);
}

private CompletableFuture<ClientWriteResponse> writeTransactions(
private CompletableFuture<ClientWriteResponse> writeNonTransaction(
String storeId, ClientWriteRequest request, ClientWriteOptions writeOptions) {
var options = writeOptions != null
Expand All @@ -400,10 +400,10 @@ public class OpenFgaClient {

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

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

for (int i = 1; i < transactions.size(); i++) {
final int index = i; // Must be final in this scope for closure.
Expand All @@ -412,7 +412,7 @@ public class OpenFgaClient {
// 1. The first exception thrown in a failed completion. Other thenCompose() will not be evaluated.
// 2. The final successful ClientWriteResponse.
futureResponse = futureResponse.thenCompose(
_response -> this.writeNonTransaction(storeId, transactions.get(index), options));
_response -> this.writeTransactions(storeId, transactions.get(index), options));
}

return futureResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ public class OpenFgaClientTest {
}

@Test
public void writeTest_transactions() throws Exception {
public void writeTest_nonTransaction() throws Exception {
// Given
String postPath = "https://localhost/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write";
String writeTupleBody = String.format(
Expand Down Expand Up @@ -1132,7 +1132,7 @@ public class OpenFgaClientTest {
.writes(List.of(writeTuple, writeTuple, writeTuple, writeTuple, writeTuple))
.deletes(List.of(tuple, tuple, tuple, tuple, tuple));
ClientWriteOptions options =
new ClientWriteOptions().disableTransactions(false).transactionChunkSize(2);
new ClientWriteOptions().disableTransactions(true).transactionChunkSize(2);
// When
var response = fga.write(request, options).get();
Expand Down Expand Up @@ -1170,7 +1170,7 @@ public class OpenFgaClientTest {
}

@Test
public void writeTest_transactionsWithFailure() throws Exception {
public void writeTest_nonTransactionsWithFailure() {
// Given
String postPath = "https://localhost/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write";
String firstUser = "user:first";
Expand Down Expand Up @@ -1200,7 +1200,7 @@ public class OpenFgaClientTest {
.condition(DEFAULT_CONDITION))
.collect(Collectors.toList()));
ClientWriteOptions options =
new ClientWriteOptions().disableTransactions(false).transactionChunkSize(1);
new ClientWriteOptions().disableTransactions(true).transactionChunkSize(1);
// When
var execException = assertThrows(
Expand Down Expand Up @@ -1233,7 +1233,7 @@ public class OpenFgaClientTest {
}

@Test
public void writeTest_nonTransaction() throws Exception {
public void writeTest_transaction() throws Exception {
// Given
String postPath = "https://localhost/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write";
String writeTupleBody = String.format(
Expand Down Expand Up @@ -1262,7 +1262,7 @@ public class OpenFgaClientTest {
// We expect transactionChunkSize will be ignored, and exactly one request will be sent.
ClientWriteOptions options =
new ClientWriteOptions().disableTransactions(true).transactionChunkSize(1);
new ClientWriteOptions().disableTransactions(false).transactionChunkSize(1);
// When
var response = fga.write(request, options).get();
Expand All @@ -1273,7 +1273,7 @@ public class OpenFgaClientTest {
}

@Test
public void writeTest_nonTransactionsWithFailure() throws Exception {
public void writeTest_transactionWithFailure() {
// Given
String postPath = "https://localhost/stores/01YCP46JKYM8FJCQ37NMBYHE5X/write";
String writeTupleBody = String.format(
Expand Down Expand Up @@ -1305,7 +1305,7 @@ public class OpenFgaClientTest {
// We expect transactionChunkSize will be ignored, and exactly one request will be sent.
ClientWriteOptions options =
new ClientWriteOptions().disableTransactions(true).transactionChunkSize(1);
new ClientWriteOptions().disableTransactions(false).transactionChunkSize(1);
// When
var execException = assertThrows(
Expand Down

0 comments on commit 161c953

Please sign in to comment.