Skip to content

Commit c95bc4d

Browse files
authored
[Java] Fix repeated OAuth files being generated (#6285)
* fix repeated oauth files being generated * fix oauth check
1 parent 9c4b372 commit c95bc4d

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ public class JavaClientCodegen extends AbstractJavaCodegen
106106
protected String authFolder;
107107
protected String serializationLibrary = null;
108108

109+
protected boolean addOAuthSupportingFiles = false;
110+
protected boolean addOAuthRetrySupportingFiles = false;
111+
109112
public JavaClientCodegen() {
110113
super();
111114

@@ -368,7 +371,7 @@ public void processOpts() {
368371
supportingFiles.add(new SupportingFile("ProgressResponseBody.mustache", invokerFolder, "ProgressResponseBody.java"));
369372
supportingFiles.add(new SupportingFile("GzipRequestInterceptor.mustache", invokerFolder, "GzipRequestInterceptor.java"));
370373

371-
// NOTE: below moved to postProcessOpoerationsWithModels
374+
// NOTE: below moved to postProcessOperationsWithModels
372375
//supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java"));
373376
//supportingFiles.add(new SupportingFile("auth/RetryingOAuth.mustache", authFolder, "RetryingOAuth.java"));
374377
forceSerializationLibrary(SERIALIZATION_LIBRARY_GSON);
@@ -588,16 +591,22 @@ public int compare(CodegenParameter one, CodegenParameter another) {
588591
}
589592
}
590593

591-
// for okhttp-gson (default), check to see if OAuth is defined and included OAuth-related files accordingly
592-
if ((OKHTTP_GSON.equals(getLibrary()) || StringUtils.isEmpty(getLibrary())) && ProcessUtils.hasOAuthMethods(objs)) {
593-
supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java"));
594-
supportingFiles.add(new SupportingFile("auth/RetryingOAuth.mustache", authFolder, "RetryingOAuth.java"));
595-
}
594+
// has OAuth defined
595+
if (ProcessUtils.hasOAuthMethods(objs)) {
596+
// for okhttp-gson (default), check to see if OAuth is defined and included OAuth-related files accordingly
597+
if ((OKHTTP_GSON.equals(getLibrary()) || StringUtils.isEmpty(getLibrary())) && !addOAuthRetrySupportingFiles) {
598+
supportingFiles.add(new SupportingFile("auth/OAuthOkHttpClient.mustache", authFolder, "OAuthOkHttpClient.java"));
599+
supportingFiles.add(new SupportingFile("auth/RetryingOAuth.mustache", authFolder, "RetryingOAuth.java"));
600+
addOAuthRetrySupportingFiles = true; // add only once
601+
}
596602

597-
// google-api-client doesn't use the OpenAPI auth, because it uses Google Credential directly (HttpRequestInitializer)
598-
if ((!(GOOGLE_API_CLIENT.equals(getLibrary()) || REST_ASSURED.equals(getLibrary()) || usePlayWS || NATIVE.equals(getLibrary()) || MICROPROFILE.equals(getLibrary()))) && ProcessUtils.hasOAuthMethods(objs)) {
599-
supportingFiles.add(new SupportingFile("auth/OAuth.mustache", authFolder, "OAuth.java"));
600-
supportingFiles.add(new SupportingFile("auth/OAuthFlow.mustache", authFolder, "OAuthFlow.java"));
603+
// google-api-client doesn't use the OpenAPI auth, because it uses Google Credential directly (HttpRequestInitializer)
604+
if (!(GOOGLE_API_CLIENT.equals(getLibrary()) || REST_ASSURED.equals(getLibrary()) || usePlayWS
605+
|| NATIVE.equals(getLibrary()) || MICROPROFILE.equals(getLibrary())) && !addOAuthSupportingFiles) {
606+
supportingFiles.add(new SupportingFile("auth/OAuth.mustache", authFolder, "OAuth.java"));
607+
supportingFiles.add(new SupportingFile("auth/OAuthFlow.mustache", authFolder, "OAuthFlow.java"));
608+
addOAuthSupportingFiles = true; // add only once
609+
}
601610
}
602611

603612
if (MICROPROFILE.equals(getLibrary())) {

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ProcessUtils.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010

1111
public class ProcessUtils {
1212

13+
private static Boolean hasOAuthMethods;
14+
1315
/**
1416
* Add x-index extension to the model's properties
1517
*
16-
* @param models List of models
18+
* @param models List of models
1719
* @param initialIndex starting index to use
1820
*/
1921
public static void addIndexToProperties(List<Object> models, int initialIndex) {
@@ -66,7 +68,6 @@ public static boolean hasOAuthMethods(Map<String, Object> objs) {
6668
}
6769
}
6870
}
69-
7071
return false;
7172
}
7273

@@ -97,34 +98,34 @@ public static boolean hasBearerMethods(Map<String, Object> objs) {
9798
/**
9899
* Returns true if the specified OAS model has at least one operation with the HTTP basic
99100
* security scheme.
100-
*
101+
*
101102
* @param authMethods List of auth methods.
102103
* @return True if at least one operation has HTTP basic security scheme defined
103104
*/
104105
public static boolean hasHttpBasicMethods(List<CodegenSecurity> authMethods) {
105106
if (authMethods != null && !authMethods.isEmpty()) {
106-
for (CodegenSecurity cs : authMethods) {
107-
if (Boolean.TRUE.equals(cs.isBasicBasic)) {
108-
return true;
109-
}
110-
}
107+
for (CodegenSecurity cs : authMethods) {
108+
if (Boolean.TRUE.equals(cs.isBasicBasic)) {
109+
return true;
110+
}
111+
}
111112
}
112113
return false;
113114
}
114115

115116
/**
116117
* Returns true if the specified OAS model has at least one operation with API keys.
117-
*
118+
*
118119
* @param authMethods List of auth methods.
119120
* @return True if at least one operation has API key security scheme defined
120121
*/
121122
public static boolean hasApiKeyMethods(List<CodegenSecurity> authMethods) {
122123
if (authMethods != null && !authMethods.isEmpty()) {
123-
for (CodegenSecurity cs : authMethods) {
124-
if (Boolean.TRUE.equals(cs.isApiKey)) {
125-
return true;
126-
}
127-
}
124+
for (CodegenSecurity cs : authMethods) {
125+
if (Boolean.TRUE.equals(cs.isApiKey)) {
126+
return true;
127+
}
128+
}
128129
}
129130
return false;
130131
}
@@ -133,17 +134,17 @@ public static boolean hasApiKeyMethods(List<CodegenSecurity> authMethods) {
133134
* Returns true if the specified OAS model has at least one operation with the HTTP signature
134135
* security scheme.
135136
* The HTTP signature scheme is defined in https://datatracker.ietf.org/doc/draft-cavage-http-signatures/
136-
*
137+
*
137138
* @param authMethods List of auth methods.
138139
* @return True if at least one operation has HTTP signature security scheme defined
139140
*/
140141
public static boolean hasHttpSignatureMethods(List<CodegenSecurity> authMethods) {
141142
if (authMethods != null && !authMethods.isEmpty()) {
142-
for (CodegenSecurity cs : authMethods) {
143-
if (Boolean.TRUE.equals(cs.isHttpSignature)) {
144-
return true;
145-
}
146-
}
143+
for (CodegenSecurity cs : authMethods) {
144+
if (Boolean.TRUE.equals(cs.isHttpSignature)) {
145+
return true;
146+
}
147+
}
147148
}
148149
return false;
149150
}

0 commit comments

Comments
 (0)