Skip to content

[Java] Generate valid code if no Authentication implementations present #5788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public void testGetAuthentications() {
}
}

@Ignore("There is no more basic auth in petstore security definitions")
@Test
public void testSetUsernameAndPassword() {
HttpBasicAuth auth = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public void testGetAuthentications() {
}
}

@Ignore("There is no more basic auth in petstore security definitions")
@Test
public void testSetUsernameAndPassword() {
HttpBasicAuth auth = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ public void testGetAuthentications() {
}
}

@Ignore("There is no more basic auth in petstore security definitions")
@Test
public void testSetUsernameAndPassword() {
HttpBasicAuth auth = null;
Expand Down
30 changes: 15 additions & 15 deletions bin/windows/java-petstore-all.bat
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
call .\bin\windows\java-petstore-feign-10x.bat
call .\bin\windows\java-petstore-feign.bat
call .\bin\windows\java-petstore-google-api-client.bat
call .\bin\windows\java-petstore-jersey1.bat
call .\bin\windows\java-petstore-jersey2-java6.bat
call .\bin\windows\java-petstore-jersey2-java7.bat
call .\bin\windows\java-petstore-jersey2-java8.bat
call .\bin\windows\java-petstore-feign.bat
call .\bin\windows\java-petstore-feign-10x.bat
call .\bin\windows\java-petstore-native.bat
call .\bin\windows\java-petstore-okhttp-gson.bat
call .\bin\windows\java-petstore-okhttp-gson-parcelable.bat
call .\bin\windows\java-petstore-okhttp-gson.bat
call .\bin\windows\java-petstore-rest-assured.bat
call .\bin\windows\java-petstore-rest-assured-jackson.bat
call .\bin\windows\java-petstore-resteasy.bat
call .\bin\windows\java-petstore-resttemplate-withxml.bat
call .\bin\windows\java-petstore-resttemplate.bat
call .\bin\windows\java-petstore-retrofit.bat
call .\bin\windows\java-petstore-retrofit2.bat
call .\bin\windows\java-petstore-retrofit2rx.bat
call .\bin\windows\java-petstore-retrofit2rx2.bat
call .\bin\windows\java8-petstore-jersey2.bat
call .\bin\windows\java-petstore-retrofit2-play24.bat
call .\bin\windows\java-petstore-retrofit2-play25.bat
call .\bin\windows\java-petstore-retrofit2-play26.bat
call .\bin\windows\java-petstore-jersey2-java6.bat
call .\bin\windows\java-petstore-resttemplate.bat
call .\bin\windows\java-petstore-resttemplate-withxml.bat
call .\bin\windows\java-petstore-webclient.bat
call .\bin\windows\java-petstore-resteasy.bat
call .\bin\windows\java-petstore-google-api-client.bat
call .\bin\windows\java-petstore-rest-assured.bat
call .\bin\windows\java-petstore-rest-assured-jackson.bat
call .\bin\windows\java-petstore-retrofit2.bat
call .\bin\windows\java-petstore-retrofit2rx.bat
call .\bin\windows\java-petstore-retrofit2rx2.bat
call .\bin\windows\java-petstore-vertx.bat
call .\bin\windows\java-petstore-webclient.bat
2 changes: 1 addition & 1 deletion bin/windows/java-petstore-jersey1.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ If Not Exist %executable% (
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
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
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

java %JAVA_OPTS% -jar %executable% %ags%
10 changes: 10 additions & 0 deletions bin/windows/java-petstore-jersey2-java7.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar

If Not Exist %executable% (
mvn clean package
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
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

java %JAVA_OPTS% -jar %executable% %ags%
2 changes: 1 addition & 1 deletion bin/windows/java-petstore-jersey2-java8.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ If Not Exist %executable% (
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
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
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

java %JAVA_OPTS% -jar %executable% %ags%
2 changes: 1 addition & 1 deletion bin/windows/java-petstore-rest-assured.bat
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ If Not Exist %executable% (
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
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
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

java %JAVA_OPTS% -jar %executable% %ags%
10 changes: 10 additions & 0 deletions bin/windows/java-petstore-webclient.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar

If Not Exist %executable% (
mvn clean package
)

REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M
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

java %JAVA_OPTS% -jar %executable% %ags%
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,6 @@ private Map<String, Object> buildSupportFileBundle(List<Object> allOperations, L
bundle.put("hasOAuthMethods", true);
bundle.put("oauthMethods", ProcessUtils.getOAuthMethods(authMethods));
}

if (ProcessUtils.hasHttpBearerMethods(authMethods)) {
bundle.put("hasHttpBearerMethods", true);
}
Expand Down Expand Up @@ -1445,8 +1444,6 @@ private List<CodegenSecurity> filterAuthMethods(List<CodegenSecurity> authMethod
return result;
}



protected File writeInputStreamToFile(String filename, InputStream in, String templateFile) throws IOException {
if (in != null) {
byte[] bytes = IOUtils.toByteArray(in);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public static boolean hasHttpSignatureMethods(List<CodegenSecurity> authMethods)
public static boolean hasHttpBearerMethods(List<CodegenSecurity> authMethods) {
if (authMethods != null && !authMethods.isEmpty()) {
for (CodegenSecurity cs : authMethods) {
if (Boolean.TRUE.equals(cs.isBasicBasic)) {
if (Boolean.TRUE.equals(cs.isBasicBearer)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ import java.io.UnsupportedEncodingException;
import java.text.DateFormat;

import {{invokerPackage}}.auth.Authentication;
{{#hasHttpBasicMethods}}
import {{invokerPackage}}.auth.HttpBasicAuth;
{{/hasHttpBasicMethods}}
{{#hasHttpBearerMethods}}
import {{invokerPackage}}.auth.HttpBearerAuth;
{{/hasHttpBearerMethods}}
{{#hasApiKeyMethods}}
import {{invokerPackage}}.auth.ApiKeyAuth;
{{/hasApiKeyMethods}}
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth;
{{/hasOAuthMethods}}
Expand Down Expand Up @@ -267,6 +273,24 @@ public class ApiClient {
return authentications.get(authName);
}

{{#hasHttpBearerMethods}}
/**
* Helper method to set access token for the first Bearer authentication.
* @param bearerToken Bearer token
*/
public void setBearerToken(String bearerToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBearerAuth) {
((HttpBearerAuth) auth).setBearerToken(bearerToken);
return;
}
}
throw new RuntimeException("No Bearer authentication configured!");
}

{{/hasHttpBearerMethods}}

{{#hasHttpBasicMethods}}
/**
* Helper method to set username for the first HTTP basic authentication.
* @param username Username
Expand Down Expand Up @@ -295,6 +319,9 @@ public class ApiClient {
throw new RuntimeException("No HTTP basic authentication configured!");
}

{{/hasHttpBasicMethods}}

{{#hasApiKeyMethods}}
/**
* Helper method to set API key value for the first API key authentication.
* @param apiKey the API key
Expand Down Expand Up @@ -323,6 +350,8 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!");
}

{{/hasApiKeyMethods}}

{{#hasOAuthMethods}}
/**
* Helper method to set access token for the first OAuth2 authentication.
Expand All @@ -340,20 +369,6 @@ public class ApiClient {

{{/hasOAuthMethods}}

/**
* Helper method to set access token for the first Bearer authentication.
* @param bearerToken Bearer token
*/
public void setBearerToken(String bearerToken) {
for (Authentication auth : authentications.values()) {
if (auth instanceof HttpBearerAuth) {
((HttpBearerAuth) auth).setBearerToken(bearerToken);
return;
}
}
throw new RuntimeException("No Bearer authentication configured!");
}

/**
* Set the User-Agent header's value (by adding to the default header map).
* @param userAgent User agent
Expand Down
Loading