Skip to content

Commit 47c0a19

Browse files
committed
avoid using shared state
1 parent 6eaa43c commit 47c0a19

File tree

2 files changed

+37
-101
lines changed

2 files changed

+37
-101
lines changed

modules/openapi-generator/src/main/resources/Java/ApiClient.mustache

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -846,14 +846,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
846846
}
847847
}
848848

849-
/**
850-
* Get authentications (key: authentication name, value: authentication).
851-
*
852-
* @return Map of authentication objects
853-
*/
854-
public Map<String, Authentication> getAuthentications() {
855-
return authentications;
856-
}
849+
857850

858851
/**
859852
* Update query and header parameters based on authentication settings.

modules/openapi-generator/src/main/resources/Java/api.mustache

Lines changed: 36 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,6 @@ import {{invokerPackage}}.ApiClient;
88
import {{invokerPackage}}.Configuration;
99
import {{modelPackage}}.*;
1010
import {{invokerPackage}}.Pair;
11-
import {{invokerPackage}}.auth.Authentication;
12-
{{#hasHttpBearerMethods}}
13-
import {{invokerPackage}}.auth.HttpBearerAuth;
14-
{{/hasHttpBearerMethods}}
15-
{{#hasHttpBasicMethods}}
16-
import {{invokerPackage}}.auth.HttpBasicAuth;
17-
{{/hasHttpBasicMethods}}
18-
{{#hasApiKeyMethods}}
19-
import {{invokerPackage}}.auth.ApiKeyAuth;
20-
{{/hasApiKeyMethods}}
21-
{{#hasOAuthMethods}}
22-
import {{invokerPackage}}.auth.OAuth;
23-
{{/hasOAuthMethods}}
2411

2512
{{#imports}}import {{import}};
2613
{{/imports}}
@@ -30,6 +17,9 @@ import java.util.ArrayList;
3017
import java.util.HashMap;
3118
import java.util.List;
3219
import java.util.Map;
20+
{{#hasHttpBasicMethods}}
21+
import java.util.Base64;
22+
{{/hasHttpBasicMethods}}
3323

3424
{{>generatedAnnotation}}
3525
{{#operations}}
@@ -135,93 +125,49 @@ public class {{classname}} {
135125
{{/hasOAuthMethods}}
136126

137127
/**
138-
* Apply authentication settings to the API client for this request only.
139-
* This does not modify the shared ApiClient's authentication state.
128+
* Apply authentication settings directly to request headers.
129+
* This avoids modifying the shared ApiClient's authentication state.
140130
*/
141-
private void applyAuthToApiClient() {
131+
private void applyAuthToHeaders(Map<String, String> headerParams) {
142132
{{#hasHttpBearerMethods}}
143133
if (bearerToken != null) {
144-
for (Authentication auth : apiClient.getAuthentications().values()) {
145-
if (auth instanceof HttpBearerAuth) {
146-
((HttpBearerAuth) auth).setBearerToken(bearerToken);
147-
break;
148-
}
149-
}
134+
headerParams.put("Authorization", "Bearer " + bearerToken);
150135
}
151136
{{/hasHttpBearerMethods}}
152137
{{#hasHttpBasicMethods}}
153-
if (username != null || password != null) {
154-
for (Authentication auth : apiClient.getAuthentications().values()) {
155-
if (auth instanceof HttpBasicAuth) {
156-
if (username != null) ((HttpBasicAuth) auth).setUsername(username);
157-
if (password != null) ((HttpBasicAuth) auth).setPassword(password);
158-
break;
159-
}
160-
}
138+
if (username != null && password != null) {
139+
String credentials = java.util.Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
140+
headerParams.put("Authorization", "Basic " + credentials);
161141
}
162142
{{/hasHttpBasicMethods}}
163143
{{#hasApiKeyMethods}}
164-
if (apiKey != null || apiKeyPrefix != null) {
165-
for (Authentication auth : apiClient.getAuthentications().values()) {
166-
if (auth instanceof ApiKeyAuth) {
167-
if (apiKey != null) ((ApiKeyAuth) auth).setApiKey(apiKey);
168-
if (apiKeyPrefix != null) ((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
169-
break;
170-
}
171-
}
144+
if (apiKey != null) {
145+
{{#authMethods}}{{#isApiKey}}{{#isKeyInHeader}}
146+
String keyValue = apiKeyPrefix != null ? apiKeyPrefix + " " + apiKey : apiKey;
147+
headerParams.put("{{keyParamName}}", keyValue);
148+
{{/isKeyInHeader}}{{/isApiKey}}{{/authMethods}}
172149
}
173150
{{/hasApiKeyMethods}}
174151
{{#hasOAuthMethods}}
175152
if (accessToken != null) {
176-
for (Authentication auth : apiClient.getAuthentications().values()) {
177-
if (auth instanceof OAuth) {
178-
((OAuth) auth).setAccessToken(accessToken);
179-
break;
180-
}
181-
}
153+
headerParams.put("Authorization", "Bearer " + accessToken);
182154
}
183155
{{/hasOAuthMethods}}
184156
}
185157

186158
/**
187-
* Clear authentication settings from the API client after the request.
188-
* This ensures no authentication state persists in the shared ApiClient.
159+
* Apply authentication settings directly to query parameters.
160+
* This avoids modifying the shared ApiClient's authentication state.
189161
*/
190-
private void clearAuthFromApiClient() {
191-
{{#hasHttpBearerMethods}}
192-
for (Authentication auth : apiClient.getAuthentications().values()) {
193-
if (auth instanceof HttpBearerAuth) {
194-
((HttpBearerAuth) auth).setBearerToken((String) null);
195-
break;
196-
}
197-
}
198-
{{/hasHttpBearerMethods}}
199-
{{#hasHttpBasicMethods}}
200-
for (Authentication auth : apiClient.getAuthentications().values()) {
201-
if (auth instanceof HttpBasicAuth) {
202-
((HttpBasicAuth) auth).setUsername(null);
203-
((HttpBasicAuth) auth).setPassword(null);
204-
break;
205-
}
206-
}
207-
{{/hasHttpBasicMethods}}
162+
private void applyAuthToQueryParams(List<Pair> queryParams) {
208163
{{#hasApiKeyMethods}}
209-
for (Authentication auth : apiClient.getAuthentications().values()) {
210-
if (auth instanceof ApiKeyAuth) {
211-
((ApiKeyAuth) auth).setApiKey(null);
212-
((ApiKeyAuth) auth).setApiKeyPrefix(null);
213-
break;
214-
}
164+
if (apiKey != null) {
165+
{{#authMethods}}{{#isApiKey}}{{#isKeyInQuery}}
166+
String keyValue = apiKeyPrefix != null ? apiKeyPrefix + " " + apiKey : apiKey;
167+
queryParams.add(new Pair("{{keyParamName}}", keyValue));
168+
{{/isKeyInQuery}}{{/isApiKey}}{{/authMethods}}
215169
}
216170
{{/hasApiKeyMethods}}
217-
{{#hasOAuthMethods}}
218-
for (Authentication auth : apiClient.getAuthentications().values()) {
219-
if (auth instanceof OAuth) {
220-
((OAuth) auth).setAccessToken(null);
221-
break;
222-
}
223-
}
224-
{{/hasOAuthMethods}}
225171
}
226172

227173
{{#operation}}
@@ -281,6 +227,10 @@ public class {{classname}} {
281227
localVarFormParams.put("{{baseName}}", {{paramName}});
282228
{{/formParams}}
283229

230+
// Apply authentication directly to request parameters (no shared state modification)
231+
applyAuthToHeaders(localVarHeaderParams);
232+
applyAuthToQueryParams(localVarQueryParams);
233+
284234
final String[] localVarAccepts = {
285235
{{#produces}}"{{{mediaType}}}"{{^-last}}, {{/-last}}{{/produces}}
286236
};
@@ -291,22 +241,15 @@ public class {{classname}} {
291241
};
292242
final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
293243

294-
String[] localVarAuthNames = new String[] { {{#authMethods}}"{{name}}"{{^-last}}, {{/-last}}{{/authMethods}} };
295-
296-
// Apply authentication for this request only
297-
applyAuthToApiClient();
244+
// No authentication names needed - auth is applied directly above
245+
String[] localVarAuthNames = new String[] {};
298246

299-
try {
300-
{{#returnType}}
301-
GenericType<{{{returnType}}}> localVarReturnType = new GenericType<{{{returnType}}}>() {};
302-
return apiClient.invokeAPI(localVarPath, "{{httpMethod}}", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, localVarAuthNames, localVarReturnType);
303-
{{/returnType}}{{^returnType}}
304-
apiClient.invokeAPI(localVarPath, "{{httpMethod}}", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, localVarAuthNames, null);
305-
{{/returnType}}
306-
} finally {
307-
// Clear authentication to prevent it from persisting in the shared ApiClient
308-
clearAuthFromApiClient();
309-
}
247+
{{#returnType}}
248+
GenericType<{{{returnType}}}> localVarReturnType = new GenericType<{{{returnType}}}>() {};
249+
return apiClient.invokeAPI(localVarPath, "{{httpMethod}}", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, localVarAuthNames, localVarReturnType);
250+
{{/returnType}}{{^returnType}}
251+
apiClient.invokeAPI(localVarPath, "{{httpMethod}}", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, localVarAuthNames, null);
252+
{{/returnType}}
310253
}
311254
{{/operation}}
312255
}

0 commit comments

Comments
 (0)