Skip to content

Commit 950508f

Browse files
authored
[Java] Generate valid code if no Authentication implementations present (#5788)
* generate valid code if no Authentication implementations present resurrects #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 f200122 commit 950508f

File tree

19 files changed

+296
-288
lines changed

19 files changed

+296
-288
lines changed

CI/samples.ci/client/petstore/java/test-manual/jersey1/ApiClientTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ public void testGetAuthentications() {
122122
}
123123
}
124124

125-
@Ignore("There is no more basic auth in petstore security definitions")
126125
@Test
127126
public void testSetUsernameAndPassword() {
128127
HttpBasicAuth auth = null;

CI/samples.ci/client/petstore/java/test-manual/jersey2/ApiClientTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ public void testGetAuthentications() {
122122
}
123123
}
124124

125-
@Ignore("There is no more basic auth in petstore security definitions")
126125
@Test
127126
public void testSetUsernameAndPassword() {
128127
HttpBasicAuth auth = null;

CI/samples.ci/client/petstore/java/test-manual/resttemplate/ApiClientTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ public void testGetAuthentications() {
128128
}
129129
}
130130

131-
@Ignore("There is no more basic auth in petstore security definitions")
132131
@Test
133132
public void testSetUsernameAndPassword() {
134133
HttpBasicAuth auth = null;

bin/windows/java-petstore-all.bat

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1+
call .\bin\windows\java-petstore-feign-10x.bat
2+
call .\bin\windows\java-petstore-feign.bat
3+
call .\bin\windows\java-petstore-google-api-client.bat
14
call .\bin\windows\java-petstore-jersey1.bat
5+
call .\bin\windows\java-petstore-jersey2-java6.bat
6+
call .\bin\windows\java-petstore-jersey2-java7.bat
27
call .\bin\windows\java-petstore-jersey2-java8.bat
3-
call .\bin\windows\java-petstore-feign.bat
4-
call .\bin\windows\java-petstore-feign-10x.bat
58
call .\bin\windows\java-petstore-native.bat
6-
call .\bin\windows\java-petstore-okhttp-gson.bat
79
call .\bin\windows\java-petstore-okhttp-gson-parcelable.bat
10+
call .\bin\windows\java-petstore-okhttp-gson.bat
11+
call .\bin\windows\java-petstore-rest-assured.bat
12+
call .\bin\windows\java-petstore-rest-assured-jackson.bat
13+
call .\bin\windows\java-petstore-resteasy.bat
14+
call .\bin\windows\java-petstore-resttemplate-withxml.bat
15+
call .\bin\windows\java-petstore-resttemplate.bat
816
call .\bin\windows\java-petstore-retrofit.bat
9-
call .\bin\windows\java-petstore-retrofit2.bat
10-
call .\bin\windows\java-petstore-retrofit2rx.bat
11-
call .\bin\windows\java-petstore-retrofit2rx2.bat
12-
call .\bin\windows\java8-petstore-jersey2.bat
1317
call .\bin\windows\java-petstore-retrofit2-play24.bat
1418
call .\bin\windows\java-petstore-retrofit2-play25.bat
1519
call .\bin\windows\java-petstore-retrofit2-play26.bat
16-
call .\bin\windows\java-petstore-jersey2-java6.bat
17-
call .\bin\windows\java-petstore-resttemplate.bat
18-
call .\bin\windows\java-petstore-resttemplate-withxml.bat
19-
call .\bin\windows\java-petstore-webclient.bat
20-
call .\bin\windows\java-petstore-resteasy.bat
21-
call .\bin\windows\java-petstore-google-api-client.bat
22-
call .\bin\windows\java-petstore-rest-assured.bat
23-
call .\bin\windows\java-petstore-rest-assured-jackson.bat
20+
call .\bin\windows\java-petstore-retrofit2.bat
21+
call .\bin\windows\java-petstore-retrofit2rx.bat
22+
call .\bin\windows\java-petstore-retrofit2rx2.bat
2423
call .\bin\windows\java-petstore-vertx.bat
24+
call .\bin\windows\java-petstore-webclient.bat

bin/windows/java-petstore-jersey1.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ If Not Exist %executable% (
55
)
66

77
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
8-
set ags=generate --artifact-id petstore-java-client-jersey1 -t modules\openapi-generator\src\main\resources\Java -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -o samples\client\petstore\java\jersey1 --additional-properties hideGenerationTimestamp=true --library=jersey1 --additional-properties useNullForUnknownEnumValue=true
8+
set ags=generate --artifact-id petstore-java-client-jersey1 -t modules\openapi-generator\src\main\resources\Java -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -o samples\client\petstore\java\jersey1 --additional-properties hideGenerationTimestamp=true --library=jersey1
99

1010
java %JAVA_OPTS% -jar %executable% %ags%
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar
2+
3+
If Not Exist %executable% (
4+
mvn clean package
5+
)
6+
7+
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
8+
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-jersey2-java7.json -o samples\client\petstore\java\jersey2-java7 --additional-properties hideGenerationTimestamp=true
9+
10+
java %JAVA_OPTS% -jar %executable% %ags%

bin/windows/java-petstore-jersey2-java8.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ If Not Exist %executable% (
55
)
66

77
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
8-
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-jersey2-java7.json -o samples\client\petstore\java\jersey2 --additional-properties hideGenerationTimestamp=true
8+
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-jersey2-java8.json -o samples\client\petstore\java\jersey2-java8 --additional-properties hideGenerationTimestamp=true
99

1010
java %JAVA_OPTS% -jar %executable% %ags%

bin/windows/java-petstore-rest-assured.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ If Not Exist %executable% (
55
)
66

77
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
8-
set ags=generate -t modules\openapi-generator\src\main\resources\Java\libraries\rest-assured -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-rest-assured.json -o samples\client\petstore\java\rest-assured --additional-properties hideGenerationTimestamp=true,booleanGetterPrefix=is
8+
set ags=generate -t modules\openapi-generator\src\main\resources\Java\libraries\rest-assured -i modules\openapi-generator\src\test\resources\2_0\petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin\java-petstore-rest-assured.json -o samples\client\petstore\java\rest-assured --additional-properties hideGenerationTimestamp=true --additional-properties useBeanValidation=true --additional-properties performBeanValidation=true --additional-properties booleanGetterPrefix=is
99

1010
java %JAVA_OPTS% -jar %executable% %ags%
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar
2+
3+
If Not Exist %executable% (
4+
mvn clean package
5+
)
6+
7+
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
8+
set ags=generate -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-webclient.json -o samples/client/petstore/java/webclient --additional-properties hideGenerationTimestamp=true
9+
10+
java %JAVA_OPTS% -jar %executable% %ags%

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultGenerator.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,6 @@ private Map<String, Object> buildSupportFileBundle(List<Object> allOperations, L
933933
bundle.put("hasOAuthMethods", true);
934934
bundle.put("oauthMethods", ProcessUtils.getOAuthMethods(authMethods));
935935
}
936-
937936
if (ProcessUtils.hasHttpBearerMethods(authMethods)) {
938937
bundle.put("hasHttpBearerMethods", true);
939938
}
@@ -1473,8 +1472,6 @@ private List<CodegenSecurity> filterAuthMethods(List<CodegenSecurity> authMethod
14731472
return result;
14741473
}
14751474

1476-
1477-
14781475
protected File writeInputStreamToFile(String filename, InputStream in, String templateFile) throws IOException {
14791476
if (in != null) {
14801477
byte[] bytes = IOUtils.toByteArray(in);

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ProcessUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public static boolean hasHttpSignatureMethods(List<CodegenSecurity> authMethods)
108108
public static boolean hasHttpBearerMethods(List<CodegenSecurity> authMethods) {
109109
if (authMethods != null && !authMethods.isEmpty()) {
110110
for (CodegenSecurity cs : authMethods) {
111-
if (Boolean.TRUE.equals(cs.isBasicBasic)) {
111+
if (Boolean.TRUE.equals(cs.isBasicBearer)) {
112112
return true;
113113
}
114114
}

modules/openapi-generator/src/main/resources/Java/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

0 commit comments

Comments
 (0)