Add File support + some fixes to Play generator (#5263)

* First commit of the Java Play Framework server generator. It is highly based on Spring so there might me a couple of things that don't make sense (like options or parameters) for the Play Framework.

* Fix suggestions in the PR discussion + add .bat and .sh file as requested.

* Updated Readme.md file

* Remove unused mustache file + fix baseName vs paramName in all the mustache files.

* Fix the compilation error when we have a body which is a list or map. Doesn't fix the problem with the annotation itself.

* Fix the problem with the Http.MultipartFormData.FilePart

* Return a FileInputStream when the returnType is "file" + Fix a couple of other bugs with boolean + updated sample

* Return an InputStream instead of FileInputStream (except in the instantiation)

* A little cleanup for the form param of type file.
This commit is contained in:
Jean-François Côté 2017-04-10 11:23:28 -04:00 committed by wing328
parent aef98f464e
commit 2e46bb9b9f
12 changed files with 46 additions and 23 deletions

View File

@ -12,9 +12,6 @@ public class CodegenParameter {
public String baseName, paramName, dataType, datatypeWithEnum, dataFormat,
collectionFormat, description, unescapedDescription, baseType, defaultValue, enumName;
//This was added for javaPlayFramework specifically to get around a bug in swagger-play. See generator for more info on the bug.
public String dataTypeForImplicitParam;
public String example; // example value (x-example)
public String jsonSchema;
public boolean isString, isInteger, isLong, isFloat, isDouble, isByteArray, isBinary, isBoolean, isDate, isDateTime;

View File

@ -188,6 +188,9 @@ public class JavaPlayFrameworkCodegen extends AbstractJavaCodegen implements Bea
typeMapping.put("DateTime", "OffsetDateTime");
importMapping.put("LocalDate", "java.time.LocalDate");
importMapping.put("OffsetDateTime", "java.time.OffsetDateTime");
importMapping.put("InputStream", "java.io.InputStream");
typeMapping.put("file", "InputStream");
}
@Override
@ -249,18 +252,16 @@ public class JavaPlayFrameworkCodegen extends AbstractJavaCodegen implements Bea
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 (param.isFormParam && param.isFile) {
param.dataType = "Http.MultipartFormData.FilePart";
}
}
for (CodegenParameter param : operation.formParams) {
if (param.isFile) {
param.dataType = "Http.MultipartFormData.FilePart";
}
}
if (operation.path.contains("{")) {

View File

@ -1 +1 @@
{{#isInteger}}Integer.parseInt({{/isInteger}}{{#isDouble}}Double.parseDouble({{/isDouble}}{{#isLong}}Long.parseLong({{/isLong}}{{#isFloat}}Float.parseFloat({{/isFloat}}{{#isString}}(String){{/isString}}
{{#isBoolean}}Boolean.getBoolean({{/isBoolean}}{{#isInteger}}Integer.parseInt({{/isInteger}}{{#isDouble}}Double.parseDouble({{/isDouble}}{{#isLong}}Long.parseLong({{/isLong}}{{#isFloat}}Float.parseFloat({{/isFloat}}{{#isString}}(String){{/isString}}

View File

@ -1 +1 @@
{{#isInteger}}){{/isInteger}}{{#isDouble}}){{/isDouble}}{{#isLong}}){{/isLong}}{{#isFloat}}){{/isFloat}}{{#isString}}{{/isString}}
{{#isBoolean}}){{/isBoolean}}{{#isInteger}}){{/isInteger}}{{#isDouble}}){{/isDouble}}{{#isLong}}){{/isLong}}{{#isFloat}}){{/isFloat}}{{#isString}}{{/isString}}

View File

@ -1 +1 @@
{{#isFormParam}}{{#notFile}}{{{dataType}}} {{paramName}}{{/notFile}}{{#isFile}}Http.MultipartFormData.FilePart {{paramName}}{{/isFile}}{{/isFormParam}}
{{#isFormParam}}{{{dataType}}} {{paramName}}{{/isFormParam}}

View File

@ -7,6 +7,7 @@ import play.mvc.Http;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.io.FileInputStream;
{{#useBeanValidation}}
import javax.validation.constraints.*;
{{/useBeanValidation}}
@ -14,9 +15,10 @@ import javax.validation.constraints.*;
{{#operations}}
public class {{classname}}ControllerImp {{#useInterfaces}}implements {{classname}}ControllerImpInterface{{/useInterfaces}} {
{{#operation}}
{{#useInterfaces}}@Override{{/useInterfaces}}
public {{>returnTypes}} {{operationId}}({{#allParams}}{{>pathParams}}{{>queryParams}}{{>bodyParams}}{{>formParams}}{{>headerParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {{#handleExceptions}}throws Exception{{/handleExceptions}} {
//Do your magic!!!
{{#returnType}}return new {{>returnTypesNoVoidNoAbstract}}();{{/returnType}}
{{#returnType}}{{#isResponseFile}}return new FileInputStream("replace this");{{/isResponseFile}}{{^isResponseFile}}return new {{>returnTypesNoVoidNoAbstract}}();{{/isResponseFile}}{{/returnType}}
}
{{/operation}}

View File

@ -84,7 +84,7 @@ public class {{classname}}Controller extends Controller {
{{/queryParams}}
{{#formParams}}
{{^notFile}}
Http.MultipartFormData.FilePart {{paramName}} = request().body().asMultipartFormData().getFile("{{baseName}}");
{{{dataType}}} {{paramName}} = request().body().asMultipartFormData().getFile("{{baseName}}");
{{#required}}if (({{paramName}} == null || ((File) {{paramName}}.getFile()).length() == 0)) {
throw new RuntimeException("File cannot be empty");
}
@ -140,8 +140,8 @@ public class {{classname}}Controller extends Controller {
{{/collectionFormat}}
{{/headerParams}}
{{#returnType}}{{>returnTypesNoVoid}} obj = {{/returnType}}imp.{{operationId}}({{#allParams}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
{{#returnType}}JsonNode result = mapper.valueToTree(obj);
return ok(result);{{/returnType}}
{{#returnType}}{{^isResponseFile}}JsonNode result = mapper.valueToTree(obj);
return ok(result);{{/isResponseFile}}{{#isResponseFile}}return ok(obj);{{/isResponseFile}}{{/returnType}}
{{^returnType}}return ok();{{/returnType}}
}
{{/operation}}

View File

@ -1,6 +1,6 @@
package controllers;
import java.io.File;
import java.io.InputStream;
import apimodels.Pet;
import play.mvc.Controller;

View File

@ -1,50 +1,59 @@
package controllers;
import java.io.File;
import java.io.InputStream;
import apimodels.Pet;
import play.mvc.Http;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.io.FileInputStream;
import javax.validation.constraints.*;
public class PetApiControllerImp implements PetApiControllerImpInterface {
@Override
public void addPet(Pet body) throws Exception {
//Do your magic!!!
}
@Override
public void deletePet(Long petId, String apiKey) throws Exception {
//Do your magic!!!
}
@Override
public List<Pet> findPetsByStatus( List<String> status) throws Exception {
//Do your magic!!!
return new ArrayList<Pet>();
}
@Override
public List<Pet> findPetsByTags( List<String> tags) throws Exception {
//Do your magic!!!
return new ArrayList<Pet>();
}
@Override
public Pet getPetById(Long petId) throws Exception {
//Do your magic!!!
return new Pet();
}
@Override
public void updatePet(Pet body) throws Exception {
//Do your magic!!!
}
@Override
public void updatePetWithForm(String petId, String name, String status) throws Exception {
//Do your magic!!!
}
@Override
public void uploadFile(Long petId, String additionalMetadata, Http.MultipartFormData.FilePart file) throws Exception {
//Do your magic!!!

View File

@ -1,6 +1,6 @@
package controllers;
import java.io.File;
import java.io.InputStream;
import apimodels.Pet;
import play.mvc.Http;

View File

@ -7,24 +7,29 @@ import play.mvc.Http;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.io.FileInputStream;
import javax.validation.constraints.*;
public class StoreApiControllerImp implements StoreApiControllerImpInterface {
@Override
public void deleteOrder(String orderId) throws Exception {
//Do your magic!!!
}
@Override
public Map<String, Integer> getInventory() throws Exception {
//Do your magic!!!
return new HashMap<String, Integer>();
}
@Override
public Order getOrderById(String orderId) throws Exception {
//Do your magic!!!
return new Order();
}
@Override
public Order placeOrder(Order body) throws Exception {
//Do your magic!!!
return new Order();

View File

@ -7,44 +7,53 @@ import play.mvc.Http;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.io.FileInputStream;
import javax.validation.constraints.*;
public class UserApiControllerImp implements UserApiControllerImpInterface {
@Override
public void createUser(User body) throws Exception {
//Do your magic!!!
}
@Override
public void createUsersWithArrayInput(List<User> body) throws Exception {
//Do your magic!!!
}
@Override
public void createUsersWithListInput(List<User> body) throws Exception {
//Do your magic!!!
}
@Override
public void deleteUser(String username) throws Exception {
//Do your magic!!!
}
@Override
public User getUserByName(String username) throws Exception {
//Do your magic!!!
return new User();
}
@Override
public String loginUser( String username, String password) throws Exception {
//Do your magic!!!
return new String();
}
@Override
public void logoutUser() throws Exception {
//Do your magic!!!
}
@Override
public void updateUser(String username, User body) throws Exception {
//Do your magic!!!