From ef65e739f1c1b59bcf8fb835e7695688bdb02be2 Mon Sep 17 00:00:00 2001 From: Philipp Reiter Date: Fri, 24 Jan 2025 13:27:30 +0100 Subject: [PATCH] fix: Do not ignore request level storeId override in non-transactional write --- client/client.go | 5 ++--- client/client_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/client/client.go b/client/client.go index 9c53134..d5a95cd 100644 --- a/client/client.go +++ b/client/client.go @@ -445,21 +445,18 @@ type SdkClient interface { */ SetAuthorizationModelId(authorizationModelId string) error - /* * GetAuthorizationModelId retrieves the Authorization Model ID for an OpenFGAClient. * @return string */ GetAuthorizationModelId() (string, error) - /* * SetStoreId allows setting the Store ID for an OpenFGAClient. * @param string storeId - The Store ID to set. */ SetStoreId(storeId string) error - /* * GetStoreId retrieves the Store ID set in the OpenFGAClient. * @return string @@ -1605,6 +1602,7 @@ func (client *OpenFgaClient) WriteExecute(request SdkClientWriteRequestInterface }, options: &ClientWriteOptions{ AuthorizationModelId: authorizationModelId, + StoreId: request.GetStoreIdOverride(), }, }) @@ -1648,6 +1646,7 @@ func (client *OpenFgaClient) WriteExecute(request SdkClientWriteRequestInterface }, options: &ClientWriteOptions{ AuthorizationModelId: authorizationModelId, + StoreId: request.GetStoreIdOverride(), }, }) diff --git a/client/client_test.go b/client/client_test.go index c9e09eb..76258a8 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -1492,6 +1492,38 @@ func TestOpenFgaClient(t *testing.T) { t.Fatalf("%v", err) } } + + // store can be overridden + storeOverrideOptions := ClientWriteOptions{ + AuthorizationModelId: openfga.PtrString(authModelId), + Transaction: &TransactionOptions{ + Disable: true, + MaxParallelRequests: 5, + MaxPerChunk: 1, + }, + StoreId: openfga.PtrString("7777HCE4YVKPQEKZQHT2R89MQV"), + } + httpmock.Reset() + httpmock.RegisterResponder(test.Method, fmt.Sprintf("%s/stores/%s/%s", fgaClient.GetConfig().ApiUrl, *storeOverrideOptions.StoreId, test.RequestPath), + func(req *http.Request) (*http.Response, error) { + resp, err := httpmock.NewJsonResponse(test.ResponseStatus, expectedResponse) + if err != nil { + return httpmock.NewStringResponse(http.StatusInternalServerError, ""), nil + } + return resp, nil + }, + ) + data, err = fgaClient.Write(context.Background()).Body(requestBody).Options(storeOverrideOptions).Execute() + if err != nil { + t.Fatalf("%v", data) + } + + for index := 0; index < len(data.Writes); index++ { + response := data.Writes[index] + if response.Error != nil { + t.Fatalf("OpenFgaClient.%v()|%d/ %v", test.Name, index, response.Error) + } + } }) t.Run("Write with invalid auth", func(t *testing.T) {