Merge remote-tracking branch 'origin/master' into 2.3.0

This commit is contained in:
wing328 2017-04-10 23:32:06 +08:00
commit 7662924ce1
14 changed files with 78 additions and 25 deletions

View File

@ -59,6 +59,7 @@ mvn clean compile
- `generateModelDocumentation` - generate the model documentation (`true` by default. Only available if `generateModels` is `true`)
- `generateSupportingFiles` - generate the supporting files (`true` by default)
- `supportingFilesToGenerate` - A comma separated list of supporting files to generate. All files is the default.
- `skip` - skip code generation (`false` by default. Can also be set globally through the `codegen.skip` property)
### Custom Generator

View File

@ -228,7 +228,11 @@ public class CodeGenMojo extends AbstractMojo {
@Parameter(name = "generateApiDocumentation", required = false)
private Boolean generateApiDocumentation = true;
/**
* Skip the execution.
*/
@Parameter(name = "skip", property = "codegen.skip", required = false, defaultValue = "false")
private Boolean skip;
/**
* Add the output directory to the project as a source root, so that the
@ -252,6 +256,14 @@ public class CodeGenMojo extends AbstractMojo {
@Override
public void execute() throws MojoExecutionException {
if(skip) {
getLog().info("Code generation is skipped.");
// Even when no new sources are generated, the existing ones should
// still be compiled if needed.
addCompileSourceRootIfConfigured();
return;
}
//attempt to read from config file
CodegenConfigurator configurator = CodegenConfigurator.fromFile(configurationFile);
@ -381,9 +393,12 @@ public class CodeGenMojo extends AbstractMojo {
}
}
Map<String, String> originalEnvironmentVariables = new HashMap<>();
if (environmentVariables != null) {
for(String key : environmentVariables.keySet()) {
originalEnvironmentVariables.put(key, System.getProperty(key));
String value = environmentVariables.get(key);
if(value == null) {
// don't put null values
@ -424,6 +439,10 @@ public class CodeGenMojo extends AbstractMojo {
throw new MojoExecutionException("Code generation failed. See above for the full exception.");
}
addCompileSourceRootIfConfigured();
}
private void addCompileSourceRootIfConfigured() {
if(addCompileSourceRoot) {
final Object sourceFolderObject = configOptions == null ? null : configOptions.get(CodegenConstants.SOURCE_FOLDER);
final String sourceFolder = sourceFolderObject == null ? "src/main/java" : sourceFolderObject.toString();
@ -431,5 +450,15 @@ public class CodeGenMojo extends AbstractMojo {
String sourceJavaFolder = output.toString() + "/" + sourceFolder;
project.addCompileSourceRoot(sourceJavaFolder);
}
// Reset all environment variables to their original value. This prevents unexpected behaviour
// when running the plugin multiple consecutive times with different configurations.
for(Map.Entry<String, String> entry : originalEnvironmentVariables.entrySet()) {
if(entry.getValue() == null) {
System.clearProperty(entry.getKey());
} else {
System.setProperty(entry.getKey(), entry.getValue());
}
}
}
}

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!!!