Skip to content

Commit 806fc05

Browse files
authored
fix: handle empty auth models list in readlatest auth model (#265)
1 parent fa6845c commit 806fc05

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/main/java/dev/openfga/sdk/api/client/model/ClientReadAuthorizationModelResponse.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ public static ClientReadAuthorizationModelResponse latestOf(
3434
ClientReadAuthorizationModelResponse clientResponse = new ClientReadAuthorizationModelResponse(
3535
apiResponse.getStatusCode(), apiResponse.getHeaders(), apiResponse.getRawResponse());
3636
ReadAuthorizationModelsResponse response = apiResponse.getData();
37-
clientResponse.setAuthorizationModel(response.getAuthorizationModels().get(0));
37+
List<dev.openfga.sdk.api.model.AuthorizationModel> models = response.getAuthorizationModels();
38+
if (!models.isEmpty()) {
39+
clientResponse.setAuthorizationModel(models.get(0));
40+
}
3841
return clientResponse;
3942
}
4043

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,23 @@ public void readLatestAuthorizationModelTest() throws Exception {
746746
assertEquals(DEFAULT_SCHEMA_VERSION, authModel.getSchemaVersion());
747747
}
748748

749+
@Test
750+
public void readLatestAuthorizationModelTest_emptyList() throws Exception {
751+
// Given
752+
String getUrl = String.format(
753+
"%s/stores/%s/authorization-models?page_size=1", FgaConstants.TEST_API_URL, DEFAULT_STORE_ID);
754+
String responseBody = "{\"authorization_models\":[]}";
755+
mockHttpClient.onGet(getUrl).doReturn(200, responseBody);
756+
757+
// When
758+
ClientReadAuthorizationModelResponse response =
759+
fga.readLatestAuthorizationModel().get();
760+
761+
// Then
762+
mockHttpClient.verify().get(getUrl).called(1);
763+
assertNull(response.getAuthorizationModel());
764+
}
765+
749766
@Test
750767
public void readChanges() throws Exception {
751768
// Given

0 commit comments

Comments
 (0)