[Java] Fix repeated OAuth files being generated (#6285)

* fix repeated oauth files being generated

* fix oauth check
This commit is contained in:
William Cheng 2020-05-14 17:23:54 +08:00 committed by GitHub
parent 9c4b37299a
commit c95bc4dfb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 30 deletions

View File

@ -106,6 +106,9 @@ public class JavaClientCodegen extends AbstractJavaCodegen
protected String authFolder;
protected String serializationLibrary = null;
protected boolean addOAuthSupportingFiles = false;
protected boolean addOAuthRetrySupportingFiles = false;
public JavaClientCodegen() {
super();
@ -368,7 +371,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
supportingFiles.add(new SupportingFile("ProgressResponseBody.mustache", invokerFolder, "ProgressResponseBody.java"));
supportingFiles.add(new SupportingFile("GzipRequestInterceptor.mustache", invokerFolder, "GzipRequestInterceptor.java"));
// NOTE: below moved to postProcessOpoerationsWithModels
// NOTE: below moved to postProcessOperationsWithModels
//supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java"));
//supportingFiles.add(new SupportingFile("auth/RetryingOAuth.mustache", authFolder, "RetryingOAuth.java"));
forceSerializationLibrary(SERIALIZATION_LIBRARY_GSON);
@ -588,16 +591,22 @@ 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"));
}
// has OAuth defined
if (ProcessUtils.hasOAuthMethods(objs)) {
// 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())) && !addOAuthRetrySupportingFiles) {
supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java"));
supportingFiles.add(new SupportingFile("auth/RetryingOAuth.mustache", authFolder, "RetryingOAuth.java"));
addOAuthRetrySupportingFiles = true; // add only once
}
// 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 || NATIVE.equals(getLibrary()) || MICROPROFILE.equals(getLibrary()))) && ProcessUtils.hasOAuthMethods(objs)) {
supportingFiles.add(new SupportingFile("auth/OAuth.mustache", authFolder, "OAuth.java"));
supportingFiles.add(new SupportingFile("auth/OAuthFlow.mustache", authFolder, "OAuthFlow.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
|| NATIVE.equals(getLibrary()) || MICROPROFILE.equals(getLibrary())) && !addOAuthSupportingFiles) {
supportingFiles.add(new SupportingFile("auth/OAuth.mustache", authFolder, "OAuth.java"));
supportingFiles.add(new SupportingFile("auth/OAuthFlow.mustache", authFolder, "OAuthFlow.java"));
addOAuthSupportingFiles = true; // add only once
}
}
if (MICROPROFILE.equals(getLibrary())) {

View File

@ -10,10 +10,12 @@ import java.util.Map;
public class ProcessUtils {
private static Boolean hasOAuthMethods;
/**
* Add x-index extension to the model's properties
*
* @param models List of models
* @param models List of models
* @param initialIndex starting index to use
*/
public static void addIndexToProperties(List<Object> models, int initialIndex) {
@ -66,7 +68,6 @@ public class ProcessUtils {
}
}
}
return false;
}
@ -97,34 +98,34 @@ public class ProcessUtils {
/**
* Returns true if the specified OAS model has at least one operation with the HTTP basic
* security scheme.
*
*
* @param authMethods List of auth methods.
* @return True if at least one operation has HTTP basic security scheme defined
*/
public static boolean hasHttpBasicMethods(List<CodegenSecurity> authMethods) {
if (authMethods != null && !authMethods.isEmpty()) {
for (CodegenSecurity cs : authMethods) {
if (Boolean.TRUE.equals(cs.isBasicBasic)) {
return true;
}
}
for (CodegenSecurity cs : authMethods) {
if (Boolean.TRUE.equals(cs.isBasicBasic)) {
return true;
}
}
}
return false;
}
/**
* Returns true if the specified OAS model has at least one operation with API keys.
*
*
* @param authMethods List of auth methods.
* @return True if at least one operation has API key security scheme defined
*/
public static boolean hasApiKeyMethods(List<CodegenSecurity> authMethods) {
if (authMethods != null && !authMethods.isEmpty()) {
for (CodegenSecurity cs : authMethods) {
if (Boolean.TRUE.equals(cs.isApiKey)) {
return true;
}
}
for (CodegenSecurity cs : authMethods) {
if (Boolean.TRUE.equals(cs.isApiKey)) {
return true;
}
}
}
return false;
}
@ -133,17 +134,17 @@ public class ProcessUtils {
* Returns true if the specified OAS model has at least one operation with the HTTP signature
* security scheme.
* The HTTP signature scheme is defined in https://datatracker.ietf.org/doc/draft-cavage-http-signatures/
*
*
* @param authMethods List of auth methods.
* @return True if at least one operation has HTTP signature security scheme defined
*/
public static boolean hasHttpSignatureMethods(List<CodegenSecurity> authMethods) {
if (authMethods != null && !authMethods.isEmpty()) {
for (CodegenSecurity cs : authMethods) {
if (Boolean.TRUE.equals(cs.isHttpSignature)) {
return true;
}
}
for (CodegenSecurity cs : authMethods) {
if (Boolean.TRUE.equals(cs.isHttpSignature)) {
return true;
}
}
}
return false;
}