Skip to content

Commit 1863aca

Browse files
committed
set retrofit as a java library
Fix #1247 Fix #1248
1 parent 27e434d commit 1863aca

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+905
-931
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ CONFIG OPTIONS
265265
<default> - HTTP client: Jersey client 1.18. JSON processing: Jackson 2.4.2
266266
jersey2 - HTTP client: Jersey client 2.6
267267
okhttp-gson - HTTP client: OkHttp 2.4.0. JSON processing: Gson 2.3.1
268+
retrofit - HTTP client: OkHttp 2.4.0. JSON processing: Gson 2.3.1 (Retrofit 1.9.0)
268269
```
269270

270271
Your config file for java can look like

bin/all-petstore.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ cd $APP_DIR
2626
./bin/java-petstore.sh
2727
./bin/java-petstore-jersey2.sh
2828
./bin/java-petstore-okhttp-gson.sh
29+
./bin/java-petstore-retrofit.sh
2930
./bin/jaxrs-petstore-server.sh
3031
./bin/nodejs-petstore-server.sh
3132
./bin/objc-petstore.sh
@@ -34,7 +35,6 @@ cd $APP_DIR
3435
./bin/python-petstore.sh
3536
./bin/python3-petstore.sh
3637
./bin/qt5-petstore.sh
37-
./bin/retrofit-petstore.sh
3838
./bin/ruby-petstore.sh
3939
./bin/scala-async-petstore.sh
4040
./bin/scala-petstore.sh

bin/java-petstore-retrofit.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"library": "retrofit",
3+
"artifactId": "swagger-petstore-retrofit"
4+
}

bin/retrofit-petstore.sh renamed to bin/java-petstore-retrofit.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ fi
2626

2727
# if you've executed sbt assembly previously it will use that instead.
2828
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29-
ags="$@ generate -t modules/swagger-codegen/src/main/resources/retrofit -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l retrofit -o samples/client/petstore/retrofit"
29+
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l java -c bin/java-petstore-retrofit.json -o samples/client/petstore/java/retrofit"
3030

3131
java $JAVA_OPTS -jar $executable $ags

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaClientCodegen.java

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.swagger.codegen.CodegenConfig;
66
import io.swagger.codegen.CodegenConstants;
77
import io.swagger.codegen.CodegenModel;
8+
import io.swagger.codegen.CodegenOperation;
89
import io.swagger.codegen.CodegenProperty;
910
import io.swagger.codegen.CodegenType;
1011
import io.swagger.codegen.DefaultCodegen;
@@ -84,6 +85,7 @@ public JavaClientCodegen() {
8485
supportedLibraries.put("<default>", "HTTP client: Jersey client 1.18. JSON processing: Jackson 2.4.2");
8586
supportedLibraries.put("jersey2", "HTTP client: Jersey client 2.6");
8687
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)");
8789
cliOptions.add(buildLibraryCliOption(supportedLibraries));
8890
}
8991

@@ -155,12 +157,22 @@ public void processOpts() {
155157
final String invokerFolder = (sourceFolder + File.separator + invokerPackage).replace(".", File.separator);
156158
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
157159
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"));
162160
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+
164176
// library-specific files
165177
if ("okhttp-gson".equals(getLibrary())) {
166178
// the "okhttp-gson" library template requires "ApiCallback.mustache" for async call
@@ -169,16 +181,11 @@ public void processOpts() {
169181
supportingFiles.add(new SupportingFile("build.gradle.mustache", "", "build.gradle"));
170182
// "build.sbt" is for development with SBT
171183
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"));
173186
} else {
174187
supportingFiles.add(new SupportingFile("TypeRef.mustache", invokerFolder, "TypeRef.java"));
175188
}
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"));
182189
}
183190

184191
private void sanitizeConfig() {
@@ -373,6 +380,29 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
373380
}
374381
return objs;
375382
}
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+
}
376406

377407
private CodegenModel reconcileInlineEnums(CodegenModel codegenModel, CodegenModel parentCodegenModel) {
378408
// This generator uses inline classes to define enums, which breaks when

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RetrofitClientCodegen.java

Lines changed: 0 additions & 220 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package {{invokerPackage}}.auth;
22

3-
public enum OauthFlow {
3+
public enum OAuthFlow {
44
accessCode, implicit, password, application
55
}

0 commit comments

Comments
 (0)