Skip to content

Commit 33fa182

Browse files
committed
refactor: update writeNonTransaction and batchCheck to handle immutable map for headers
1 parent d478606 commit 33fa182

File tree

2 files changed

+52
-18
lines changed

2 files changed

+52
-18
lines changed

src/main/java/dev/openfga/sdk/api/client/OpenFgaClient.java

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -558,12 +558,12 @@ private CompletableFuture<ClientWriteResponse> writeNonTransaction(
558558
? writeOptions
559559
: new ClientWriteOptions().transactionChunkSize(DEFAULT_MAX_METHOD_PARALLEL_REQS);
560560

561-
if (options.getAdditionalHeaders() == null) {
562-
options.additionalHeaders(new HashMap<>());
563-
}
564-
options.getAdditionalHeaders().putIfAbsent(CLIENT_METHOD_HEADER, "Write");
565-
options.getAdditionalHeaders()
566-
.putIfAbsent(CLIENT_BULK_REQUEST_ID_HEADER, randomUUID().toString());
561+
HashMap<String, String> headers = options.getAdditionalHeaders() != null
562+
? new HashMap<>(options.getAdditionalHeaders())
563+
: new HashMap<>();
564+
headers.putIfAbsent(CLIENT_METHOD_HEADER, "Write");
565+
headers.putIfAbsent(CLIENT_BULK_REQUEST_ID_HEADER, randomUUID().toString());
566+
options.additionalHeaders(headers);
567567

568568
int chunkSize = options.getTransactionChunkSize();
569569

@@ -899,12 +899,13 @@ public CompletableFuture<ClientBatchCheckResponse> batchCheck(
899899
: new ClientBatchCheckOptions()
900900
.maxParallelRequests(DEFAULT_MAX_METHOD_PARALLEL_REQS)
901901
.maxBatchSize(DEFAULT_MAX_BATCH_SIZE);
902-
if (options.getAdditionalHeaders() == null) {
903-
options.additionalHeaders(new HashMap<>());
904-
}
905-
options.getAdditionalHeaders().putIfAbsent(CLIENT_METHOD_HEADER, "BatchCheck");
906-
options.getAdditionalHeaders()
907-
.putIfAbsent(CLIENT_BULK_REQUEST_ID_HEADER, randomUUID().toString());
902+
903+
HashMap<String, String> headers = options.getAdditionalHeaders() != null
904+
? new HashMap<>(options.getAdditionalHeaders())
905+
: new HashMap<>();
906+
headers.putIfAbsent(CLIENT_METHOD_HEADER, "BatchCheck");
907+
headers.putIfAbsent(CLIENT_BULK_REQUEST_ID_HEADER, randomUUID().toString());
908+
options.additionalHeaders(headers);
908909

909910
Map<String, ClientBatchCheckItem> correlationIdToCheck = new HashMap<>();
910911

@@ -1135,8 +1136,8 @@ public CompletableFuture<ClientListRelationsResponse> listRelations(
11351136
HashMap<String, String> headers = options.getAdditionalHeaders() != null
11361137
? new HashMap<>(options.getAdditionalHeaders())
11371138
: new HashMap<>();
1138-
headers.put(CLIENT_METHOD_HEADER, "ListRelations");
1139-
headers.put(CLIENT_BULK_REQUEST_ID_HEADER, randomUUID().toString());
1139+
headers.putIfAbsent(CLIENT_METHOD_HEADER, "ListRelations");
1140+
headers.putIfAbsent(CLIENT_BULK_REQUEST_ID_HEADER, randomUUID().toString());
11401141
options.additionalHeaders(headers);
11411142

11421143
var batchCheckRequests = request.getRelations().stream()

src/test/java/dev/openfga/sdk/api/client/OpenFgaClientHeadersTest.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,41 @@ public void write_withHeaders() throws Exception {
428428
assertEquals(200, response.getStatusCode());
429429
}
430430

431+
@Test
432+
public void writeNonTransaction_withHeaders() throws Exception {
433+
// Given
434+
String postPath = String.format("https://api.fga.example/stores/%s/write", DEFAULT_STORE_ID);
435+
String expectedBody = String.format(
436+
"{\"writes\":{\"tuple_keys\":[{\"user\":\"%s\",\"relation\":\"%s\",\"object\":\"%s\",\"condition\":null}]},\"deletes\":null,\"authorization_model_id\":\"%s\"}",
437+
DEFAULT_USER, DEFAULT_RELATION, DEFAULT_OBJECT, DEFAULT_AUTH_MODEL_ID);
438+
mockHttpClient
439+
.onPost(postPath)
440+
.withBody(is(expectedBody))
441+
.withHeader("another-header", "another-value")
442+
.withHeader("test-header", "test-value-per-call")
443+
.doReturn(200, EMPTY_RESPONSE_BODY);
444+
ClientWriteRequest request = new ClientWriteRequest()
445+
.writes(List.of(new ClientTupleKey()
446+
._object(DEFAULT_OBJECT)
447+
.relation(DEFAULT_RELATION)
448+
.user(DEFAULT_USER)));
449+
ClientWriteOptions options = new ClientWriteOptions()
450+
.additionalHeaders(Map.of("test-header", "test-value-per-call"))
451+
.disableTransactions(true);
452+
453+
// When
454+
ClientWriteResponse response = fga.write(request, options).get();
455+
456+
// Then
457+
mockHttpClient
458+
.verify()
459+
.post(postPath)
460+
.withHeader("another-header", "another-value")
461+
.withHeader("test-header", "test-value-per-call")
462+
.called(1);
463+
assertEquals(200, response.getStatusCode());
464+
}
465+
431466
@Test
432467
public void check_withHeaders() throws Exception {
433468
// Given
@@ -480,10 +515,8 @@ public void batchCheck_withHeaders() throws Exception {
480515
._object(DEFAULT_OBJECT)
481516
.correlationId("cor-1");
482517
ClientBatchCheckRequest request = new ClientBatchCheckRequest().checks(List.of(item));
483-
// Use HashMap instead of Map.of() to create a mutable map
484-
Map<String, String> headers = new java.util.HashMap<>();
485-
headers.put("test-header", "test-value-per-call");
486-
ClientBatchCheckOptions options = new ClientBatchCheckOptions().additionalHeaders(headers);
518+
ClientBatchCheckOptions options =
519+
new ClientBatchCheckOptions().additionalHeaders(Map.of("test-header", "test-value-per-call"));
487520

488521
// When
489522
ClientBatchCheckResponse response = fga.batchCheck(request, options).join();

0 commit comments

Comments
 (0)