Skip to content

Commit 258b743

Browse files
committed
fix: dedupe,interrupexception handle
1 parent 81fa5cf commit 258b743

5 files changed

Lines changed: 40 additions & 24 deletions

File tree

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,19 @@ protected HttpRequest buildHttpRequest(String method, String path, Object body,
168168
byte[] bodyBytes = objectMapper.writeValueAsBytes(body);
169169
HttpRequest.Builder requestBuilder = ApiClient.requestBuilder(method, path, bodyBytes, configuration);
170170

171-
// Attach authorization header if credentials are configured
172-
String accessToken = apiClient.getAccessToken(configuration);
173-
if (accessToken != null) {
174-
requestBuilder.header("Authorization", "Bearer " + accessToken);
175-
}
176-
177171
// Apply request interceptors if any
178172
var interceptor = apiClient.getRequestInterceptor();
179173
if (interceptor != null) {
180174
interceptor.accept(requestBuilder);
181175
}
182176

177+
// Attach authorization header if credentials are configured
178+
// Applied after interceptors to avoid duplicate headers
179+
String accessToken = apiClient.getAccessToken(configuration);
180+
if (accessToken != null) {
181+
requestBuilder.setHeader("Authorization", "Bearer " + accessToken);
182+
}
183+
183184
return requestBuilder.build();
184185
} catch (ApiException e) {
185186
throw e;

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

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,20 +1347,6 @@ private String pathWithParams(String basePath, Object... params) {
13471347
* @throws IllegalStateException when the configuration is invalid
13481348
*/
13491349
private String getAccessToken(Configuration configuration) throws ApiException {
1350-
CredentialsMethod credentialsMethod = configuration.getCredentials().getCredentialsMethod();
1351-
1352-
if (credentialsMethod == CredentialsMethod.API_TOKEN) {
1353-
return configuration.getCredentials().getApiToken().getToken();
1354-
}
1355-
1356-
if (credentialsMethod == CredentialsMethod.CLIENT_CREDENTIALS) {
1357-
try {
1358-
return oAuth2Client.getAccessToken().get();
1359-
} catch (Exception e) {
1360-
throw new ApiException(e);
1361-
}
1362-
}
1363-
1364-
throw new IllegalStateException("Configuration is invalid.");
1350+
return apiClient.getAccessToken(configuration);
13651351
}
13661352
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,9 @@ public String getAccessToken(Configuration configuration) throws ApiException {
376376
return oAuth2Client.getAccessToken().get();
377377
} catch (ApiException e) {
378378
throw e;
379+
} catch (InterruptedException e) {
380+
Thread.currentThread().interrupt();
381+
throw new ApiException(e);
379382
} catch (Exception e) {
380383
throw new ApiException(e);
381384
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,17 @@ HttpRequest buildHttpRequest(Configuration configuration, ApiClient apiClient)
206206
httpRequestBuilder = ApiClient.requestBuilder(method.name(), resolvedPath, configuration);
207207
}
208208

209-
// Attach authorization header if credentials are configured
209+
headers.forEach(httpRequestBuilder::header);
210+
211+
// Attach authorization header if credentials are configured and the caller
212+
// has not already provided one.
213+
boolean hasAuthorizationHeader =
214+
headers.keySet().stream().anyMatch("Authorization"::equalsIgnoreCase);
210215
String accessToken = apiClient.getAccessToken(configuration);
211-
if (accessToken != null) {
216+
if (!hasAuthorizationHeader && accessToken != null) {
212217
httpRequestBuilder.header("Authorization", "Bearer " + accessToken);
213218
}
214219

215-
headers.forEach(httpRequestBuilder::header);
216220

217221
if (apiClient.getRequestInterceptor() != null) {
218222
apiClient.getRequestInterceptor().accept(httpRequestBuilder);

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
1414
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
1515
import com.pgssoft.httpclient.HttpClientMock;
16+
import dev.openfga.sdk.api.auth.OAuth2Client;
1617
import dev.openfga.sdk.api.client.model.*;
1718
import dev.openfga.sdk.api.configuration.*;
1819
import dev.openfga.sdk.api.model.*;
@@ -88,6 +89,27 @@ public void beforeEachTest() throws Exception {
8889
when(mockApiClient.getObjectMapper()).thenReturn(new ObjectMapper());
8990
when(mockApiClient.getHttpClientBuilder()).thenReturn(mockHttpClientBuilder);
9091

92+
// Allow setOAuth2Client to store the value and getAccessToken to use real logic
93+
final OAuth2Client[] oAuth2ClientHolder = new OAuth2Client[1];
94+
doAnswer(invocation -> {
95+
oAuth2ClientHolder[0] = invocation.getArgument(0);
96+
return null;
97+
}).when(mockApiClient).setOAuth2Client(any());
98+
when(mockApiClient.getAccessToken(any())).thenAnswer(invocation -> {
99+
Configuration config = invocation.getArgument(0);
100+
var credentialsMethod = config.getCredentials().getCredentialsMethod();
101+
if (credentialsMethod == dev.openfga.sdk.api.configuration.CredentialsMethod.NONE) {
102+
return null;
103+
}
104+
if (credentialsMethod == dev.openfga.sdk.api.configuration.CredentialsMethod.API_TOKEN) {
105+
return config.getCredentials().getApiToken().getToken();
106+
}
107+
if (credentialsMethod == dev.openfga.sdk.api.configuration.CredentialsMethod.CLIENT_CREDENTIALS) {
108+
return oAuth2ClientHolder[0].getAccessToken().get();
109+
}
110+
return null;
111+
});
112+
91113
fga = new OpenFgaClient(clientConfiguration, mockApiClient);
92114
}
93115

0 commit comments

Comments
 (0)