From c95bc4dfb00cb7048721f5dc726d0b129cc93752 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Thu, 14 May 2020 17:23:54 +0800 Subject: [PATCH] [Java] Fix repeated OAuth files being generated (#6285) * fix repeated oauth files being generated * fix oauth check --- .../codegen/languages/JavaClientCodegen.java | 29 ++++++++----- .../codegen/utils/ProcessUtils.java | 41 ++++++++++--------- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java index d524849dd9a..2c305b30cd8 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java @@ -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())) { diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ProcessUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ProcessUtils.java index 91b7b674d66..750a7f9d9c1 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ProcessUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ProcessUtils.java @@ -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 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 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 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 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; }