Skip to content

Commit c91ce17

Browse files
ignaciomolinawing328
authored andcommitted
Feature/javaPlayWithAsynchronousControllers (#7705)
* Add property 'supportAsync' to allow the use of CompletionStage of java8 * Add support for completionStage in play-framework templates * Add script to generate samples for play-framework async controllers * Add generated samples for java play framework with asynchronous controllers * Add missing templates and generate samples * Remove useless comments from generated samples in play framework async * Fix ControllerImp template for java play framework * Add script for java play framework async to general script * Regenerate java play framework server samples * Fix missing whitespace * Fix unnecessary blank lines at imports * Fix tabulation issue * Fix tabulation issue in controllers * Remove blanks from api Imp * Remove more empty lines * Add blank between methods * Remove blanks before call to service method * Fix some tabulations in java play async templates * Regenerate samples for java play async
1 parent 009dcf0 commit c91ce17

File tree

45 files changed

+3488
-46
lines changed

Some content is hidden

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

45 files changed

+3488
-46
lines changed

bin/java-play-framework-petstore-server-all.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
./bin/java-play-framework-petstore-server-no-swagger-ui.sh
99
./bin/java-play-framework-petstore-server-no-wrap-calls.sh
1010
./bin/java-play-framework-petstore-server-fake-endpoints.sh
11-
./bin/java-play-framework-petstore-server-api-package-override.sh
11+
./bin/java-play-framework-petstore-server-api-package-override.sh
12+
./bin/java-play-framework-petstore-server-async.sh
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"additionalProperties" : {
3+
"supportAsync" : true
4+
}
5+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
3+
SCRIPT="$0"
4+
5+
while [ -h "$SCRIPT" ] ; do
6+
ls=`ls -ld "$SCRIPT"`
7+
link=`expr "$ls" : '.*-> \(.*\)$'`
8+
if expr "$link" : '/.*' > /dev/null; then
9+
SCRIPT="$link"
10+
else
11+
SCRIPT=`dirname "$SCRIPT"`/"$link"
12+
fi
13+
done
14+
15+
if [ ! -d "${APP_DIR}" ]; then
16+
APP_DIR=`dirname "$SCRIPT"`/..
17+
APP_DIR=`cd "${APP_DIR}"; pwd`
18+
fi
19+
20+
executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar"
21+
22+
if [ ! -f "$executable" ]
23+
then
24+
mvn clean package
25+
fi
26+
27+
# if you've executed sbt assembly previously it will use that instead.
28+
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
29+
ags="$@ generate -t modules/swagger-codegen/src/main/resources/JavaPlayFramework -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l java-play-framework -c bin/java-play-framework-petstore-server-async.json -o samples/server/petstore/java-play-framework-async -DhideGenerationTimestamp=true"
30+
31+
java $JAVA_OPTS -jar $executable $ags

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
5151
public static final String DEFAULT_LIBRARY = "<default>";
5252
public static final String DATE_LIBRARY = "dateLibrary";
5353
public static final String JAVA8_MODE = "java8";
54+
public static final String SUPPORT_ASYNC = "supportAsync";
5455
public static final String WITH_XML = "withXml";
5556
public static final String SUPPORT_JAVA6 = "supportJava6";
5657

5758
protected String dateLibrary = "threetenbp";
59+
protected boolean supportAsync = false;
5860
protected boolean java8Mode = false;
5961
protected boolean withXml = false;
6062
protected String invokerPackage = "io.swagger";
@@ -173,7 +175,6 @@ public AbstractJavaCodegen() {
173175
java8ModeOptions.put("false", "Various third party libraries as needed");
174176
java8Mode.setEnum(java8ModeOptions);
175177
cliOptions.add(java8Mode);
176-
177178
}
178179

179180
@Override
@@ -381,14 +382,21 @@ public void processOpts() {
381382
// used later in recursive import in postProcessingModels
382383
importMapping.put("com.fasterxml.jackson.annotation.JsonProperty", "com.fasterxml.jackson.annotation.JsonCreator");
383384

384-
if(additionalProperties.containsKey(JAVA8_MODE)) {
385+
if (additionalProperties.containsKey(JAVA8_MODE)) {
385386
setJava8Mode(Boolean.parseBoolean(additionalProperties.get(JAVA8_MODE).toString()));
386387
if ( java8Mode ) {
387388
additionalProperties.put("java8", "true");
388389
}
389390
}
390391

391-
if(additionalProperties.containsKey(WITH_XML)) {
392+
if (additionalProperties.containsKey(SUPPORT_ASYNC)) {
393+
setSupportAsync(Boolean.parseBoolean(additionalProperties.get(SUPPORT_ASYNC).toString()));
394+
if (supportAsync) {
395+
additionalProperties.put(SUPPORT_ASYNC, "true");
396+
}
397+
}
398+
399+
if (additionalProperties.containsKey(WITH_XML)) {
392400
setWithXml(Boolean.parseBoolean(additionalProperties.get(WITH_XML).toString()));
393401
if ( withXml ) {
394402
additionalProperties.put(WITH_XML, "true");
@@ -1217,6 +1225,10 @@ public void setJava8Mode(boolean enabled) {
12171225
this.java8Mode = enabled;
12181226
}
12191227

1228+
public void setSupportAsync(boolean enabled) {
1229+
this.supportAsync = enabled;
1230+
}
1231+
12201232
@Override
12211233
public String escapeQuotationMark(String input) {
12221234
// remove " to avoid code injection

modules/swagger-codegen/src/main/resources/JavaPlayFramework/newApi.mustache

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@ import javax.validation.constraints.*;
1616
public class {{classname}}ControllerImp {{#useInterfaces}}implements {{classname}}ControllerImpInterface{{/useInterfaces}} {
1717
{{#operation}}
1818
{{#useInterfaces}}@Override{{/useInterfaces}}
19-
public {{>returnTypes}} {{operationId}}({{#allParams}}{{>pathParams}}{{>queryParams}}{{>bodyParams}}{{>formParams}}{{>headerParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {{#handleExceptions}}throws Exception{{/handleExceptions}} {
19+
public {{^returnType}}void{{/returnType}}{{#returnType}}{{#supportAsync}}CompletionStage<{{/supportAsync}}{{>returnTypesNoVoid}}{{#supportAsync}}>{{/supportAsync}}{{/returnType}} {{operationId}}({{#allParams}}{{>pathParams}}{{>queryParams}}{{>bodyParams}}{{>formParams}}{{>headerParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {{#handleExceptions}}throws Exception{{/handleExceptions}} {
2020
//Do your magic!!!
2121
{{#returnType}}
22+
{{#supportAsync}}
23+
return CompletableFuture.supplyAsync(() -> {
24+
{{/supportAsync}}
2225
{{#isResponseFile}}
2326
return new FileInputStream("replace this");
2427
{{/isResponseFile}}
2528
{{^isResponseFile}}
26-
return new {{>returnTypesNoVoidNoAbstract}}({{#vendorExtensions.missingReturnInfoIfNeeded}}{{vendorExtensions.missingReturnInfoIfNeeded}}{{/vendorExtensions.missingReturnInfoIfNeeded}});
29+
{{#supportAsync}} {{/supportAsync}}return new {{>returnTypesNoVoidNoAbstract}}({{#vendorExtensions.missingReturnInfoIfNeeded}}{{vendorExtensions.missingReturnInfoIfNeeded}}{{/vendorExtensions.missingReturnInfoIfNeeded}});
2730
{{/isResponseFile}}
31+
{{#supportAsync}}
32+
});
33+
{{/supportAsync}}
2834
{{/returnType}}
2935
}
3036

modules/swagger-codegen/src/main/resources/JavaPlayFramework/newApiController.mustache

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ import java.io.IOException;
1818
{{/handleExceptions}}
1919
import swagger.SwaggerUtils;
2020
import com.fasterxml.jackson.core.type.TypeReference;
21+
{{#supportAsync}}
22+
23+
import java.util.concurrent.CompletionStage;
24+
import java.util.concurrent.CompletableFuture;
25+
{{/supportAsync}}
2126

2227
{{#useBeanValidation}}
2328
import javax.validation.constraints.*;
@@ -54,7 +59,7 @@ public class {{classname}}Controller extends Controller {
5459
{{#operation}}
5560

5661
{{#wrapCalls}}@ApiAction{{/wrapCalls}}
57-
public Result {{operationId}}({{#pathParams}}{{>pathParams}}{{#hasMore}},{{/hasMore}}{{/pathParams}}) {{^handleExceptions}}{{#bodyParams}}throws IOException{{/bodyParams}}{{/handleExceptions}}{{#handleExceptions}}throws Exception{{/handleExceptions}} {
62+
public {{#supportAsync}}CompletionStage<{{/supportAsync}}Result{{#supportAsync}}>{{/supportAsync}} {{operationId}}({{#pathParams}}{{>pathParams}}{{#hasMore}},{{/hasMore}}{{/pathParams}}) {{^handleExceptions}}{{#bodyParams}}throws IOException{{/bodyParams}}{{/handleExceptions}}{{#handleExceptions}}throws Exception{{/handleExceptions}} {
5863
{{#bodyParams}}
5964
{{^collectionFormat}}
6065
JsonNode node{{paramName}} = request().body().asJson();
@@ -195,11 +200,17 @@ public class {{classname}}Controller extends Controller {
195200
{{/collectionFormat}}
196201
{{/headerParams}}
197202
{{^controllerOnly}}
198-
{{#returnType}}{{>returnTypesNoVoid}} obj = {{/returnType}}imp.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
203+
{{^returnType}}
204+
{{#supportAsync}}
205+
return CompletableFuture.supplyAsync(() -> {
206+
{{/supportAsync}}
207+
{{/returnType}}
208+
{{#returnType}}{{#supportAsync}}CompletionStage<{{>returnTypesNoVoid}}> stage = {{/supportAsync}}{{^supportAsync}}{{>returnTypesNoVoid}} obj = {{/supportAsync}}{{/returnType}}{{^returnType}}{{#supportAsync}} {{/supportAsync}}{{/returnType}}imp.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}){{#returnType}}{{#supportAsync}}.thenApply(obj -> { {{/supportAsync}}{{/returnType}}{{^supportAsync}};{{/supportAsync}}
199209
{{#returnType}}
200210
{{^isResponseFile}}
201211
{{^returnTypeIsPrimitive}}
202212
{{#useBeanValidation}}
213+
{{^supportAsync}}
203214
if (configuration.getBoolean("useOutputBeanValidation")) {
204215
{{#isListContainer}}
205216
for ({{{returnType}}} curItem : obj) {
@@ -215,21 +226,50 @@ public class {{classname}}Controller extends Controller {
215226
SwaggerUtils.validate(obj);
216227
{{/returnContainer}}
217228
}
229+
{{/supportAsync}}
230+
{{#supportAsync}}
231+
if (configuration.getBoolean("useOutputBeanValidation")) {
232+
{{#isListContainer}}
233+
for ({{{returnType}}} curItem : obj) {
234+
SwaggerUtils.validate(curItem);
235+
}
236+
{{/isListContainer}}
237+
{{#isMapContainer}}
238+
for (Map.Entry<String, {{{returnType}}}> entry : obj.entrySet()) {
239+
SwaggerUtils.validate(entry.getValue());
240+
}
241+
{{/isMapContainer}}
242+
{{^returnContainer}}
243+
SwaggerUtils.validate(obj);
244+
{{/returnContainer}}
245+
}
246+
{{/supportAsync}}
218247
{{/useBeanValidation}}
219248
{{/returnTypeIsPrimitive}}
220249
{{/isResponseFile}}
250+
{{#supportAsync}}
251+
return obj;
252+
});
253+
{{/supportAsync}}
221254
{{/returnType}}
222255
{{#returnType}}
223-
{{^isResponseFile}}JsonNode result = mapper.valueToTree(obj);
224-
return ok(result);
256+
{{#supportAsync}}
257+
stage.thenApply(obj -> {
258+
{{/supportAsync}}
259+
{{^isResponseFile}}
260+
{{#supportAsync}} {{/supportAsync}}JsonNode result = mapper.valueToTree(obj);
261+
{{#supportAsync}} {{/supportAsync}}return ok(result);
225262
{{/isResponseFile}}
226263
{{#isResponseFile}}
227-
return ok(obj);
264+
{{#supportAsync}} {{/supportAsync}}return ok(obj);
228265
{{/isResponseFile}}
229266
{{/returnType}}
230267
{{^returnType}}
231-
return ok();
268+
{{#supportAsync}} {{/supportAsync}}return ok();
232269
{{/returnType}}
270+
{{#supportAsync}}
271+
});
272+
{{/supportAsync}}
233273
{{/controllerOnly}}
234274
{{#controllerOnly}}
235275
return ok();

modules/swagger-codegen/src/main/resources/JavaPlayFramework/newApiInterface.mustache

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import play.mvc.Http;
77
import java.util.List;
88
import java.util.ArrayList;
99
import java.util.HashMap;
10+
{{#supportAsync}}
11+
import java.util.concurrent.CompletionStage;
12+
import java.util.concurrent.CompletableFuture;
13+
{{/supportAsync}}
1014

1115
{{#useBeanValidation}}
1216
import javax.validation.constraints.*;
@@ -16,7 +20,7 @@ import javax.validation.constraints.*;
1620
@SuppressWarnings("RedundantThrows")
1721
public interface {{classname}}ControllerImpInterface {
1822
{{#operation}}
19-
{{>returnTypes}} {{operationId}}({{#allParams}}{{>pathParams}}{{>queryParams}}{{>bodyParams}}{{>formParams}}{{>headerParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {{#handleExceptions}}throws Exception{{/handleExceptions}};
23+
{{^returnType}}void{{/returnType}}{{#returnType}}{{#supportAsync}}CompletionStage<{{/supportAsync}}{{>returnTypesNoVoid}}{{#supportAsync}}>{{/supportAsync}}{{/returnType}} {{operationId}}({{#allParams}}{{>pathParams}}{{>queryParams}}{{>bodyParams}}{{>formParams}}{{>headerParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {{#handleExceptions}}throws Exception{{/handleExceptions}};
2024

2125
{{/operation}}
2226
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Swagger Codegen Ignore
2+
# Generated by swagger-codegen https://github.com/swagger-api/swagger-codegen
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell Swagger Codgen to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.4.0-SNAPSHOT
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
This software is licensed under the Apache 2 license, quoted below.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with
4+
the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
5+
6+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
7+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
8+
language governing permissions and limitations under the License.

0 commit comments

Comments
 (0)