[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 authFolder;
protected String serializationLibrary = null; protected String serializationLibrary = null;
protected boolean addOAuthSupportingFiles = false;
protected boolean addOAuthRetrySupportingFiles = false;
public JavaClientCodegen() { public JavaClientCodegen() {
super(); super();
@ -368,7 +371,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
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"));
// 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/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java"));
//supportingFiles.add(new SupportingFile("auth/RetryingOAuth.mustache", authFolder, "RetryingOAuth.java")); //supportingFiles.add(new SupportingFile("auth/RetryingOAuth.mustache", authFolder, "RetryingOAuth.java"));
forceSerializationLibrary(SERIALIZATION_LIBRARY_GSON); 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 // has OAuth defined
if ((OKHTTP_GSON.equals(getLibrary()) || StringUtils.isEmpty(getLibrary())) && ProcessUtils.hasOAuthMethods(objs)) { if (ProcessUtils.hasOAuthMethods(objs)) {
supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java")); // for okhttp-gson (default), check to see if OAuth is defined and included OAuth-related files accordingly
supportingFiles.add(new SupportingFile("auth/RetryingOAuth.mustache", authFolder, "RetryingOAuth.java")); 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) // 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)) { if (!(GOOGLE_API_CLIENT.equals(getLibrary()) || REST_ASSURED.equals(getLibrary()) || usePlayWS
supportingFiles.add(new SupportingFile("auth/OAuth.mustache", authFolder, "OAuth.java")); || NATIVE.equals(getLibrary()) || MICROPROFILE.equals(getLibrary())) && !addOAuthSupportingFiles) {
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"));
addOAuthSupportingFiles = true; // add only once
}
} }
if (MICROPROFILE.equals(getLibrary())) { if (MICROPROFILE.equals(getLibrary())) {

View File

@ -10,10 +10,12 @@ import java.util.Map;
public class ProcessUtils { public class ProcessUtils {
private static Boolean hasOAuthMethods;
/** /**
* 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
* @param initialIndex starting index to use * @param initialIndex starting index to use
*/ */
public static void addIndexToProperties(List<Object> models, int initialIndex) { public static void addIndexToProperties(List<Object> models, int initialIndex) {
@ -66,7 +68,6 @@ public class ProcessUtils {
} }
} }
} }
return false; return false;
} }
@ -103,11 +104,11 @@ public class ProcessUtils {
*/ */
public static boolean hasHttpBasicMethods(List<CodegenSecurity> authMethods) { public static boolean hasHttpBasicMethods(List<CodegenSecurity> authMethods) {
if (authMethods != null && !authMethods.isEmpty()) { if (authMethods != null && !authMethods.isEmpty()) {
for (CodegenSecurity cs : authMethods) { for (CodegenSecurity cs : authMethods) {
if (Boolean.TRUE.equals(cs.isBasicBasic)) { if (Boolean.TRUE.equals(cs.isBasicBasic)) {
return true; return true;
} }
} }
} }
return false; return false;
} }
@ -120,11 +121,11 @@ public class ProcessUtils {
*/ */
public static boolean hasApiKeyMethods(List<CodegenSecurity> authMethods) { public static boolean hasApiKeyMethods(List<CodegenSecurity> authMethods) {
if (authMethods != null && !authMethods.isEmpty()) { if (authMethods != null && !authMethods.isEmpty()) {
for (CodegenSecurity cs : authMethods) { for (CodegenSecurity cs : authMethods) {
if (Boolean.TRUE.equals(cs.isApiKey)) { if (Boolean.TRUE.equals(cs.isApiKey)) {
return true; return true;
} }
} }
} }
return false; return false;
} }
@ -139,11 +140,11 @@ public class ProcessUtils {
*/ */
public static boolean hasHttpSignatureMethods(List<CodegenSecurity> authMethods) { public static boolean hasHttpSignatureMethods(List<CodegenSecurity> authMethods) {
if (authMethods != null && !authMethods.isEmpty()) { if (authMethods != null && !authMethods.isEmpty()) {
for (CodegenSecurity cs : authMethods) { for (CodegenSecurity cs : authMethods) {
if (Boolean.TRUE.equals(cs.isHttpSignature)) { if (Boolean.TRUE.equals(cs.isHttpSignature)) {
return true; return true;
} }
} }
} }
return false; return false;
} }