Skip to content

Commit b4ad6db

Browse files
committed
applied change to correct lib type
1 parent 9cab493 commit b4ad6db

File tree

1 file changed

+159
-3
lines changed
  • modules/openapi-generator/src/main/resources/Java/libraries/native

1 file changed

+159
-3
lines changed

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

Lines changed: 159 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ public class {{classname}} {
6464
private final Consumer<HttpResponse<InputStream>> memberVarResponseInterceptor;
6565
private final Consumer<HttpResponse<String>> memberVarAsyncResponseInterceptor;
6666
67+
{{#hasHttpBearerMethods}}
68+
// Per-API Bearer authentication
69+
private String bearerToken;
70+
{{/hasHttpBearerMethods}}
71+
72+
{{#hasHttpBasicMethods}}
73+
// Per-API Basic authentication
74+
private String username;
75+
private String password;
76+
{{/hasHttpBasicMethods}}
77+
78+
{{#hasApiKeyMethods}}
79+
// Per-API API key authentication
80+
private String apiKey;
81+
private String apiKeyPrefix;
82+
{{/hasApiKeyMethods}}
83+
84+
{{#hasOAuthMethods}}
85+
// Per-API OAuth authentication
86+
private String accessToken;
87+
{{/hasOAuthMethods}}
88+
6789
public {{classname}}() {
6890
this(Configuration.getDefaultApiClient());
6991
}
@@ -77,6 +99,127 @@ public class {{classname}} {
7799
memberVarResponseInterceptor = apiClient.getResponseInterceptor();
78100
memberVarAsyncResponseInterceptor = apiClient.getAsyncResponseInterceptor();
79101
}
102+
103+
{{#hasHttpBearerMethods}}
104+
/**
105+
* Helper method to set access token for Bearer authentication.
106+
* @param bearerToken Bearer token
107+
* @return {{classname}}
108+
*/
109+
public {{classname}} setBearerToken(String bearerToken) {
110+
this.bearerToken = bearerToken;
111+
return this;
112+
}
113+
{{/hasHttpBearerMethods}}
114+
115+
{{#hasHttpBasicMethods}}
116+
/**
117+
* Helper method to set username for HTTP basic authentication.
118+
* @param username Username
119+
* @return {{classname}}
120+
*/
121+
public {{classname}} setUsername(String username) {
122+
this.username = username;
123+
return this;
124+
}
125+
126+
/**
127+
* Helper method to set password for HTTP basic authentication.
128+
* @param password Password
129+
* @return {{classname}}
130+
*/
131+
public {{classname}} setPassword(String password) {
132+
this.password = password;
133+
return this;
134+
}
135+
{{/hasHttpBasicMethods}}
136+
137+
{{#hasApiKeyMethods}}
138+
/**
139+
* Helper method to set API key value for API key authentication.
140+
* @param apiKey API key
141+
* @return {{classname}}
142+
*/
143+
public {{classname}} setApiKey(String apiKey) {
144+
this.apiKey = apiKey;
145+
return this;
146+
}
147+
148+
/**
149+
* Helper method to set API key prefix for API key authentication.
150+
* @param apiKeyPrefix API key prefix
151+
* @return {{classname}}
152+
*/
153+
public {{classname}} setApiKeyPrefix(String apiKeyPrefix) {
154+
this.apiKeyPrefix = apiKeyPrefix;
155+
return this;
156+
}
157+
{{/hasApiKeyMethods}}
158+
159+
{{#hasOAuthMethods}}
160+
/**
161+
* Helper method to set access token for OAuth2 authentication.
162+
* @param accessToken Access token
163+
* @return {{classname}}
164+
*/
165+
public {{classname}} setAccessToken(String accessToken) {
166+
this.accessToken = accessToken;
167+
return this;
168+
}
169+
{{/hasOAuthMethods}}
170+
171+
/**
172+
* Apply authentication settings directly to request headers.
173+
* This avoids modifying the shared ApiClient's authentication state.
174+
*/
175+
private void applyAuthToHeaders(HttpRequest.Builder localVarRequestBuilder) {
176+
{{#hasHttpBearerMethods}}
177+
if (bearerToken != null) {
178+
localVarRequestBuilder.header("Authorization", "Bearer " + bearerToken);
179+
}
180+
{{/hasHttpBearerMethods}}
181+
{{#hasHttpBasicMethods}}
182+
if (username != null && password != null) {
183+
String credentials = java.util.Base64.getEncoder().encodeToString((username + ":" + password).getBytes());
184+
localVarRequestBuilder.header("Authorization", "Basic " + credentials);
185+
}
186+
{{/hasHttpBasicMethods}}
187+
{{#hasApiKeyMethods}}
188+
if (apiKey != null) {
189+
{{#authMethods}}{{#isApiKey}}{{#isKeyInHeader}}
190+
String keyValue = apiKeyPrefix != null ? apiKeyPrefix + " " + apiKey : apiKey;
191+
localVarRequestBuilder.header("{{keyParamName}}", keyValue);
192+
{{/isKeyInHeader}}{{/isApiKey}}{{/authMethods}}
193+
}
194+
{{/hasApiKeyMethods}}
195+
{{#hasOAuthMethods}}
196+
if (accessToken != null) {
197+
localVarRequestBuilder.header("Authorization", "Bearer " + accessToken);
198+
}
199+
{{/hasOAuthMethods}}
200+
}
201+
202+
/**
203+
* Apply authentication settings directly to query parameters.
204+
* This avoids modifying the shared ApiClient's authentication state.
205+
*/
206+
private String applyAuthToQueryParams(String queryString) {
207+
{{#hasApiKeyMethods}}
208+
if (apiKey != null) {
209+
{{#authMethods}}{{#isApiKey}}{{#isKeyInQuery}}
210+
String keyValue = apiKeyPrefix != null ? apiKeyPrefix + " " + apiKey : apiKey;
211+
String authParam = "{{keyParamName}}=" + keyValue;
212+
if (queryString != null && !queryString.isEmpty()) {
213+
return queryString + "&" + authParam;
214+
} else {
215+
return authParam;
216+
}
217+
{{/isKeyInQuery}}{{/isApiKey}}{{/authMethods}}
218+
}
219+
{{/hasApiKeyMethods}}
220+
return queryString;
221+
}
222+
80223
{{#asyncNative}}
81224

82225
private ApiException getApiException(String operationId, HttpResponse<String> response) {
@@ -439,13 +582,24 @@ public class {{classname}} {
439582
if (localVarQueryStringJoiner.length() != 0) {
440583
queryJoiner.add(localVarQueryStringJoiner.toString());
441584
}
442-
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath + '?' + queryJoiner.toString()));
585+
String finalQuery = applyAuthToQueryParams(queryJoiner.toString());
586+
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath + '?' + finalQuery));
443587
} else {
444-
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));
588+
String authQuery = applyAuthToQueryParams(null);
589+
if (authQuery != null && !authQuery.isEmpty()) {
590+
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath + '?' + authQuery));
591+
} else {
592+
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));
593+
}
445594
}
446595
{{/hasQueryParams}}
447596
{{^hasQueryParams}}
448-
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));
597+
String authQuery = applyAuthToQueryParams(null);
598+
if (authQuery != null && !authQuery.isEmpty()) {
599+
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath + '?' + authQuery));
600+
} else {
601+
localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));
602+
}
449603
{{/hasQueryParams}}
450604

451605
{{#headerParams}}
@@ -569,6 +723,8 @@ public class {{classname}} {
569723
if (memberVarReadTimeout != null) {
570724
localVarRequestBuilder.timeout(memberVarReadTimeout);
571725
}
726+
// Apply per-API authentication directly to the request
727+
applyAuthToHeaders(localVarRequestBuilder);
572728
if (memberVarInterceptor != null) {
573729
memberVarInterceptor.accept(localVarRequestBuilder);
574730
}

0 commit comments

Comments
 (0)