Skip to content

Commit

Permalink
fix(java-sdk): don't send tuple key on empty read request (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhamzeh authored Oct 31, 2023
2 parents a386024 + 7a890dd commit ca2b7a3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
7 changes: 7 additions & 0 deletions config/clients/java/CHANGELOG.md.mustache
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## v0.2.2

### [0.2.2](https://{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/compare/v0.2.1...v0.2.2) (2023-10-31)

- fix(client): an empty read request will no longer send an empty tuple
- fix(client): an unused "user" field, and related methods, was removed from ClientExpandRequest

## v0.2.1

### [0.2.1](https://{{gitHost}}/{{gitUserId}}/{{gitRepoId}}/compare/v0.2.0...v0.2.1) (2023-10-13)
Expand Down
2 changes: 1 addition & 1 deletion config/clients/java/config.overrides.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"gitRepoId": "java-sdk",
"artifactId": "openfga-sdk",
"groupId": "dev.openfga",
"packageVersion": "0.2.1",
"packageVersion": "0.2.2",
"apiPackage": "dev.openfga.sdk.api",
"authPackage": "dev.openfga.sdk.api.auth",
"clientPackage": "dev.openfga.sdk.api.client",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
package {{invokerPackage}};

public class ClientExpandRequest {
private String user;
private String relation;
private String _object;
Expand Down Expand Up @@ -31,17 +30,4 @@ public class ClientExpandRequest {
public String getRelation() {
return relation;
}

public ClientExpandRequest user(String user) {
this.user = user;
return this;
}

/**
* Get user
* @return user
**/
public String getUser() {
return user;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,17 @@ public class OpenFgaClient {
String storeId = configuration.getStoreIdChecked();
ReadRequest body = new ReadRequest();
TupleKey tupleKey = new TupleKey();
if (request != null) {
if (request != null && (request.getUser() != null || request.getRelation() != null || request.getObject() != null)) {
TupleKey tupleKey = new TupleKey();
tupleKey.user(request.getUser()).relation(request.getRelation())._object(request.getObject());
body.tupleKey(tupleKey);
}

if (options != null) {
body.pageSize(options.getPageSize()).continuationToken(options.getContinuationToken());
}

body.tupleKey(tupleKey);

return call(() -> api.read(storeId, body)).thenApply(ClientReadResponse::new);
}

Expand Down Expand Up @@ -398,7 +397,6 @@ public class OpenFgaClient {
if (request != null) {
body.tupleKey(new TupleKey()
.user(request.getUser())
.relation(request.getRelation())
._object(request.getObject()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,21 @@ public class OpenFgaClientTest {
assertEquals(DEFAULT_OBJECT, key.getObject());
}

@Test
public void read_emptyRequestSendsNoTupleKey() throws Exception {
// Given
String postUrl = String.format("https://localhost/stores/%s/read", DEFAULT_STORE_ID);
String expectedBody = "{\"tuple_key\":null,\"page_size\":null,\"continuation_token\":null}";
mockHttpClient.onPost(postUrl).withBody(is(expectedBody)).doReturn(200, EMPTY_RESPONSE_BODY);
ClientReadRequest request = new ClientReadRequest();
// When
ClientReadResponse response = fga.read(request).get();
// Then
mockHttpClient.verify().post(postUrl).withBody(is(expectedBody)).called(1);
}

@Test
public void read_storeIdRequired() {
// Given
Expand Down Expand Up @@ -1294,14 +1309,13 @@ public class OpenFgaClientTest {
// Given
String postPath = "https://localhost/stores/01YCP46JKYM8FJCQ37NMBYHE5X/expand";
String expectedBody = String.format(
"{\"tuple_key\":{\"object\":\"%s\",\"relation\":\"%s\",\"user\":\"%s\"},\"authorization_model_id\":\"%s\"}",
DEFAULT_OBJECT, DEFAULT_RELATION, DEFAULT_USER, DEFAULT_AUTH_MODEL_ID);
"{\"tuple_key\":{\"object\":\"%s\",\"relation\":\"%s\",\"user\":null},\"authorization_model_id\":\"%s\"}",
DEFAULT_OBJECT, DEFAULT_RELATION, DEFAULT_AUTH_MODEL_ID);
String responseBody = String.format(
"{\"tree\":{\"root\":{\"union\":{\"nodes\":[{\"leaf\":{\"users\":{\"users\":[\"%s\"]}}}]}}}}",
DEFAULT_USER);
mockHttpClient.onPost(postPath).withBody(is(expectedBody)).doReturn(200, responseBody);
ClientExpandRequest request = new ClientExpandRequest()
.user(DEFAULT_USER)
.relation(DEFAULT_RELATION)
._object(DEFAULT_OBJECT);
ClientExpandOptions options = new ClientExpandOptions().authorizationModelId(DEFAULT_AUTH_MODEL_ID);
Expand Down

0 comments on commit ca2b7a3

Please sign in to comment.