Skip to content

Commit 4558e49

Browse files
lukoyanovwing328
authored andcommitted
[Java] Retrofit2 Play! Framework 2.6.x support (#901)
* added play framework 2.6 support * generated petstore sample * generated petstore sample #2 * generated petstore sample #3 * Revert "generated petstore sample #3" * generated petstore sample #4 * fixed circleci configs * one more time samples regen
1 parent 5926ee5 commit 4558e49

File tree

131 files changed

+10842
-41
lines changed

Some content is hidden

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

131 files changed

+10842
-41
lines changed

CI/pom.xml.circleci

+1
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,7 @@
899899
<module>samples/client/petstore/java/retrofit2</module>
900900
<module>samples/client/petstore/java/retrofit2rx</module>
901901
<module>samples/client/petstore/java/retrofit2-play25</module>
902+
<module>samples/client/petstore/java/retrofit2-play26</module>
902903
<module>samples/client/petstore/jaxrs-cxf-client</module>
903904
<module>samples/client/petstore/java/resttemplate</module>
904905
<module>samples/client/petstore/java/resttemplate-withXml</module>
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"useBeanValidation":"true","enableBuilderSupport":"true","library":"retrofit2","usePlayWS":"true","playVersion":"play26"}

bin/java-petstore-retrofit2-play26.sh

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
echo "# START SCRIPT: $SCRIPT"
5+
6+
while [ -h "$SCRIPT" ] ; do
7+
ls=`ls -ld "$SCRIPT"`
8+
link=`expr "$ls" : '.*-> \(.*\)$'`
9+
if expr "$link" : '/.*' > /dev/null; then
10+
SCRIPT="$link"
11+
else
12+
SCRIPT=`dirname "$SCRIPT"`/"$link"
13+
fi
14+
done
15+
16+
if [ ! -d "${APP_DIR}" ]; then
17+
APP_DIR=`dirname "$SCRIPT"`/..
18+
APP_DIR=`cd "${APP_DIR}"; pwd`
19+
fi
20+
21+
executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar"
22+
23+
if [ ! -f "$executable" ]
24+
then
25+
mvn -B clean package
26+
fi
27+
28+
# if you've executed sbt assembly previously it will use that instead.
29+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
30+
ags="generate --artifact-id petstore-java-client-retrofit2-play26 -i modules/openapi-generator/src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml -g java -c bin/java-petstore-retrofit2-play26.json -o samples/client/petstore/java/retrofit2-play26 -DhideGenerationTimestamp=true $@"
31+
32+
echo "Removing files and folders under samples/client/petstore/java/retrofit2-play26/src/main"
33+
rm -rf samples/client/petstore/java/retrofit2-play26/src/main
34+
find samples/client/petstore/java/retrofit2-play26 -maxdepth 1 -type f ! -name "README.md" -exec rm {} +
35+
java $JAVA_OPTS -jar $executable $ags

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

+19-7
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717

1818
package org.openapitools.codegen.languages;
1919

20-
import static com.google.common.base.CaseFormat.LOWER_CAMEL;
21-
import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;
22-
import static java.util.Collections.sort;
23-
2420
import org.apache.commons.lang3.BooleanUtils;
2521
import org.apache.commons.lang3.StringUtils;
2622
import org.openapitools.codegen.CliOption;
@@ -43,12 +39,14 @@
4339
import java.util.Comparator;
4440
import java.util.HashMap;
4541
import java.util.Iterator;
46-
import java.util.LinkedHashMap;
47-
import java.util.LinkedList;
4842
import java.util.List;
4943
import java.util.Map;
5044
import java.util.regex.Pattern;
5145

