From 9ea73d60b203142f26c853c44dfe52903507f845 Mon Sep 17 00:00:00 2001 From: "Justin \"J.R.\" Hill" Date: Tue, 31 Oct 2023 09:57:03 -0700 Subject: [PATCH] fix(java-sdk): don't send tuple key on empty read request closes #30 --- .../template/client-OpenFgaClient.java.mustache | 7 +++---- .../client-OpenFgaClientTest.java.mustache | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/config/clients/java/template/client-OpenFgaClient.java.mustache b/config/clients/java/template/client-OpenFgaClient.java.mustache index a375753f..048bf0b6 100644 --- a/config/clients/java/template/client-OpenFgaClient.java.mustache +++ b/config/clients/java/template/client-OpenFgaClient.java.mustache @@ -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); } diff --git a/config/clients/java/template/client-OpenFgaClientTest.java.mustache b/config/clients/java/template/client-OpenFgaClientTest.java.mustache index 6546e0ab..1554c433 100644 --- a/config/clients/java/template/client-OpenFgaClientTest.java.mustache +++ b/config/clients/java/template/client-OpenFgaClientTest.java.mustache @@ -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