[Java] Generate OAuth related files only if OAuth security schema is used (#1907)

* optionally include oauth files in java client

* fix java templates

* fix tests

* fix CI issues
This commit is contained in:
William Cheng 2019-01-28 11:05:37 +08:00 committed by GitHub
parent 9029103c94
commit 887b688014
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 151 additions and 62 deletions

View File

@ -31,6 +31,7 @@ import org.openapitools.codegen.languages.features.BeanValidationFeatures;
import org.openapitools.codegen.languages.features.GzipFeatures; import org.openapitools.codegen.languages.features.GzipFeatures;
import org.openapitools.codegen.languages.features.PerformBeanValidationFeatures; import org.openapitools.codegen.languages.features.PerformBeanValidationFeatures;
import org.openapitools.codegen.mustache.CaseFormatLambda; import org.openapitools.codegen.mustache.CaseFormatLambda;
import org.openapitools.codegen.utils.ProcessUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -101,6 +102,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
protected boolean useGzipFeature = false; protected boolean useGzipFeature = false;
protected boolean useRuntimeException = false; protected boolean useRuntimeException = false;
protected boolean useReflectionEqualsHashCode = false; protected boolean useReflectionEqualsHashCode = false;
protected String authFolder;
public JavaClientCodegen() { public JavaClientCodegen() {
super(); super();
@ -236,8 +238,8 @@ public class JavaClientCodegen extends AbstractJavaCodegen
} }
final String invokerFolder = (sourceFolder + '/' + invokerPackage).replace(".", "/"); final String invokerFolder = (sourceFolder + '/' + invokerPackage).replace(".", "/");
final String authFolder = (sourceFolder + '/' + invokerPackage + ".auth").replace(".", "/");
final String apiFolder = (sourceFolder + '/' + apiPackage).replace(".", "/"); final String apiFolder = (sourceFolder + '/' + apiPackage).replace(".", "/");
authFolder = (sourceFolder + '/' + invokerPackage + ".auth").replace(".", "/");
//Common files //Common files
writeOptional(outputFolder, new SupportingFile("pom.mustache", "", "pom.xml")); writeOptional(outputFolder, new SupportingFile("pom.mustache", "", "pom.xml"));
@ -253,12 +255,13 @@ public class JavaClientCodegen extends AbstractJavaCodegen
supportingFiles.add(new SupportingFile("StringUtil.mustache", invokerFolder, "StringUtil.java")); supportingFiles.add(new SupportingFile("StringUtil.mustache", invokerFolder, "StringUtil.java"));
} }
// google-api-client doesn't use the Swagger auth, because it uses Google Credential directly (HttpRequestInitializer) // google-api-client doesn't use the OpenAPI auth, because it uses Google Credential directly (HttpRequestInitializer)
if (!(GOOGLE_API_CLIENT.equals(getLibrary()) || REST_ASSURED.equals(getLibrary()))) { if (!(GOOGLE_API_CLIENT.equals(getLibrary()) || REST_ASSURED.equals(getLibrary()))) {
supportingFiles.add(new SupportingFile("auth/HttpBasicAuth.mustache", authFolder, "HttpBasicAuth.java")); supportingFiles.add(new SupportingFile("auth/HttpBasicAuth.mustache", authFolder, "HttpBasicAuth.java"));
supportingFiles.add(new SupportingFile("auth/ApiKeyAuth.mustache", authFolder, "ApiKeyAuth.java")); supportingFiles.add(new SupportingFile("auth/ApiKeyAuth.mustache", authFolder, "ApiKeyAuth.java"));
supportingFiles.add(new SupportingFile("auth/OAuth.mustache", authFolder, "OAuth.java")); // NOTE: below moved to postProcessOperationsWithModels
supportingFiles.add(new SupportingFile("auth/OAuthFlow.mustache", authFolder, "OAuthFlow.java")); //supportingFiles.add(new SupportingFile("auth/OAuth.mustache", authFolder, "OAuth.java"));
//supportingFiles.add(new SupportingFile("auth/OAuthFlow.mustache", authFolder, "OAuthFlow.java"));
} }
supportingFiles.add(new SupportingFile("gradlew.mustache", "", "gradlew")); supportingFiles.add(new SupportingFile("gradlew.mustache", "", "gradlew"));
supportingFiles.add(new SupportingFile("gradlew.bat.mustache", "", "gradlew.bat")); supportingFiles.add(new SupportingFile("gradlew.bat.mustache", "", "gradlew.bat"));
@ -302,8 +305,10 @@ public class JavaClientCodegen extends AbstractJavaCodegen
supportingFiles.add(new SupportingFile("ProgressRequestBody.mustache", invokerFolder, "ProgressRequestBody.java")); supportingFiles.add(new SupportingFile("ProgressRequestBody.mustache", invokerFolder, "ProgressRequestBody.java"));
supportingFiles.add(new SupportingFile("ProgressResponseBody.mustache", invokerFolder, "ProgressResponseBody.java")); supportingFiles.add(new SupportingFile("ProgressResponseBody.mustache", invokerFolder, "ProgressResponseBody.java"));
supportingFiles.add(new SupportingFile("GzipRequestInterceptor.mustache", invokerFolder, "GzipRequestInterceptor.java")); supportingFiles.add(new SupportingFile("GzipRequestInterceptor.mustache", invokerFolder, "GzipRequestInterceptor.java"));
supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java"));
supportingFiles.add(new SupportingFile("auth/RetryingOAuth.mustache", authFolder, "RetryingOAuth.java")); // NOTE: below moved to postProcessOpoerationsWithModels
//supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java"));
//supportingFiles.add(new SupportingFile("auth/RetryingOAuth.mustache", authFolder, "RetryingOAuth.java"));
additionalProperties.put("gson", "true"); additionalProperties.put("gson", "true");
} else if (usesAnyRetrofitLibrary()) { } else if (usesAnyRetrofitLibrary()) {
supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java")); supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java"));
@ -484,6 +489,18 @@ public class JavaClientCodegen extends AbstractJavaCodegen
} }
} }
// for okhttp-gson (default), check to see if OAuth is defined and included OAuth-related files accordingly
if ((OKHTTP_GSON.equals(getLibrary()) || StringUtils.isEmpty(getLibrary())) && ProcessUtils.hasOAuthMethods(objs)) {
supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java"));
supportingFiles.add(new SupportingFile("auth/RetryingOAuth.mustache", authFolder, "RetryingOAuth.java"));
}
// google-api-client doesn't use the OpenAPI auth, because it uses Google Credential directly (HttpRequestInitializer)
if ((!(GOOGLE_API_CLIENT.equals(getLibrary()) || REST_ASSURED.equals(getLibrary()) || usePlayWS)) && ProcessUtils.hasOAuthMethods(objs)) {
supportingFiles.add(new SupportingFile("auth/OAuth.mustache", authFolder, "OAuth.java"));
supportingFiles.add(new SupportingFile("auth/OAuthFlow.mustache", authFolder, "OAuthFlow.java"));
}
return objs; return objs;
} }

