5
5
import io .swagger .codegen .CodegenConfig ;
6
6
import io .swagger .codegen .CodegenConstants ;
7
7
import io .swagger .codegen .CodegenModel ;
8
+ import io .swagger .codegen .CodegenOperation ;
8
9
import io .swagger .codegen .CodegenProperty ;
9
10
import io .swagger .codegen .CodegenType ;
10
11
import io .swagger .codegen .DefaultCodegen ;
@@ -84,6 +85,7 @@ public JavaClientCodegen() {
84
85
supportedLibraries .put ("<default>" , "HTTP client: Jersey client 1.18. JSON processing: Jackson 2.4.2" );
85
86
supportedLibraries .put ("jersey2" , "HTTP client: Jersey client 2.6" );
86
87
supportedLibraries .put ("okhttp-gson" , "HTTP client: OkHttp 2.4.0. JSON processing: Gson 2.3.1" );
88
+ supportedLibraries .put ("retrofit" , "HTTP client: OkHttp 2.4.0. JSON processing: Gson 2.3.1 (Retrofit 1.9.0)" );
87
89
cliOptions .add (buildLibraryCliOption (supportedLibraries ));
88
90
}
89
91
@@ -155,12 +157,22 @@ public void processOpts() {
155
157
final String invokerFolder = (sourceFolder + File .separator + invokerPackage ).replace ("." , File .separator );
156
158
supportingFiles .add (new SupportingFile ("pom.mustache" , "" , "pom.xml" ));
157
159
supportingFiles .add (new SupportingFile ("ApiClient.mustache" , invokerFolder , "ApiClient.java" ));
158
- supportingFiles .add (new SupportingFile ("apiException.mustache" , invokerFolder , "ApiException.java" ));
159
- supportingFiles .add (new SupportingFile ("Configuration.mustache" , invokerFolder , "Configuration.java" ));
160
- supportingFiles .add (new SupportingFile ("JSON.mustache" , invokerFolder , "JSON.java" ));
161
- supportingFiles .add (new SupportingFile ("Pair.mustache" , invokerFolder , "Pair.java" ));
162
160
supportingFiles .add (new SupportingFile ("StringUtil.mustache" , invokerFolder , "StringUtil.java" ));
163
-
161
+
162
+ final String authFolder = (sourceFolder + File .separator + invokerPackage + ".auth" ).replace ("." , File .separator );
163
+ supportingFiles .add (new SupportingFile ("auth/HttpBasicAuth.mustache" , authFolder , "HttpBasicAuth.java" ));
164
+ supportingFiles .add (new SupportingFile ("auth/ApiKeyAuth.mustache" , authFolder , "ApiKeyAuth.java" ));
165
+ supportingFiles .add (new SupportingFile ("auth/OAuth.mustache" , authFolder , "OAuth.java" ));
166
+ supportingFiles .add (new SupportingFile ("auth/OAuthFlow.mustache" , authFolder , "OAuthFlow.java" ));
167
+
168
+ if (!"retrofit" .equals (getLibrary ())) {
169
+ supportingFiles .add (new SupportingFile ("apiException.mustache" , invokerFolder , "ApiException.java" ));
170
+ supportingFiles .add (new SupportingFile ("Configuration.mustache" , invokerFolder , "Configuration.java" ));
171
+ supportingFiles .add (new SupportingFile ("JSON.mustache" , invokerFolder , "JSON.java" ));
172
+ supportingFiles .add (new SupportingFile ("Pair.mustache" , invokerFolder , "Pair.java" ));
173
+ supportingFiles .add (new SupportingFile ("auth/Authentication.mustache" , authFolder , "Authentication.java" ));
174
+ }
175
+
164
176
// library-specific files
165
177
if ("okhttp-gson" .equals (getLibrary ())) {
166
178
// the "okhttp-gson" library template requires "ApiCallback.mustache" for async call
@@ -169,16 +181,11 @@ public void processOpts() {
169
181
supportingFiles .add (new SupportingFile ("build.gradle.mustache" , "" , "build.gradle" ));
170
182
// "build.sbt" is for development with SBT
171
183
supportingFiles .add (new SupportingFile ("build.sbt.mustache" , "" , "build.sbt" ));
172
- // and does not require "TypeRef.mustache"
184
+ } else if ("retrofit" .equals (getLibrary ())) {
185
+ supportingFiles .add (new SupportingFile ("auth/OAuthOkHttpClient.mustache" , authFolder , "OAuthOkHttpClient.java" ));
173
186
} else {
174
187
supportingFiles .add (new SupportingFile ("TypeRef.mustache" , invokerFolder , "TypeRef.java" ));
175
188
}
176
-
177
- final String authFolder = (sourceFolder + File .separator + invokerPackage + ".auth" ).replace ("." , File .separator );
178
- supportingFiles .add (new SupportingFile ("auth/Authentication.mustache" , authFolder , "Authentication.java" ));
179
- supportingFiles .add (new SupportingFile ("auth/HttpBasicAuth.mustache" , authFolder , "HttpBasicAuth.java" ));
180
- supportingFiles .add (new SupportingFile ("auth/ApiKeyAuth.mustache" , authFolder , "ApiKeyAuth.java" ));
181
- supportingFiles .add (new SupportingFile ("auth/OAuth.mustache" , authFolder , "OAuth.java" ));
182
189
}
183
190
184
191
private void sanitizeConfig () {
@@ -373,6 +380,29 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
373
380
}
374
381
return objs ;
375
382
}
383
+
384
+ public Map <String , Object > postProcessOperations (Map <String , Object > objs ) {
385
+ if ("retrofit" .equals (getLibrary ())) {
386
+ Map <String , Object > operations = (Map <String , Object >) objs .get ("operations" );
387
+ if (operations != null ) {
388
+ List <CodegenOperation > ops = (List <CodegenOperation >) operations .get ("operation" );
389
+ for (CodegenOperation operation : ops ) {
390
+ if (operation .hasConsumes == Boolean .TRUE ) {
391
+ Map <String , String > firstType = operation .consumes .get (0 );
392
+ if (firstType != null ) {
393
+ if ("multipart/form-data" .equals (firstType .get ("mediaType" ))) {
394
+ operation .isMultipart = Boolean .TRUE ;
395
+ }
396
+ }
397
+ }
398
+ if (operation .returnType == null ) {
399
+ operation .returnType = "Void" ;
400
+ }
401
+ }
402
+ }
403
+ }
404
+ return objs ;
405
+ }
376
406
377
407
private CodegenModel reconcileInlineEnums (CodegenModel codegenModel , CodegenModel parentCodegenModel ) {
378
408
// This generator uses inline classes to define enums, which breaks when
0 commit comments