Skip to content
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 @@ -31,6 +31,7 @@
import org.openapitools.codegen.languages.features.GzipFeatures;
import org.openapitools.codegen.languages.features.PerformBeanValidationFeatures;
import org.openapitools.codegen.mustache.CaseFormatLambda;
import org.openapitools.codegen.utils.ProcessUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -101,6 +102,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
protected boolean useGzipFeature = false;
protected boolean useRuntimeException = false;
protected boolean useReflectionEqualsHashCode = false;
protected String authFolder;

public JavaClientCodegen() {
super();
Expand Down Expand Up @@ -236,8 +238,8 @@ public void processOpts() {
}

final String invokerFolder = (sourceFolder + '/' + invokerPackage).replace(".", "/");
final String authFolder = (sourceFolder + '/' + invokerPackage + ".auth").replace(".", "/");
final String apiFolder = (sourceFolder + '/' + apiPackage).replace(".", "/");
authFolder = (sourceFolder + '/' + invokerPackage + ".auth").replace(".", "/");

//Common files
writeOptional(outputFolder, new SupportingFile("pom.mustache", "", "pom.xml"));
Expand All @@ -253,12 +255,13 @@ public void processOpts() {
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()))) {
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/OAuth.mustache", authFolder, "OAuth.java"));
supportingFiles.add(new SupportingFile("auth/OAuthFlow.mustache", authFolder, "OAuthFlow.java"));
// NOTE: below moved to postProcessOperationsWithModels
//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.bat.mustache", "", "gradlew.bat"));
Expand Down Expand Up @@ -302,8 +305,10 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("ProgressRequestBody.mustache", invokerFolder, "ProgressRequestBody.java"));
supportingFiles.add(new SupportingFile("ProgressResponseBody.mustache", invokerFolder, "ProgressResponseBody.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");
} else if (usesAnyRetrofitLibrary()) {
supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java"));
Expand Down Expand Up @@ -484,6 +489,18 @@ public int compare(CodegenParameter one, CodegenParameter another) {
}
}

// 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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.openapitools.codegen.utils;

import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenOperation;
import org.openapitools.codegen.CodegenProperty;
import org.openapitools.codegen.CodegenSecurity;

import java.util.List;
import java.util.Map;
Expand All @@ -11,7 +13,7 @@ public class ProcessUtils {
/**
* 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) {
for (Object _mo : models) {
Expand All @@ -34,4 +36,28 @@ public static void addIndexToProperties(List<Object> models) {

}

/**
* 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ import java.text.DateFormat;
import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth;
{{/hasOAuthMethods}}

{{>generatedAnnotation}}
public class ApiClient {
Expand Down Expand Up @@ -261,6 +263,7 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!");
}

{{#hasOAuthMethods}}
/**
* Helper method to set access token for the first OAuth2 authentication.
* @param accessToken Access token
Expand All @@ -275,6 +278,7 @@ public class ApiClient {
throw new RuntimeException("No OAuth2 authentication configured!");
}

{{/hasOAuthMethods}}
/**
* Set the User-Agent header's value (by adding to the default header map).
* @param userAgent User agent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import feign.slf4j.Slf4jLogger;
import {{invokerPackage}}.auth.*;
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth.AccessTokenListener;
{{/hasOAuthMethods}}

{{>generatedAnnotation}}
public class ApiClient {
Expand Down Expand Up @@ -104,6 +106,7 @@ public class ApiClient {
this.setCredentials(username, password);
}

{{#hasOAuthMethods}}
/**
* Helper constructor for single password oauth2
* @param authName
Expand All @@ -112,15 +115,16 @@ public class ApiClient {
* @param username
* @param password
*/
public ApiClient(String authName, String clientId, String secret, String username, String password) {
this(authName);
this.getTokenEndPoint()
.setClientId(clientId)
.setClientSecret(secret)
.setUsername(username)
.setPassword(password);
}
public ApiClient(String authName, String clientId, String secret, String username, String password) {
this(authName);
this.getTokenEndPoint()
.setClientId(clientId)
.setClientSecret(secret)
.setUsername(username)
.setPassword(password);
}

{{/hasOAuthMethods}}
public String getBasePath() {
return basePath;
}
Expand Down Expand Up @@ -248,15 +252,18 @@ public class ApiClient {
basicAuth.setCredentials(username, password);
return;
}
{{#hasOAuthMethods}}
if (apiAuthorization instanceof OAuth) {
OAuth oauth = (OAuth) apiAuthorization;
oauth.getTokenRequestBuilder().setUsername(username).setPassword(password);
return;
}
{{/hasOAuthMethods}}
}
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)
* @return Token request builder
Expand Down Expand Up @@ -336,6 +343,7 @@ public class ApiClient {
}
}

