Skip to content

Commit ab3da5e

Browse files
authored
[Java] Generate valid code if no Authentication implementations present (#5788)
* generate valid code if no Authentication implementations present resurrects OpenAPITools/openapi-generator#2861 * remove what I assume are human generated test cases * need to iterate over authMethods in order to pull out name * fix another test * update more tests * rename hasTokenAuthMethods to hasApiKeyAuthMethods * remove duplicate methods, fix hasHttpBearerMethods check * update templates * update windows java-petstore files * update windows java-petstore files * re-generate * re-generate * restore samples.ci tests * restore samples.ci tests
1 parent 1800478 commit ab3da5e

File tree

2 files changed

+124
-93
lines changed

2 files changed

+124
-93
lines changed

.generator/templates/ApiClient.mustache

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,15 @@ import java.io.UnsupportedEncodingException;
5353
import java.text.DateFormat;
5454

5555
import {{invokerPackage}}.auth.Authentication;
56+
{{#hasHttpBasicMethods}}
5657
import {{invokerPackage}}.auth.HttpBasicAuth;
58+
{{/hasHttpBasicMethods}}
59+
{{#hasHttpBearerMethods}}
5760
import {{invokerPackage}}.auth.HttpBearerAuth;
61+
{{/hasHttpBearerMethods}}
62+
{{#hasApiKeyMethods}}
5863
import {{invokerPackage}}.auth.ApiKeyAuth;
64+
{{/hasApiKeyMethods}}
5965
{{#hasOAuthMethods}}
6066
import {{invokerPackage}}.auth.OAuth;
6167
{{/hasOAuthMethods}}
@@ -267,6 +273,24 @@ public class ApiClient {
267273
return authentications.get(authName);
268274
}
269275

276+
{{#hasHttpBearerMethods}}
277+
/**
278+
* Helper method to set access token for the first Bearer authentication.
279+
* @param bearerToken Bearer token
280+
*/
281+
public void setBearerToken(String bearerToken) {
282+
for (Authentication auth : authentications.values()) {
283+
if (auth instanceof HttpBearerAuth) {
284+
((HttpBearerAuth) auth).setBearerToken(bearerToken);
285+
return;
286+
}
287+
}
288+
throw new RuntimeException("No Bearer authentication configured!");
289+
}
290+
291+
{{/hasHttpBearerMethods}}
292+
293+
{{#hasHttpBasicMethods}}
270294
/**
271295
* Helper method to set username for the first HTTP basic authentication.
272296
* @param username Username
@@ -295,6 +319,9 @@ public class ApiClient {
295319
throw new RuntimeException("No HTTP basic authentication configured!");
296320
}
297321

322+
{{/hasHttpBasicMethods}}
323+
324+
{{#hasApiKeyMethods}}
298325
/**
299326
* Helper method to set API key value for the first API key authentication.
300327
* @param apiKey the API key
@@ -323,6 +350,8 @@ public class ApiClient {
323350
throw new RuntimeException("No API key authentication configured!");
324351
}
325352

353+
{{/hasApiKeyMethods}}
354+
326355
{{#hasOAuthMethods}}
327356
/**
328357
* Helper method to set access token for the first OAuth2 authentication.
@@ -340,20 +369,6 @@ public class ApiClient {
340369

341370
{{/hasOAuthMethods}}
342371

343-
/**
344-
* Helper method to set access token for the first Bearer authentication.
345-
* @param bearerToken Bearer token
346-
*/
347-
public void setBearerToken(String bearerToken) {
348-
for (Authentication auth : authentications.values()) {
349-
if (auth instanceof HttpBearerAuth) {
350-
((HttpBearerAuth) auth).setBearerToken(bearerToken);
351-
return;
352-
}
353-
}
354-
throw new RuntimeException("No Bearer authentication configured!");
355-
}
356-
357372
/**
358373
* Set the User-Agent header's value (by adding to the default header map).
359374
* @param userAgent User agent

.generator/templates/libraries/resttemplate/ApiClient.mustache

Lines changed: 95 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,15 @@ import java.util.Map.Entry;
6666
import java.util.TimeZone;
6767

6868
import {{invokerPackage}}.auth.Authentication;
69+
{{#hasHttpBasicMethods}}
6970
import {{invokerPackage}}.auth.HttpBasicAuth;
71+
{{/hasHttpBasicMethods}}
72+
{{#hasHttpBearerMethods}}
7073
import {{invokerPackage}}.auth.HttpBearerAuth;
74+
{{/hasHttpBearerMethods}}
75+
{{#hasApiKeyMethods}}
7176
import {{invokerPackage}}.auth.ApiKeyAuth;
77+
{{/hasApiKeyMethods}}
7278
{{#hasOAuthMethods}}
7379
import {{invokerPackage}}.auth.OAuth;
7480
{{/hasOAuthMethods}}
@@ -170,93 +176,103 @@ public class ApiClient {
170176
return authentications.get(authName);
171177
}
172178

173-
/**
174-
* Helper method to set token for HTTP bearer authentication.
175-
* @param bearerToken the token
176-
*/
177-
public void setBearerToken(String bearerToken) {
178-
for (Authentication auth : authentications.values()) {
179-
if (auth instanceof HttpBearerAuth) {
180-
((HttpBearerAuth) auth).setBearerToken(bearerToken);
181-
return;
182-
}
179+
{{#hasHttpBearerMethods}}
180+
/**
181+
* Helper method to set token for HTTP bearer authentication.
182+
* @param bearerToken the token
183+
*/
184+
public void setBearerToken(String bearerToken) {
185+
for (Authentication auth : authentications.values()) {
186+
if (auth instanceof HttpBearerAuth) {
187+
((HttpBearerAuth) auth).setBearerToken(bearerToken);
188+
return;
183189
}
184-
throw new RuntimeException("No Bearer authentication configured!");
185190
}
186-
187-
/**
188-
* Helper method to set username for the first HTTP basic authentication.
189-
* @param username the username
190-
*/
191-
public void setUsername(String username) {
192-
for (Authentication auth : authentications.values()) {
193-
if (auth instanceof HttpBasicAuth) {
194-
((HttpBasicAuth) auth).setUsername(username);
195-
return;
196-
}
197-
}
198-
throw new RuntimeException("No HTTP basic authentication configured!");
191+
throw new RuntimeException("No Bearer authentication configured!");
192+
}
193+
194+
{{/hasHttpBearerMethods}}
195+
196+
{{#hasHttpBasicMethods}}
197+
/**
198+
* Helper method to set username for the first HTTP basic authentication.
199+
* @param username Username
200+
*/
201+
public void setUsername(String username) {
202+
for (Authentication auth : authentications.values()) {
203+
if (auth instanceof HttpBasicAuth) {
204+
((HttpBasicAuth) auth).setUsername(username);
205+
return;
206+
}
199207
}
200-
201-
/**
202-
* Helper method to set password for the first HTTP basic authentication.
203-
* @param password the password
204-
*/
205-
public void setPassword(String password) {
206-
for (Authentication auth : authentications.values()) {
207-
if (auth instanceof HttpBasicAuth) {
208-
((HttpBasicAuth) auth).setPassword(password);
209-
return;
210-
}
211-
}
212-
throw new RuntimeException("No HTTP basic authentication configured!");
208+
throw new RuntimeException("No HTTP basic authentication configured!");
209+
}
210+
211+
/**
212+
* Helper method to set password for the first HTTP basic authentication.
213+
* @param password Password
214+
*/
215+
public void setPassword(String password) {
216+
for (Authentication auth : authentications.values()) {
217+
if (auth instanceof HttpBasicAuth) {
218+
((HttpBasicAuth) auth).setPassword(password);
219+
return;
220+
}
213221
}
214-
215-
/**
216-
* Helper method to set API key value for the first API key authentication.
217-
* @param apiKey the API key
218-
*/
219-
public void setApiKey(String apiKey) {
220-
for (Authentication auth : authentications.values()) {
221-
if (auth instanceof ApiKeyAuth) {
222-
((ApiKeyAuth) auth).setApiKey(apiKey);
223-
return;
224-
}
225-
}
226-
throw new RuntimeException("No API key authentication configured!");
222+
throw new RuntimeException("No HTTP basic authentication configured!");
223+
}
224+
225+
{{/hasHttpBasicMethods}}
226+
227+
{{#hasApiKeyMethods}}
228+
/**
229+
* Helper method to set API key value for the first API key authentication.
230+
* @param apiKey the API key
231+
*/
232+
public void setApiKey(String apiKey) {
233+
for (Authentication auth : authentications.values()) {
234+
if (auth instanceof ApiKeyAuth) {
235+
((ApiKeyAuth) auth).setApiKey(apiKey);
236+
return;
237+
}
227238
}
228-
229-
/**
230-
* Helper method to set API key prefix for the first API key authentication.
231-
* @param apiKeyPrefix the API key prefix
232-
*/
233-
public void setApiKeyPrefix(String apiKeyPrefix) {
234-
for (Authentication auth : authentications.values()) {
235-
if (auth instanceof ApiKeyAuth) {
236-
((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
237-
return;
238-
}
239-
}
240-
throw new RuntimeException("No API key authentication configured!");
239+
throw new RuntimeException("No API key authentication configured!");
240+
}
241+
242+
/**
243+
* Helper method to set API key prefix for the first API key authentication.
244+
* @param apiKeyPrefix API key prefix
245+
*/
246+
public void setApiKeyPrefix(String apiKeyPrefix) {
247+
for (Authentication auth : authentications.values()) {
248+
if (auth instanceof ApiKeyAuth) {
249+
((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
250+
return;
251+
}
241252
}
242-
243-
{{#hasOAuthMethods}}
244-
/**
245-
* Helper method to set access token for the first OAuth2 authentication.
246-
* @param accessToken the access token
247-
*/
248-
public void setAccessToken(String accessToken) {
249-
for (Authentication auth : authentications.values()) {
250-
if (auth instanceof OAuth) {
251-
((OAuth) auth).setAccessToken(accessToken);
252-
return;
253-
}
254-
}
255-
throw new RuntimeException("No OAuth2 authentication configured!");
253+
throw new RuntimeException("No API key authentication configured!");
254+
}
255+
256+
{{/hasApiKeyMethods}}
257+
258+
{{#hasOAuthMethods}}
259+
/**
260+
* Helper method to set access token for the first OAuth2 authentication.
261+
* @param accessToken Access token
262+
*/
263+
public void setAccessToken(String accessToken) {
264+
for (Authentication auth : authentications.values()) {
265+
if (auth instanceof OAuth) {
266+
((OAuth) auth).setAccessToken(accessToken);
267+
return;
268+
}
256269
}
270+
throw new RuntimeException("No OAuth2 authentication configured!");
271+
}
257272

258-
{{/hasOAuthMethods}}
259-
/**
273+
{{/hasOAuthMethods}}
274+
275+
/**
260276
* Set the User-Agent header's value (by adding to the default header map).
261277
* @param userAgent the user agent string
262278
* @return ApiClient this client

0 commit comments

Comments
 (0)