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

This commit is contained in:
wing328
2017-03-22 23:03:02 +08:00
44 changed files with 1249 additions and 592 deletions

View File

@@ -1,7 +1,11 @@
package io.swagger.codegen.languages;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.codegen.*;
import io.swagger.codegen.languages.features.BeanValidationFeatures;
import io.swagger.models.Model;
import io.swagger.models.Swagger;
import io.swagger.util.Json;
import java.io.File;
import java.util.List;
@@ -13,18 +17,16 @@ public class JavaPlayFrameworkCodegen extends AbstractJavaCodegen implements Bea
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";
public static final String USE_INTERFACES = "useInterfaces";
public static final String HANDLE_EXCEPTIONS = "handleExceptions";
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 useInterfaces = true;
protected boolean useBeanValidation = true;
protected boolean handleExceptions = true;
public JavaPlayFrameworkCodegen() {
super();
@@ -49,11 +51,12 @@ public class JavaPlayFrameworkCodegen extends AbstractJavaCodegen implements Bea
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"));
//Custom options for this generator
cliOptions.add(createBooleanCliWithDefault(CONTROLLER_ONLY, "Whether to generate only API interface stubs without the server files.", controllerOnly));
cliOptions.add(createBooleanCliWithDefault(USE_BEANVALIDATION, "Use BeanValidation API annotations", useBeanValidation));
cliOptions.add(createBooleanCliWithDefault(USE_INTERFACES, "Makes the controllerImp implements an interface to facilitate automatic completion when updating from version x to y of your spec", useInterfaces));
cliOptions.add(createBooleanCliWithDefault(HANDLE_EXCEPTIONS, "Add a wrapper to each controller to handle exceptions that pop in the controller", handleExceptions));
}
@Override
@@ -94,33 +97,38 @@ public class JavaPlayFrameworkCodegen extends AbstractJavaCodegen implements Bea
}
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()));
this.setControllerOnly(convertPropertyToBoolean(CONTROLLER_ONLY));
} else {
writePropertyBack(CONTROLLER_ONLY, controllerOnly);
}
if (additionalProperties.containsKey(USE_BEANVALIDATION)) {
this.setUseBeanValidation(convertPropertyToBoolean(USE_BEANVALIDATION));
}
if (useBeanValidation) {
} else {
writePropertyBack(USE_BEANVALIDATION, useBeanValidation);
}
if (additionalProperties.containsKey(USE_INTERFACES)) {
this.setUseInterfaces(convertPropertyToBoolean(USE_INTERFACES));
} else {
writePropertyBack(USE_INTERFACES, useInterfaces);
}
if (additionalProperties.containsKey(HANDLE_EXCEPTIONS)) {
this.setHandleExceptions(convertPropertyToBoolean(HANDLE_EXCEPTIONS));
} else {
writePropertyBack(HANDLE_EXCEPTIONS, handleExceptions);
}
//We don't use annotation anymore
importMapping.remove("ApiModelProperty");
importMapping.remove("ApiModel");
//Root folder
supportingFiles.add(new SupportingFile("README.mustache", "", "README"));
supportingFiles.add(new SupportingFile("LICENSE.mustache", "", "LICENSE"));
supportingFiles.add(new SupportingFile("build.mustache", "", "build.sbt"));
supportingFiles.add(new SupportingFile("swagger.mustache", "public", "swagger.json"));
//Project folder
supportingFiles.add(new SupportingFile("buildproperties.mustache", "project", "build.properties"));
@@ -133,6 +141,9 @@ public class JavaPlayFrameworkCodegen extends AbstractJavaCodegen implements Bea
//App/Utils folder
supportingFiles.add(new SupportingFile("swaggerUtils.mustache", "app/swagger", "SwaggerUtils.java"));
if (this.handleExceptions) {
supportingFiles.add(new SupportingFile("apiCall.mustache", "app/swagger", "ApiCall.java"));
}
//App/Controllers
supportingFiles.add(new SupportingFile("apiDocController.mustache", "app/controllers", "ApiDocController.java"));
@@ -143,6 +154,9 @@ public class JavaPlayFrameworkCodegen extends AbstractJavaCodegen implements Bea
if (!this.controllerOnly) {
apiTemplateFiles.put("newApi.mustache", "ControllerImp.java");
}
if (this.useInterfaces) {
apiTemplateFiles.put("newApiInterface.mustache", "ControllerImpInterface.java");
}
additionalProperties.put("javaVersion", "1.8");
additionalProperties.put("jdk8", "true");
@@ -150,17 +164,24 @@ public class JavaPlayFrameworkCodegen extends AbstractJavaCodegen implements Bea
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;
@Override
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
super.postProcessModelProperty(model, property);
//We don't use annotation anymore
model.imports.remove("ApiModelProperty");
model.imports.remove("ApiModel");
}
@Override
public CodegenModel fromModel(String name, Model model, Map<String, Model> allDefinitions) {
CodegenModel codegenModel = super.fromModel(name, model, allDefinitions);
if(codegenModel.description != null) {
codegenModel.imports.remove("ApiModel");
}
return codegenModel;
}
public void setTitle(String title) {
@@ -177,14 +198,16 @@ public class JavaPlayFrameworkCodegen extends AbstractJavaCodegen implements Bea
public void setControllerOnly(boolean controllerOnly) { this.controllerOnly = controllerOnly; }
public void setSingleContentTypes(boolean singleContentTypes) {
this.singleContentTypes = singleContentTypes;
public void setUseInterfaces(boolean useInterfaces) {
this.useInterfaces = useInterfaces;
}
public void setResponseWrapper(String responseWrapper) { this.responseWrapper = responseWrapper; }
public void setUseBeanValidation(boolean useBeanValidation) {
this.useBeanValidation = useBeanValidation;
}
public void setUseTags(boolean useTags) {
this.useTags = useTags;
public void setHandleExceptions(boolean handleExceptions) {
this.handleExceptions = handleExceptions;
}
@Override
@@ -242,7 +265,23 @@ public class JavaPlayFrameworkCodegen extends AbstractJavaCodegen implements Bea
return objs;
}
public void setUseBeanValidation(boolean useBeanValidation) {
this.useBeanValidation = useBeanValidation;
private CliOption createBooleanCliWithDefault(String optionName, String description, boolean defaultValue) {
CliOption defaultOption = CliOption.newBoolean(optionName, description);
defaultOption.setDefault(Boolean.toString(defaultValue));
return defaultOption;
}
@Override
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
Swagger swagger = (Swagger)objs.get("swagger");
System.out.println("swagger" + swagger.toString());
if(swagger != null) {
try {
objs.put("swagger-json", Json.pretty().writeValueAsString(swagger));
} catch (JsonProcessingException e) {
LOGGER.error(e.getMessage(), e);
}
}
return super.postProcessSupportingFileData(objs);
}
}