Skip to content

Commit aa04993

Browse files
aaguiarzrhamzeh
authored andcommitted
fix(client): fix passing model ID in BatchCheck
bringing the openfga/sdk-generator#555 from the generator
1 parent 71ddedc commit aa04993

4 files changed

Lines changed: 56 additions & 2 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,8 @@ Similar to [check](#check), but instead of checking a single user-object relatio
605605

606606
[API Documentation](https://openfga.dev/api/service#/Relationship%20Queries/BatchCheck)
607607

608+
> **Note**: The order of `batchCheck` results is not guaranteed to match the order of the checks provided. Use `correlationId` to pair responses with requests.
609+
608610
> Passing `ClientBatchCheckOptions` is optional. All fields of `ClientBatchCheckOptions` are optional.
609611
610612
```java

src/main/java/dev/openfga/sdk/api/OpenFgaApi.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,11 @@ private Map<Attribute, String> buildTelemetryAttributes(Map<String, Object> attr
11541154
if (body instanceof BatchCheckRequest) {
11551155
BatchCheckRequest batchCheckRequest = (BatchCheckRequest) body;
11561156

1157+
if (!isNullOrWhitespace(batchCheckRequest.getAuthorizationModelId())) {
1158+
telemetryAttributes.put(
1159+
Attributes.FGA_CLIENT_REQUEST_MODEL_ID, batchCheckRequest.getAuthorizationModelId());
1160+
}
1161+
11571162
if (batchCheckRequest.getChecks() != null) {
11581163
telemetryAttributes.put(
11591164
Attributes.FGA_CLIENT_REQUEST_BATCH_CHECK_SIZE,

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,8 +711,23 @@ public CompletableFuture<ClientBatchCheckResponse> batchCheck(
711711

712712
var override = new ConfigurationOverride().addHeaders(options);
713713

714-
Consumer<List<BatchCheckItem>> singleBatchCheckRequest = request -> call(() ->
715-
api.batchCheck(configuration.getStoreId(), new BatchCheckRequest().checks(request), override))
714+
Consumer<List<BatchCheckItem>> singleBatchCheckRequest = request -> call(() -> {
715+
BatchCheckRequest body = new BatchCheckRequest().checks(request);
716+
if (options.getConsistency() != null) {
717+
body.consistency(options.getConsistency());
718+
}
719+
720+
// Set authorizationModelId from options if available; otherwise, use the default from configuration
721+
String authorizationModelId = !isNullOrWhitespace(options.getAuthorizationModelId())
722+
? options.getAuthorizationModelId()
723+
: configuration.getAuthorizationModelId();
724+
725+
if (!isNullOrWhitespace(authorizationModelId)) {
726+
body.authorizationModelId(authorizationModelId);
727+
}
728+
729+
return api.batchCheck(configuration.getStoreId(), body, override);
730+
})
716731
.handleAsync((batchCheckResponseApiResponse, throwable) -> {
717732
Map<String, BatchCheckSingleResult> response =
718733
batchCheckResponseApiResponse.getData().getResult();

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2040,6 +2040,38 @@ public void shouldSplitBatchesSuccessfully(WireMockRuntimeInfo wireMockRuntimeIn
20402040
assertEquals("relation not found", response3.getError().getMessage());
20412041
}
20422042

2043+
@Test
2044+
public void batchCheck_withOptions() throws Exception {
2045+
// Given
2046+
String postUrl = String.format("https://api.fga.example/stores/%s/batch-check", DEFAULT_STORE_ID);
2047+
String expectedBody = String.format(
2048+
"{\"checks\":[{\"tuple_key\":{\"user\":\"%s\",\"relation\":\"%s\",\"object\":\"%s\"},\"contextual_tuples\":null,\"context\":null,\"correlation_id\":\"cor-1\"}],\"authorization_model_id\":\"%s\",\"consistency\":\"%s\"}",
2049+
DEFAULT_USER,
2050+
DEFAULT_RELATION,
2051+
DEFAULT_OBJECT,
2052+
DEFAULT_AUTH_MODEL_ID,
2053+
ConsistencyPreference.MINIMIZE_LATENCY);
2054+
mockHttpClient.onPost(postUrl).withBody(is(expectedBody)).doReturn(200, "{\"result\":{}}");
2055+
2056+
ClientBatchCheckItem item = new ClientBatchCheckItem()
2057+
.user(DEFAULT_USER)
2058+
.relation(DEFAULT_RELATION)
2059+
._object(DEFAULT_OBJECT)
2060+
.correlationId("cor-1");
2061+
ClientBatchCheckRequest request = new ClientBatchCheckRequest().checks(List.of(item));
2062+
ClientBatchCheckOptions options = new ClientBatchCheckOptions()
2063+
.authorizationModelId(DEFAULT_AUTH_MODEL_ID)
2064+
.consistency(ConsistencyPreference.MINIMIZE_LATENCY);
2065+
2066+
// When
2067+
ClientBatchCheckResponse response = fga.batchCheck(request, options).join();
2068+
2069+
// Then
2070+
mockHttpClient.verify().post(postUrl).withBody(is(expectedBody)).called(1);
2071+
assertNotNull(response);
2072+
assertTrue(response.getResult().isEmpty());
2073+
}
2074+
20432075
/**
20442076
* Expand all relationships in userset tree format, and following userset rewrite rules. Useful to reason
20452077
* about and debug a certain relationship.

0 commit comments

Comments
 (0)