46+
import static com.google.common.base.CaseFormat.LOWER_CAMEL;
47+
import static com.google.common.base.CaseFormat.UPPER_UNDERSCORE;
48+
import static java.util.Collections.sort;
49+
5250
public class JavaClientCodegen extends AbstractJavaCodegen
5351
implements BeanValidationFeatures, PerformBeanValidationFeatures,
5452
GzipFeatures {
@@ -67,6 +65,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
6765

6866
public static final String PLAY_24 = "play24";
6967
public static final String PLAY_25 = "play25";
68+
public static final String PLAY_26 = "play26";
7069

7170
public static final String FEIGN = "feign";
7271
public static final String GOOGLE_API_CLIENT = "google-api-client";
@@ -333,7 +332,9 @@ public void processOpts() {
333332
supportingFiles.add(new SupportingFile("play24/Play24CallFactory.mustache", invokerFolder, "Play24CallFactory.java"));
334333
supportingFiles.add(new SupportingFile("play24/Play24CallAdapterFactory.mustache", invokerFolder,
335334
"Play24CallAdapterFactory.java"));
336-
} else {
335+
}
336+
337+
if (PLAY_25.equals(playVersion)) {
337338
additionalProperties.put(PLAY_25, true);
338339
apiTemplateFiles.put("play25/api.mustache", ".java");
339340

@@ -344,6 +345,17 @@ public void processOpts() {
344345
additionalProperties.put("java8", "true");
345346
}
346347

348+
if (PLAY_26.equals(playVersion)) {
349+
additionalProperties.put(PLAY_26, true);
350+
apiTemplateFiles.put("play26/api.mustache", ".java");
351+
352+
supportingFiles.add(new SupportingFile("play26/ApiClient.mustache", invokerFolder, "ApiClient.java"));
353+
supportingFiles.add(new SupportingFile("play26/Play26CallFactory.mustache", invokerFolder, "Play26CallFactory.java"));
354+
supportingFiles.add(new SupportingFile("play26/Play26CallAdapterFactory.mustache", invokerFolder,
355+
"Play26CallAdapterFactory.java"));
356+
additionalProperties.put("java8", "true");
357+
}
358+
347359
supportingFiles.add(new SupportingFile("play-common/auth/ApiKeyAuth.mustache", authFolder, "ApiKeyAuth.java"));
348360
supportingFiles.add(new SupportingFile("auth/Authentication.mustache", authFolder, "Authentication.java"));
349361
supportingFiles.add(new SupportingFile("Pair.mustache", invokerFolder, "Pair.java"));

modules/openapi-generator/src/main/resources/Java/libraries/retrofit2/build.gradle.mustache

+10
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ ext {
129129
jackson_version = "2.7.8"
130130
play_version = "2.5.14"
131131
{{/play25}}
132+
{{#play26}}
133+
jackson_version = "2.8.10"
134+
play_version = "2.6.7"
135+
{{/play26}}
132136
{{/usePlayWS}}
133137
swagger_annotations_version = "1.5.17"
134138
junit_version = "4.12"
@@ -171,7 +175,13 @@ dependencies {
171175
compile "org.threeten:threetenbp:$threetenbp_version"
172176
{{/threetenbp}}
173177
{{#usePlayWS}}
178+
{{#play26}}
179+
compile "com.typesafe.play:play-ahc-ws_2.12:$play_version"
180+
compile "javax.validation:validation-api:1.1.0.Final"
181+
{{/play26}}
182+
{{^play26}}
174183
compile "com.typesafe.play:play-java-ws_2.11:$play_version"
184+
{{/play26}}
175185
compile "com.squareup.retrofit2:converter-jackson:$retrofit_version"
176186
compile "com.fasterxml.jackson.core:jackson-core:$jackson_version"
177187
compile "com.fasterxml.jackson.core:jackson-annotations:$jackson_version"

modules/openapi-generator/src/main/resources/Java/libraries/retrofit2/build.sbt.mustache

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ lazy val root = (project in file(".")).
2727
"com.fasterxml.jackson.core" % "jackson-annotations" % "2.7.8" % "compile",
2828
"com.fasterxml.jackson.core" % "jackson-databind" % "2.7.8" % "compile",
2929
{{/play25}}
30+
{{#play26}}
31+
"com.typesafe.play" % "play-ahc-ws_2.12" % "2.6.7" % "compile",
32+
"javax.validation" % "validation-api" % "1.1.0.Final" % "compile",
33+
"com.fasterxml.jackson.core" % "jackson-core" % "2.8.10" % "compile",
34+
"com.fasterxml.jackson.core" % "jackson-annotations" % "2.8.10" % "compile",
35+
"com.fasterxml.jackson.core" % "jackson-databind" % "2.8.10" % "compile",
36+
{{/play26}}
3037
"com.squareup.retrofit2" % "converter-jackson" % "2.3.0" % "compile",
3138
{{/usePlayWS}}
3239
{{#useRxJava}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
package {{invokerPackage}};
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.lang.annotation.Annotation;
6+
import java.lang.reflect.Type;
7+
import java.nio.file.Files;
8+
import java.nio.file.Paths;
9+
import java.util.*;
10+
11+
import com.fasterxml.jackson.databind.ObjectMapper;
12+
import retrofit2.Converter;
13+
import retrofit2.Retrofit;
14+
import retrofit2.converter.scalars.ScalarsConverterFactory;
15+
import retrofit2.converter.jackson.JacksonConverterFactory;
16+
17+
import play.libs.Json;
18+
import play.libs.ws.WSClient;
19+
20+
import {{invokerPackage}}.Play26CallAdapterFactory;
21+
import {{invokerPackage}}.Play26CallFactory;
22+
23+
import okhttp3.Interceptor;
24+
import okhttp3.ResponseBody;
25+
import {{invokerPackage}}.auth.ApiKeyAuth;
26+
import {{invokerPackage}}.auth.Authentication;
27+
28+
/**
29+
* API client
30+
*/
31+
public class ApiClient {
32+
33+
/** Underlying HTTP-client */
34+
private WSClient wsClient;
35+
36+
/** Creates HTTP call instances */
37+
private Play26CallFactory callFactory;
38+
39+
/** Create {@link java.util.concurrent.CompletionStage} instances from HTTP calls */
40+
private Play26CallAdapterFactory callAdapterFactory;
41+
42+
/** Supported auths */
43+
private Map<String, Authentication> authentications;
44+
45+
/** API base path */
46+
private String basePath = "{{{basePath}}}";
47+
48+
/** Default ObjectMapper */
49+
private ObjectMapper defaultMapper;
50+
51+
public ApiClient(WSClient wsClient) {
52+
this();
53+
this.wsClient = wsClient;
54+
}
55+
56+
public ApiClient() {
57+
// Setup authentications (key: authentication name, value: authentication).
58+
authentications = new HashMap<>();{{#authMethods}}{{#isBasic}}
59+
// authentications.put("{{name}}", new HttpBasicAuth());{{/isBasic}}{{#isApiKey}}
60+
authentications.put("{{name}}", new ApiKeyAuth({{#isKeyInHeader}}"header"{{/isKeyInHeader}}{{^isKeyInHeader}}"query"{{/isKeyInHeader}}, "{{keyParamName}}"));{{/isApiKey}}{{#isOAuth}}
61+
// authentications.put("{{name}}", new OAuth());{{/isOAuth}}{{/authMethods}}
62+
// Prevent the authentications from being modified.
63+
authentications = Collections.unmodifiableMap(authentications);
64+
}
65+
66+
/**
67+
* Creates a retrofit2 client for given API interface
68+
*/
69+
public <S> S createService(Class<S> serviceClass) {
70+
if(!basePath.endsWith("/")) {
71+
basePath = basePath + "/";
72+
}
73+
74+
Map<String, String> extraHeaders = new HashMap<>();
75+
List<Pair> extraQueryParams = new ArrayList<>();
76+
77+
for (String authName : authentications.keySet()) {
78+
Authentication auth = authentications.get(authName);
79+
if (auth == null) throw new RuntimeException("Authentication undefined: " + authName);
80+
81+
auth.applyToParams(extraQueryParams, extraHeaders);
82+
}
83+
84+
if (callFactory == null) {
85+
callFactory = new Play26CallFactory(wsClient, extraHeaders, extraQueryParams);
86+
}
87+
if (callAdapterFactory == null) {
88+
callAdapterFactory = new Play26CallAdapterFactory();
89+
}
90+
if (defaultMapper == null) {
91+
defaultMapper = Json.mapper();
92+
}
93+
94+
return new Retrofit.Builder()
95+
.baseUrl(basePath)
96+
.addConverterFactory(new FileConverter())
97+
.addConverterFactory(ScalarsConverterFactory.create())
98+
.addConverterFactory(JacksonConverterFactory.create(defaultMapper))
99+
.callFactory(callFactory)
100+
.addCallAdapterFactory(callAdapterFactory)
101+
.build()
102+
.create(serviceClass);
103+
}
104+
105+
/**
106+
* Helper method to set API base path
107+
*/
108+
public ApiClient setBasePath(String basePath) {
109+
this.basePath = basePath;
110+
return this;
111+
}
112+
113+
/**
114+
* Get authentications (key: authentication name, value: authentication).
115+
*/
116+
public Map<String, Authentication> getAuthentications() {
117+
return authentications;
118+
}
119+
120+
/**
121+
* Get authentication for the given name.
122+
*
123+
* @param authName The authentication name
124+
* @return The authentication, null if not found
125+
*/
126+
public Authentication getAuthentication(String authName) {
127+
return authentications.get(authName);
128+
}
129+
130+
/**
131+
* Helper method to set API key value for the first API key authentication.
132+
*/
133+
public ApiClient setApiKey(String apiKey) {
134+
for (Authentication auth : authentications.values()) {
135+
if (auth instanceof ApiKeyAuth) {
136+
((ApiKeyAuth) auth).setApiKey(apiKey);
137+
return this;
138+
}
139+
}
140+
141+
throw new RuntimeException("No API key authentication configured!");
142+
}
143+
144+
/**
145+
* Helper method to set API key prefix for the first API key authentication.
146+
*/
147+
public ApiClient setApiKeyPrefix(String apiKeyPrefix) {
148+
for (Authentication auth : authentications.values()) {
149+
if (auth instanceof ApiKeyAuth) {
150+
((ApiKeyAuth) auth).setApiKeyPrefix(apiKeyPrefix);
151+
return this;
152+
}
153+
}
154+
155+
throw new RuntimeException("No API key authentication configured!");
156+
}
157+
158+
/**
159+
* Helper method to set HTTP call instances factory
160+
*/
161+
public ApiClient setCallFactory(Play26CallFactory callFactory) {
162+
this.callFactory = callFactory;
163+
return this;
164+
}
165+
166+
/**
167+
* Helper method to set {@link java.util.concurrent.CompletionStage} instances factory
168+
*/
169+
public ApiClient setCallAdapterFactory(Play26CallAdapterFactory callAdapterFactory) {
170+
this.callAdapterFactory = callAdapterFactory;
171+
return this;
172+
}
173+
174+
/**
175+
* Helper method to set Jackson's {@link ObjectMapper}
176+
*/
177+
public ApiClient setObjectMapper(ObjectMapper mapper) {
178+
this.defaultMapper = mapper;
179+
return this;
180+
}
181+
182+
static class FileConverter extends Converter.Factory {
183+
184+
@Override
185+
public Converter<ResponseBody, File> responseBodyConverter(Type type,
186+
Annotation[] annotations, Retrofit retrofit) {
187+
188+
if (!File.class.getTypeName().equals(type.getTypeName())) {
189+
return null;
190+
}
191+
192+
return new Converter<ResponseBody, File>() {
193+
194+
@Override
195+
public File convert(ResponseBody value) throws IOException {
196+
197+
File file = File.createTempFile("retrofit-file", ".tmp");
198+
Files.write(Paths.get(file.getPath()), value.bytes());
199+
return file;
200+
}
201+
};
202+
}
203+
}
204+
205+
}

0 commit comments

Comments
 (0)