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, public String baseName, paramName, dataType, datatypeWithEnum, dataFormat,
collectionFormat, description, unescapedDescription, baseType, defaultValue, enumName; 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 example; // example value (x-example)
public String jsonSchema; public String jsonSchema;
public boolean isString, isInteger, isLong, isFloat, isDouble, isByteArray, isBinary, isBoolean, isDate, isDateTime; 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"); typeMapping.put("DateTime", "OffsetDateTime");
importMapping.put("LocalDate", "java.time.LocalDate"); importMapping.put("LocalDate", "java.time.LocalDate");
importMapping.put("OffsetDateTime", "java.time.OffsetDateTime"); importMapping.put("OffsetDateTime", "java.time.OffsetDateTime");
importMapping.put("InputStream", "java.io.InputStream");
typeMapping.put("file", "InputStream");
} }
@Override @Override
@ -249,18 +252,16 @@ public class JavaPlayFrameworkCodegen extends AbstractJavaCodegen implements Bea
List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation"); List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
for (CodegenOperation operation : ops) { 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) { for (CodegenParameter param : operation.allParams) {
if (!param.isPathParam ) { if (param.isFormParam && param.isFile) {
if (!param.isPrimitiveType && !param.isListContainer && !param.isMapContainer) { param.dataType = "Http.MultipartFormData.FilePart";
param.dataTypeForImplicitParam = String.format("%s.%s", modelPackage, param.dataType);
} else {
param.dataTypeForImplicitParam = param.dataType;
} }
} }
for (CodegenParameter param : operation.formParams) {
if (param.isFile) {
param.dataType = "Http.MultipartFormData.FilePart";
}
} }
if (operation.path.contains("{")) { 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.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.io.FileInputStream;
{{#useBeanValidation}} {{#useBeanValidation}}
import javax.validation.constraints.*; import javax.validation.constraints.*;
{{/useBeanValidation}} {{/useBeanValidation}}
@ -14,9 +15,10 @@ import javax.validation.constraints.*;
{{#operations}} {{#operations}}
public class {{classname}}ControllerImp {{#useInterfaces}}implements {{classname}}ControllerImpInterface{{/useInterfaces}} { public class {{classname}}ControllerImp {{#useInterfaces}}implements {{classname}}ControllerImpInterface{{/useInterfaces}} {
{{#operation}} {{#operation}}
{{#useInterfaces}}@Override{{/useInterfaces}}
public {{>returnTypes}} {{operationId}}({{#allParams}}{{>pathParams}}{{>queryParams}}{{>bodyParams}}{{>formParams}}{{>headerParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {{#handleExceptions}}throws Exception{{/handleExceptions}} { public {{>returnTypes}} {{operationId}}({{#allParams}}{{>pathParams}}{{>queryParams}}{{>bodyParams}}{{>formParams}}{{>headerParams}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) {{#handleExceptions}}throws Exception{{/handleExceptions}} {
//Do your magic!!! //Do your magic!!!
{{#returnType}}return new {{>returnTypesNoVoidNoAbstract}}();{{/returnType}} {{#returnType}}{{#isResponseFile}}return new FileInputStream("replace this");{{/isResponseFile}}{{^isResponseFile}}return new {{>returnTypesNoVoidNoAbstract}}();{{/isResponseFile}}{{/returnType}}
} }
{{/operation}} {{/operation}}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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