-
Notifications
You must be signed in to change notification settings - Fork 6k
Add new generator: Java Play Framework Server Generator #4943
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
46afc93
First commit of the Java Play Framework server generator. It is highl…
JFCote ed9e40b
Fix suggestions in the PR discussion + add .bat and .sh file as reque…
JFCote 75920eb
Updated Readme.md file
JFCote 7c18907
Remove unused mustache file + fix baseName vs paramName in all the mu…
JFCote 61bf7d9
Merge branch 'upstream/master'
JFCote fba520a
Fix the compilation error when we have a body which is a list or map.…
JFCote 902c49b
Fix the problem with the Http.MultipartFormData.FilePart
JFCote File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #!/bin/sh | ||
|
|
||
| SCRIPT="$0" | ||
|
|
||
| while [ -h "$SCRIPT" ] ; do | ||
| ls=`ls -ld "$SCRIPT"` | ||
| link=`expr "$ls" : '.*-> \(.*\)$'` | ||
| if expr "$link" : '/.*' > /dev/null; then | ||
| SCRIPT="$link" | ||
| else | ||
| SCRIPT=`dirname "$SCRIPT"`/"$link" | ||
| fi | ||
| done | ||
|
|
||
| if [ ! -d "${APP_DIR}" ]; then | ||
| APP_DIR=`dirname "$SCRIPT"`/.. | ||
| APP_DIR=`cd "${APP_DIR}"; pwd` | ||
| fi | ||
|
|
||
| executable="./modules/swagger-codegen-cli/target/swagger-codegen-cli.jar" | ||
|
|
||
| if [ ! -f "$executable" ] | ||
| then | ||
| mvn clean package | ||
| fi | ||
|
|
||
| # if you've executed sbt assembly previously it will use that instead. | ||
| export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" | ||
| ags="$@ generate -t modules/swagger-codegen/src/main/resources/JavaPlayFramework -i modules/swagger-codegen/src/test/resources/2_0/petstore.json -l java-play-framework -o samples/server/petstore/java-play-framework -DhideGenerationTimestamp=true" | ||
|
|
||
| java $JAVA_OPTS -jar $executable $ags |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| set executable=.\modules\swagger-codegen-cli\target\swagger-codegen-cli.jar | ||
|
|
||
| If Not Exist %executable% ( | ||
| mvn clean package | ||
| ) | ||
|
|
||
| REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M | ||
| set ags=generate -i modules\swagger-codegen\src\test\resources\2_0\petstore.json -l java-play-framework -o samples\server\petstore\java-play-framework | ||
|
|
||
| java %JAVA_OPTS% -jar %executable% %ags% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
248 changes: 248 additions & 0 deletions
248
.../swagger-codegen/src/main/java/io/swagger/codegen/languages/JavaPlayFrameworkCodegen.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,248 @@ | ||
| package io.swagger.codegen.languages; | ||
|
|
||
| import io.swagger.codegen.*; | ||
| import io.swagger.codegen.languages.features.BeanValidationFeatures; | ||
|
|
||
| import java.io.File; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| public class JavaPlayFrameworkCodegen extends AbstractJavaCodegen implements BeanValidationFeatures { | ||
|
|
||
| public static final String TITLE = "title"; | ||
| public static final String CONFIG_PACKAGE = "configPackage"; | ||
| public static final String BASE_PACKAGE = "basePackage"; | ||
| public static final String CONTROLLER_ONLY = "controllerOnly"; | ||
| public static final String SINGLE_CONTENT_TYPES = "singleContentTypes"; | ||
| public static final String RESPONSE_WRAPPER = "responseWrapper"; | ||
| public static final String USE_TAGS = "useTags"; | ||
|
|
||
| protected String title = "swagger-petstore"; | ||
| protected String configPackage = "io.swagger.configuration"; | ||
| protected String basePackage = "io.swagger"; | ||
| protected boolean controllerOnly = false; | ||
| protected boolean singleContentTypes = false; | ||
| protected String responseWrapper = ""; | ||
| protected boolean useTags = false; | ||
| protected boolean useBeanValidation = true; | ||
|
|
||
| public JavaPlayFrameworkCodegen() { | ||
| super(); | ||
| outputFolder = "generated-code/javaPlayFramework"; | ||
| apiTestTemplateFiles.clear(); | ||
| embeddedTemplateDir = templateDir = "JavaPlayFramework"; | ||
| apiPackage = "controllers"; | ||
| modelPackage = "apimodels"; | ||
| invokerPackage = "io.swagger.api"; | ||
| artifactId = "swagger-java-playframework"; | ||
|
|
||
| projectFolder = ""; | ||
| sourceFolder = projectFolder + File.separator + "app"; | ||
| projectTestFolder = projectFolder + File.separator + "test"; | ||
| testFolder = projectTestFolder; | ||
|
|
||
| additionalProperties.put(CONFIG_PACKAGE, configPackage); | ||
| additionalProperties.put(BASE_PACKAGE, basePackage); | ||
|
|
||
| additionalProperties.put("jackson", "true"); | ||
|
|
||
| cliOptions.add(new CliOption(TITLE, "server title name or client service name")); | ||
| cliOptions.add(new CliOption(CONFIG_PACKAGE, "configuration package for generated code")); | ||
| cliOptions.add(new CliOption(BASE_PACKAGE, "base package for generated code")); | ||
| cliOptions.add(CliOption.newBoolean(CONTROLLER_ONLY, "Whether to generate only API interface stubs without the server files.")); | ||
| cliOptions.add(CliOption.newBoolean(SINGLE_CONTENT_TYPES, "Whether to select only one produces/consumes content-type by operation.")); | ||
| cliOptions.add(new CliOption(RESPONSE_WRAPPER, "wrap the responses in given type (Future,Callable,CompletableFuture,ListenableFuture,DeferredResult,HystrixCommand,RxObservable,RxSingle or fully qualified type)")); | ||
| cliOptions.add(CliOption.newBoolean(USE_TAGS, "use tags for creating interface and controller classnames")); | ||
| cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations")); | ||
| } | ||
|
|
||
| @Override | ||
| public CodegenType getTag() { | ||
| return CodegenType.SERVER; | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "java-play-framework"; | ||
| } | ||
|
|
||
| @Override | ||
| public String getHelp() { | ||
| return "Generates a Java Play Framework Server application."; | ||
| } | ||
|
|
||
| @Override | ||
| public void processOpts() { | ||
| super.processOpts(); | ||
|
|
||
| // clear model and api doc template as this codegen | ||
| // does not support auto-generated markdown doc at the moment | ||
| //TODO: add doc templates | ||
| modelDocTemplateFiles.remove("model_doc.mustache"); | ||
| apiDocTemplateFiles.remove("api_doc.mustache"); | ||
|
|
||
| if (additionalProperties.containsKey(TITLE)) { | ||
| this.setTitle((String) additionalProperties.get(TITLE)); | ||
| } | ||
|
|
||
| if (additionalProperties.containsKey(CONFIG_PACKAGE)) { | ||
| this.setConfigPackage((String) additionalProperties.get(CONFIG_PACKAGE)); | ||
| } | ||
|
|
||
| if (additionalProperties.containsKey(BASE_PACKAGE)) { | ||
| this.setBasePackage((String) additionalProperties.get(BASE_PACKAGE)); | ||
| } | ||
|
|
||
| if (additionalProperties.containsKey(CONTROLLER_ONLY)) { | ||
| this.setControllerOnly(Boolean.valueOf(additionalProperties.get(CONTROLLER_ONLY).toString())); | ||
| } | ||
|
|
||
| if (additionalProperties.containsKey(SINGLE_CONTENT_TYPES)) { | ||
| this.setSingleContentTypes(Boolean.valueOf(additionalProperties.get(SINGLE_CONTENT_TYPES).toString())); | ||
| } | ||
|
|
||
| if (additionalProperties.containsKey(RESPONSE_WRAPPER)) { | ||
| this.setResponseWrapper((String) additionalProperties.get(RESPONSE_WRAPPER)); | ||
| } | ||
|
|
||
| if (additionalProperties.containsKey(USE_TAGS)) { | ||
| this.setUseTags(Boolean.valueOf(additionalProperties.get(USE_TAGS).toString())); | ||
| } | ||
|
|
||
| if (additionalProperties.containsKey(USE_BEANVALIDATION)) { | ||
| this.setUseBeanValidation(convertPropertyToBoolean(USE_BEANVALIDATION)); | ||
| } | ||
|
|
||
| if (useBeanValidation) { | ||
| writePropertyBack(USE_BEANVALIDATION, useBeanValidation); | ||
| } | ||
|
|
||
| //Root folder | ||
| supportingFiles.add(new SupportingFile("README.mustache", "", "README")); | ||
| supportingFiles.add(new SupportingFile("LICENSE.mustache", "", "LICENSE")); | ||
| supportingFiles.add(new SupportingFile("build.mustache", "", "build.sbt")); | ||
|
|
||
| //Project folder | ||
| supportingFiles.add(new SupportingFile("buildproperties.mustache", "project", "build.properties")); | ||
| supportingFiles.add(new SupportingFile("plugins.mustache", "project", "plugins.sbt")); | ||
|
|
||
| //Conf folder | ||
| supportingFiles.add(new SupportingFile("logback.mustache", "conf", "logback.xml")); | ||
| supportingFiles.add(new SupportingFile("application.mustache", "conf", "application.conf")); | ||
| supportingFiles.add(new SupportingFile("routes.mustache", "conf", "routes")); | ||
|
|
||
| //App/Utils folder | ||
| supportingFiles.add(new SupportingFile("swaggerUtils.mustache", "app/swagger", "SwaggerUtils.java")); | ||
|
|
||
| //App/Controllers | ||
| supportingFiles.add(new SupportingFile("apiDocController.mustache", "app/controllers", "ApiDocController.java")); | ||
|
|
||
| //We remove the default api.mustache that is used | ||
| apiTemplateFiles.remove("api.mustache"); | ||
| apiTemplateFiles.put("newApiController.mustache", "Controller.java"); | ||
| if (!this.controllerOnly) { | ||
| apiTemplateFiles.put("newApi.mustache", "ControllerImp.java"); | ||
| } | ||
|
|
||
| additionalProperties.put("javaVersion", "1.8"); | ||
| additionalProperties.put("jdk8", "true"); | ||
| typeMapping.put("date", "LocalDate"); | ||
| typeMapping.put("DateTime", "OffsetDateTime"); | ||
| importMapping.put("LocalDate", "java.time.LocalDate"); | ||
| importMapping.put("OffsetDateTime", "java.time.OffsetDateTime"); | ||
|
|
||
| // Some well-known Spring or Spring-Cloud response wrappers | ||
| switch (this.responseWrapper) { | ||
| case "Future": | ||
| case "Callable": | ||
| case "CompletableFuture": | ||
| additionalProperties.put(RESPONSE_WRAPPER, "java.util.concurrent" + this.responseWrapper); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| public void setTitle(String title) { | ||
| this.title = title; | ||
| } | ||
|
|
||
| public void setConfigPackage(String configPackage) { | ||
| this.configPackage = configPackage; | ||
| } | ||
|
|
||
| public void setBasePackage(String configPackage) { | ||
| this.basePackage = configPackage; | ||
| } | ||
|
|
||
| public void setControllerOnly(boolean controllerOnly) { this.controllerOnly = controllerOnly; } | ||
|
|
||
| public void setSingleContentTypes(boolean singleContentTypes) { | ||
| this.singleContentTypes = singleContentTypes; | ||
| } | ||
|
|
||
| public void setResponseWrapper(String responseWrapper) { this.responseWrapper = responseWrapper; } | ||
|
|
||
| public void setUseTags(boolean useTags) { | ||
| this.useTags = useTags; | ||
| } | ||
|
|
||
| @Override | ||
| public Map<String, Object> postProcessOperations(Map<String, Object> objs) { | ||
| Map<String, Object> operations = (Map<String, Object>) objs.get("operations"); | ||
| if (operations != null) { | ||
| List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation"); | ||
| for (CodegenOperation operation : ops) { | ||
|
|
||
| //This is to fix this bug in the swagger-play project: https://github.com/swagger-api/swagger-play/issues/131 | ||
| //We need to explicitly add the model package name in front of the dataType because if we don't, the | ||
| //implicitParam is not valid and show error when loading the documentation | ||
| //This can be removed safely after the bug has been fixed | ||
| for (CodegenParameter param : operation.allParams) { | ||
| if (!param.isPathParam ) { | ||
| if (!param.isPrimitiveType && !param.isListContainer && !param.isMapContainer) { | ||
| param.dataTypeForImplicitParam = String.format("%s.%s", modelPackage, param.dataType); | ||
| } else { | ||
| param.dataTypeForImplicitParam = param.dataType; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (operation.path.contains("{")) { | ||
| operation.path = operation.path.replace("{", ":").replace("}", ""); | ||
| } | ||
|
|
||
| if (operation.returnType != null) { | ||
| if (operation.returnType.startsWith("List")) { | ||
| String rt = operation.returnType; | ||
| int end = rt.lastIndexOf(">"); | ||
| if (end > 0) { | ||
| operation.returnType = rt.substring("List<".length(), end).trim(); | ||
| operation.returnContainer = "List"; | ||
| } | ||
| } else if (operation.returnType.startsWith("Map")) { | ||
| String rt = operation.returnType; | ||
| int end = rt.lastIndexOf(">"); | ||
| if (end > 0) { | ||
| operation.returnType = rt.substring("Map<".length(), end).split(",")[1].trim(); | ||
| operation.returnContainer = "Map"; | ||
| } | ||
| } else if (operation.returnType.startsWith("Set")) { | ||
| String rt = operation.returnType; | ||
| int end = rt.lastIndexOf(">"); | ||
| if (end > 0) { | ||
| operation.returnType = rt.substring("Set<".length(), end).trim(); | ||
| operation.returnContainer = "Set"; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return objs; | ||
| } | ||
|
|
||
| public void setUseBeanValidation(boolean useBeanValidation) { | ||
| this.useBeanValidation = useBeanValidation; | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
modules/swagger-codegen/src/main/resources/JavaPlayFramework/LICENSE.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| This software is licensed under the Apache 2 license, quoted below. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with | ||
| the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an | ||
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific | ||
| language governing permissions and limitations under the License. | ||
4 changes: 4 additions & 0 deletions
4
modules/swagger-codegen/src/main/resources/JavaPlayFramework/README.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| This is your new Play application | ||
| ================================= | ||
|
|
||
| This file will be packaged with your application when using `activator dist`. |
15 changes: 15 additions & 0 deletions
15
modules/swagger-codegen/src/main/resources/JavaPlayFramework/apiDocController.mustache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package controllers; | ||
|
|
||
| import javax.inject.*; | ||
| import play.mvc.*; | ||
|
|
||
| public class ApiDocController extends Controller { | ||
|
|
||
| @Inject | ||
| private ApiDocController() { | ||
| } | ||
|
|
||
| public Result api() { | ||
| return redirect(String.format("/assets/lib/swagger-ui/index.html?/url=%s/api-docs", "")); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might later need to empty this file as there's a discussion about not setting any license to the auto-generated code and let the users to decide which license (e.g. MIT, GNU, Apache2, etc) to use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no problem. I was just mimicking the output of a brand new Play Framework project.