View File

@ -1,7 +1,9 @@
package org.openapitools.codegen.utils; package org.openapitools.codegen.utils;
import org.openapitools.codegen.CodegenModel; import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenProperty; import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenSecurity;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -11,7 +13,7 @@ public class ProcessUtils {
/** /**
* Add x-index extension to the model's properties * Add x-index extension to the model's properties
* *
* @param models List of models * @param models List of models
*/ */
public static void addIndexToProperties(List<Object> models) { public static void addIndexToProperties(List<Object> models) {
for (Object _mo : models) { for (Object _mo : models) {
@ -34,4 +36,28 @@ public class ProcessUtils {
} }
/**
* Returns true if at least one operation has OAuth security schema defined
*
* @param objs Map of operations
* @return True if at least one operation has OAuth security schema defined
*/
public static boolean hasOAuthMethods(Map<String, Object> objs) {
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
if (operations != null) {
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
for (CodegenOperation operation : ops) {
if (operation.authMethods != null && !operation.authMethods.isEmpty()) {
for (CodegenSecurity cs : operation.authMethods) {
if (cs.isOAuth) {
return true;
}
}
}
}
}
return false;
}
} }

View File

@ -52,7 +52,9 @@ import java.text.DateFormat;
import {{invokerPackage}}.auth.Authentication; import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth; import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.ApiKeyAuth; import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth; import {{invokerPackage}}.auth.OAuth;
{{/hasOAuthMethods}}
{{>generatedAnnotation}} {{>generatedAnnotation}}
public class ApiClient { public class ApiClient {
@ -261,6 +263,7 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!"); throw new RuntimeException("No API key authentication configured!");
} }
{{#hasOAuthMethods}}
/** /**
* Helper method to set access token for the first OAuth2 authentication. * Helper method to set access token for the first OAuth2 authentication.
* @param accessToken Access token * @param accessToken Access token
@ -275,6 +278,7 @@ public class ApiClient {
throw new RuntimeException("No OAuth2 authentication configured!"); throw new RuntimeException("No OAuth2 authentication configured!");
} }
{{/hasOAuthMethods}}
/** /**
* Set the User-Agent header's value (by adding to the default header map). * Set the User-Agent header's value (by adding to the default header map).
* @param userAgent User agent * @param userAgent User agent

View File

@ -29,7 +29,9 @@ import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder; import feign.jackson.JacksonEncoder;
import feign.slf4j.Slf4jLogger; import feign.slf4j.Slf4jLogger;
import {{invokerPackage}}.auth.*; import {{invokerPackage}}.auth.*;
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth.AccessTokenListener; import {{invokerPackage}}.auth.OAuth.AccessTokenListener;
{{/hasOAuthMethods}}
{{>generatedAnnotation}} {{>generatedAnnotation}}
public class ApiClient { public class ApiClient {
@ -104,6 +106,7 @@ public class ApiClient {
this.setCredentials(username, password); this.setCredentials(username, password);
} }
{{#hasOAuthMethods}}
/** /**
* Helper constructor for single password oauth2 * Helper constructor for single password oauth2
* @param authName * @param authName
@ -112,15 +115,16 @@ public class ApiClient {
* @param username * @param username
* @param password * @param password
*/ */
public ApiClient(String authName, String clientId, String secret, String username, String password) { public ApiClient(String authName, String clientId, String secret, String username, String password) {
this(authName); this(authName);
this.getTokenEndPoint() this.getTokenEndPoint()
.setClientId(clientId) .setClientId(clientId)
.setClientSecret(secret) .setClientSecret(secret)
.setUsername(username) .setUsername(username)
.setPassword(password); .setPassword(password);
} }
{{/hasOAuthMethods}}
public String getBasePath() { public String getBasePath() {
return basePath; return basePath;
} }
@ -248,15 +252,18 @@ public class ApiClient {
basicAuth.setCredentials(username, password); basicAuth.setCredentials(username, password);
return; return;
} }
{{#hasOAuthMethods}}
if (apiAuthorization instanceof OAuth) { if (apiAuthorization instanceof OAuth) {
OAuth oauth = (OAuth) apiAuthorization; OAuth oauth = (OAuth) apiAuthorization;
oauth.getTokenRequestBuilder().setUsername(username).setPassword(password); oauth.getTokenRequestBuilder().setUsername(username).setPassword(password);
return; return;
} }
{{/hasOAuthMethods}}
} }
throw new RuntimeException("No Basic authentication or OAuth configured!"); throw new RuntimeException("No Basic authentication or OAuth configured!");
} }
{{#hasOAuthMethods}}
/** /**
* Helper method to configure the token endpoint of the first oauth found in the apiAuthorizations (there should be only one) * Helper method to configure the token endpoint of the first oauth found in the apiAuthorizations (there should be only one)
* @return Token request builder * @return Token request builder
@ -336,6 +343,7 @@ public class ApiClient {
} }
} }
{{/hasOAuthMethods}}
/** /**
* Gets request interceptor based on authentication name * Gets request interceptor based on authentication name
* @param authName Authentiation name * @param authName Authentiation name

View File

@ -54,7 +54,9 @@ import java.util.regex.Pattern;
import {{invokerPackage}}.auth.Authentication; import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth; import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.ApiKeyAuth; import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth; import {{invokerPackage}}.auth.OAuth;
{{/hasOAuthMethods}}
{{>generatedAnnotation}} {{>generatedAnnotation}}
public class ApiClient { public class ApiClient {
@ -190,6 +192,7 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!"); throw new RuntimeException("No API key authentication configured!");
} }
{{#hasOAuthMethods}}
/** /**
* Helper method to set access token for the first OAuth2 authentication. * Helper method to set access token for the first OAuth2 authentication.
* @param accessToken Access token * @param accessToken Access token
@ -204,6 +207,7 @@ public class ApiClient {
throw new RuntimeException("No OAuth2 authentication configured!"); throw new RuntimeException("No OAuth2 authentication configured!");
} }
{{/hasOAuthMethods}}
/** /**
* Set the User-Agent header's value (by adding to the default header map). * Set the User-Agent header's value (by adding to the default header map).
* @param userAgent Http user agent * @param userAgent Http user agent

View File

@ -42,7 +42,9 @@ import org.jboss.resteasy.spi.ResteasyProviderFactory;
import {{invokerPackage}}.auth.Authentication; import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth; import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.ApiKeyAuth; import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth; import {{invokerPackage}}.auth.OAuth;
{{/hasOAuthMethods}}
{{>generatedAnnotation}} {{>generatedAnnotation}}
public class ApiClient { public class ApiClient {
@ -200,6 +202,7 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!"); throw new RuntimeException("No API key authentication configured!");
} }
{{#hasOAuthMethods}}
/** /**
* Helper method to set access token for the first OAuth2 authentication. * Helper method to set access token for the first OAuth2 authentication.
* @param accessToken the access token * @param accessToken the access token
@ -214,6 +217,7 @@ public class ApiClient {
throw new RuntimeException("No OAuth2 authentication configured!"); throw new RuntimeException("No OAuth2 authentication configured!");
} }
{{/hasOAuthMethods}}
/** /**
* Set the User-Agent header's value (by adding to the default header map). * Set the User-Agent header's value (by adding to the default header map).
* @param userAgent the User-Agent header value * @param userAgent the User-Agent header value

View File

@ -67,7 +67,9 @@ import java.util.TimeZone;
import {{invokerPackage}}.auth.Authentication; import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth; import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.ApiKeyAuth; import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth; import {{invokerPackage}}.auth.OAuth;
{{/hasOAuthMethods}}
{{>generatedAnnotation}} {{>generatedAnnotation}}
@Component("{{invokerPackage}}.ApiClient") @Component("{{invokerPackage}}.ApiClient")
@ -239,6 +241,7 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!"); throw new RuntimeException("No API key authentication configured!");
} }
{{#hasOAuthMethods}}
/** /**
* Helper method to set access token for the first OAuth2 authentication. * Helper method to set access token for the first OAuth2 authentication.
* @param accessToken the access token * @param accessToken the access token
@ -253,6 +256,7 @@ public class ApiClient {
throw new RuntimeException("No OAuth2 authentication configured!"); throw new RuntimeException("No OAuth2 authentication configured!");
} }
{{/hasOAuthMethods}}
/** /**
* Set the User-Agent header's value (by adding to the default header map). * Set the User-Agent header's value (by adding to the default header map).
* @param userAgent the user agent string * @param userAgent the user agent string

View File

@ -34,10 +34,11 @@ import com.squareup.okhttp.OkHttpClient;
import {{invokerPackage}}.auth.HttpBasicAuth; import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.ApiKeyAuth; import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth; import {{invokerPackage}}.auth.OAuth;
import {{invokerPackage}}.auth.OAuth.AccessTokenListener; import {{invokerPackage}}.auth.OAuth.AccessTokenListener;
import {{invokerPackage}}.auth.OAuthFlow; import {{invokerPackage}}.auth.OAuthFlow;
{{/hasOAuthMethods}}
public class ApiClient { public class ApiClient {
@ -104,6 +105,7 @@ public class ApiClient {
this.setCredentials(username, password); this.setCredentials(username, password);
} }
{{#hasOAuthMethods}}
/** /**
* Helper constructor for single password oauth2 * Helper constructor for single password oauth2
* @param authName Authentication name * @param authName Authentication name
@ -121,20 +123,21 @@ public class ApiClient {
.setPassword(password); .setPassword(password);
} }
public void createDefaultAdapter() { {{/hasOAuthMethods}}
Gson gson = new GsonBuilder() public void createDefaultAdapter() {
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ") Gson gson = new GsonBuilder()
.registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter()) .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter()) .registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter())
.create(); .registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter())
.create();
okClient = new OkHttpClient(); okClient = new OkHttpClient();
adapterBuilder = new RestAdapter adapterBuilder = new RestAdapter
.Builder() .Builder()
.setEndpoint("{{{basePath}}}") .setEndpoint("{{{basePath}}}")
.setClient(new OkClient(okClient)) .setClient(new OkClient(okClient))
.setConverter(new GsonConverterWrapper(gson)); .setConverter(new GsonConverterWrapper(gson));
} }
public <S> S createService(Class<S> serviceClass) { public <S> S createService(Class<S> serviceClass) {
@ -168,14 +171,17 @@ public class ApiClient {
basicAuth.setCredentials(username, password); basicAuth.setCredentials(username, password);
return; return;
} }
{{#hasOAuthMethods}}
if (apiAuthorization instanceof OAuth) { if (apiAuthorization instanceof OAuth) {
OAuth oauth = (OAuth) apiAuthorization; OAuth oauth = (OAuth) apiAuthorization;
oauth.getTokenRequestBuilder().setUsername(username).setPassword(password); oauth.getTokenRequestBuilder().setUsername(username).setPassword(password);
return; return;
} }
{{/hasOAuthMethods}}
} }
} }
{{#hasOAuthMethods}}
/** /**
* Helper method to configure the token endpoint of the first oauth found in the apiAuthorizations (there should be only one) * Helper method to configure the token endpoint of the first oauth found in the apiAuthorizations (there should be only one)
* @return Token request builder * @return Token request builder
@ -253,6 +259,7 @@ public class ApiClient {
} }
} }
} }
{{/hasOAuthMethods}}
/** /**
* Adds an authorization to be used by the client * Adds an authorization to be used by the client

View File

@ -27,9 +27,11 @@ import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory; import retrofit2.converter.scalars.ScalarsConverterFactory;
import {{invokerPackage}}.auth.HttpBasicAuth; import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.ApiKeyAuth; import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth; import {{invokerPackage}}.auth.OAuth;
import {{invokerPackage}}.auth.OAuth.AccessTokenListener; import {{invokerPackage}}.auth.OAuth.AccessTokenListener;
import {{invokerPackage}}.auth.OAuthFlow; import {{invokerPackage}}.auth.OAuthFlow;
{{/hasOAuthMethods}}
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
@ -110,6 +112,7 @@ public class ApiClient {
this.setCredentials(username, password); this.setCredentials(username, password);
} }
{{#hasOAuthMethods}}
/** /**
* Helper constructor for single password oauth2 * Helper constructor for single password oauth2
* @param authName Authentication name * @param authName Authentication name
@ -127,6 +130,7 @@ public class ApiClient {
.setPassword(password); .setPassword(password);
} }
{{/hasOAuthMethods}}
public void createDefaultAdapter() { public void createDefaultAdapter() {
json = new JSON(); json = new JSON();
okBuilder = new OkHttpClient.Builder(); okBuilder = new OkHttpClient.Builder();
@ -218,15 +222,18 @@ public class ApiClient {
basicAuth.setCredentials(username, password); basicAuth.setCredentials(username, password);
return this; return this;
} }
{{#hasOAuthMethods}}
if (apiAuthorization instanceof OAuth) { if (apiAuthorization instanceof OAuth) {
OAuth oauth = (OAuth) apiAuthorization; OAuth oauth = (OAuth) apiAuthorization;
oauth.getTokenRequestBuilder().setUsername(username).setPassword(password); oauth.getTokenRequestBuilder().setUsername(username).setPassword(password);
return this; return this;
} }
{{/hasOAuthMethods}}
} }
return this; return this;
} }
{{#hasOAuthMethods}}
/** /**
* Helper method to configure the token endpoint of the first oauth found in the apiAuthorizations (there should be only one) * Helper method to configure the token endpoint of the first oauth found in the apiAuthorizations (there should be only one)
* @return Token request builder * @return Token request builder
@ -310,6 +317,7 @@ public class ApiClient {
} }
return this; return this;
} }
{{/hasOAuthMethods}}
/** /**
* Adds an authorization to be used by the client * Adds an authorization to be used by the client

View File

@ -3,7 +3,9 @@ package {{invokerPackage}};
import {{invokerPackage}}.auth.Authentication; import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth; import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.ApiKeyAuth; import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth; import {{invokerPackage}}.auth.OAuth;
{{/hasOAuthMethods}}
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
@ -219,6 +221,7 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!"); throw new RuntimeException("No API key authentication configured!");
} }
{{#hasOAuthMethods}}
/** /**
* Helper method to set access token for the first OAuth2 authentication. * Helper method to set access token for the first OAuth2 authentication.
* *
@ -234,6 +237,7 @@ public class ApiClient {
throw new RuntimeException("No OAuth2 authentication configured!"); throw new RuntimeException("No OAuth2 authentication configured!");
} }
{{/hasOAuthMethods}}
/** /**
* Format the given Date object into string. * Format the given Date object into string.
* *

View File

@ -60,7 +60,9 @@ import java.util.TimeZone;
import {{invokerPackage}}.auth.Authentication; import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth; import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.ApiKeyAuth; import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth; import {{invokerPackage}}.auth.OAuth;
{{/hasOAuthMethods}}
{{>generatedAnnotation}} {{>generatedAnnotation}}
public class ApiClient { public class ApiClient {
@ -236,6 +238,7 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!"); throw new RuntimeException("No API key authentication configured!");
} }
{{#hasOAuthMethods}}
/** /**
* Helper method to set access token for the first OAuth2 authentication. * Helper method to set access token for the first OAuth2 authentication.
* @param accessToken the access token * @param accessToken the access token
@ -250,6 +253,7 @@ public class ApiClient {
throw new RuntimeException("No OAuth2 authentication configured!"); throw new RuntimeException("No OAuth2 authentication configured!");
} }
{{/hasOAuthMethods}}
/** /**
* Set the User-Agent header's value (by adding to the default header map). * Set the User-Agent header's value (by adding to the default header map).
* @param userAgent the user agent string * @param userAgent the user agent string

View File

@ -268,7 +268,7 @@ public class JavaClientCodegenTest {
generator.opts(clientOptInput).generate(); generator.opts(clientOptInput).generate();
Map<String, String> generatedFiles = generator.getFiles(); Map<String, String> generatedFiles = generator.getFiles();
Assert.assertEquals(generatedFiles.size(), 37); Assert.assertEquals(generatedFiles.size(), 33);
ensureContainsFile(generatedFiles, output, ".gitignore"); ensureContainsFile(generatedFiles, output, ".gitignore");
ensureContainsFile(generatedFiles, output, ".openapi-generator-ignore"); ensureContainsFile(generatedFiles, output, ".openapi-generator-ignore");
ensureContainsFile(generatedFiles, output, ".openapi-generator/VERSION"); ensureContainsFile(generatedFiles, output, ".openapi-generator/VERSION");
@ -294,8 +294,8 @@ public class JavaClientCodegenTest {
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/ApiKeyAuth.java"); ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/ApiKeyAuth.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/Authentication.java"); ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/Authentication.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/HttpBasicAuth.java"); ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/HttpBasicAuth.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/OAuth.java"); //ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/OAuth.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/OAuthFlow.java"); //ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/auth/OAuthFlow.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/Configuration.java"); ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/Configuration.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/GzipRequestInterceptor.java"); ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/GzipRequestInterceptor.java");
ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/JSON.java"); ensureContainsFile(generatedFiles, output, "src/main/java/xyz/abcdef/JSON.java");

View File

@ -95,14 +95,14 @@ public class ApiClient {
* @param username * @param username
* @param password * @param password
*/ */
public ApiClient(String authName, String clientId, String secret, String username, String password) { public ApiClient(String authName, String clientId, String secret, String username, String password) {
this(authName); this(authName);
this.getTokenEndPoint() this.getTokenEndPoint()
.setClientId(clientId) .setClientId(clientId)
.setClientSecret(secret) .setClientSecret(secret)
.setUsername(username) .setUsername(username)
.setPassword(password); .setPassword(password);
} }
public String getBasePath() { public String getBasePath() {
return basePath; return basePath;

View File

@ -95,14 +95,14 @@ public class ApiClient {
* @param username * @param username
* @param password * @param password
*/ */
public ApiClient(String authName, String clientId, String secret, String username, String password) { public ApiClient(String authName, String clientId, String secret, String username, String password) {
this(authName); this(authName);
this.getTokenEndPoint() this.getTokenEndPoint()
.setClientId(clientId) .setClientId(clientId)
.setClientSecret(secret) .setClientSecret(secret)
.setUsername(username) .setUsername(username)
.setPassword(password); .setPassword(password);
} }
public String getBasePath() { public String getBasePath() {
return basePath; return basePath;

View File

@ -38,7 +38,6 @@ import org.openapitools.client.auth.OAuth;
import org.openapitools.client.auth.OAuth.AccessTokenListener; import org.openapitools.client.auth.OAuth.AccessTokenListener;
import org.openapitools.client.auth.OAuthFlow; import org.openapitools.client.auth.OAuthFlow;
public class ApiClient { public class ApiClient {
private Map<String, Interceptor> apiAuthorizations; private Map<String, Interceptor> apiAuthorizations;
@ -117,20 +116,20 @@ public class ApiClient {
.setPassword(password); .setPassword(password);
} }
public void createDefaultAdapter() { public void createDefaultAdapter() {
Gson gson = new GsonBuilder() Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ") .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
.registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter()) .registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter())
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter()) .registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter())
.create(); .create();
okClient = new OkHttpClient(); okClient = new OkHttpClient();
adapterBuilder = new RestAdapter adapterBuilder = new RestAdapter
.Builder() .Builder()
.setEndpoint("http://petstore.swagger.io:80/v2") .setEndpoint("http://petstore.swagger.io:80/v2")
.setClient(new OkClient(okClient)) .setClient(new OkClient(okClient))
.setConverter(new GsonConverterWrapper(gson)); .setConverter(new GsonConverterWrapper(gson));
} }
public <S> S createService(Class<S> serviceClass) { public <S> S createService(Class<S> serviceClass) {