{{/hasOAuthMethods}}
/**
* Gets request interceptor based on authentication name
* @param authName Authentiation name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ import java.util.regex.Pattern;
import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth;
{{/hasOAuthMethods}}

{{>generatedAnnotation}}
public class ApiClient {
Expand Down Expand Up @@ -190,6 +192,7 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!");
}

{{#hasOAuthMethods}}
/**
* Helper method to set access token for the first OAuth2 authentication.
* @param accessToken Access token
Expand All @@ -204,6 +207,7 @@ public class ApiClient {
throw new RuntimeException("No OAuth2 authentication configured!");
}

{{/hasOAuthMethods}}
/**
* Set the User-Agent header's value (by adding to the default header map).
* @param userAgent Http user agent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ import org.jboss.resteasy.spi.ResteasyProviderFactory;
import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth;
{{/hasOAuthMethods}}

{{>generatedAnnotation}}
public class ApiClient {
Expand Down Expand Up @@ -200,6 +202,7 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!");
}

{{#hasOAuthMethods}}
/**
* Helper method to set access token for the first OAuth2 authentication.
* @param accessToken the access token
Expand All @@ -214,6 +217,7 @@ public class ApiClient {
throw new RuntimeException("No OAuth2 authentication configured!");
}

{{/hasOAuthMethods}}
/**
* Set the User-Agent header's value (by adding to the default header map).
* @param userAgent the User-Agent header value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ import java.util.TimeZone;
import {{invokerPackage}}.auth.Authentication;
import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth;
{{/hasOAuthMethods}}

{{>generatedAnnotation}}
@Component("{{invokerPackage}}.ApiClient")
Expand Down Expand Up @@ -239,6 +241,7 @@ public class ApiClient {
throw new RuntimeException("No API key authentication configured!");
}

{{#hasOAuthMethods}}
/**
* Helper method to set access token for the first OAuth2 authentication.
* @param accessToken the access token
Expand All @@ -253,6 +256,7 @@ public class ApiClient {
throw new RuntimeException("No OAuth2 authentication configured!");
}

{{/hasOAuthMethods}}
/**
* Set the User-Agent header's value (by adding to the default header map).
* @param userAgent the user agent string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ import com.squareup.okhttp.OkHttpClient;

import {{invokerPackage}}.auth.HttpBasicAuth;
import {{invokerPackage}}.auth.ApiKeyAuth;
{{#hasOAuthMethods}}
import {{invokerPackage}}.auth.OAuth;
import {{invokerPackage}}.auth.OAuth.AccessTokenListener;
import {{invokerPackage}}.auth.OAuthFlow;

{{/hasOAuthMethods}}

public class ApiClient {

Expand Down Expand Up @@ -104,6 +105,7 @@ public class ApiClient {
this.setCredentials(username, password);
}

{{#hasOAuthMethods}}
/**
* Helper constructor for single password oauth2
* @param authName Authentication name
Expand All @@ -120,21 +122,22 @@ public class ApiClient {
.setUsername(username)
.setPassword(password);
}

public void createDefaultAdapter() {
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
.registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter())
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter())
.create();

okClient = new OkHttpClient();

adapterBuilder = new RestAdapter
.Builder()
.setEndpoint("{{{basePath}}}")
.setClient(new OkClient(okClient))
.setConverter(new GsonConverterWrapper(gson));

{{/hasOAuthMethods}}
public void createDefaultAdapter() {
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
.registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter())
.registerTypeAdapter(LocalDate.class, new LocalDateTypeAdapter())
.create();

okClient = new OkHttpClient();

adapterBuilder = new RestAdapter
.Builder()
.setEndpoint("{{{basePath}}}")
.setClient(new OkClient(okClient))
.setConverter(new GsonConverterWrapper(gson));
}

public <S> S createService(Class<S> serviceClass) {
Expand Down Expand Up @@ -168,14 +171,17 @@ public class ApiClient {
basicAuth.setCredentials(username, password);
return;
}
{{#hasOAuthMethods}}
if (apiAuthorization instanceof OAuth) {
OAuth oauth = (OAuth) apiAuthorization;
oauth.getTokenRequestBuilder().setUsername(username).setPassword(password);
return;
}
{{/hasOAuthMethods}}
}
}

{{#hasOAuthMethods}}
/**
* Helper method to configure the token endpoint of the first oauth found in the apiAuthorizations (there should be only one)
* @return Token request builder
Expand Down Expand Up @@ -253,6 +259,7 @@ public class ApiClient {
}
}
}
{{/hasOAuthMethods}}

/**
* Adds an authorization to be used by the client
Expand Down
Loading