[spring] improve build-time code generation, for CI and maven integration (#5545)

* Do not generate pom.xml and README.md when interfaceOnly=true #5542

* New swaggerDocketConfig option, to generate Spring configuration class for Swagger Docket bean. Ignored when interfaceOnly=false or library=spring-cloud #5542

* Updated tests running ./bin/spring-all-petstore.sh

* Run ./bin/spring-all-petstore.sh
This commit is contained in:
Lorenzo Nicora 2017-06-15 08:37:10 +01:00 committed by wing328
parent c4f7c4f3f9
commit 01100d56f0
4 changed files with 24 additions and 6 deletions

View File

@ -30,6 +30,7 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
public static final String SPRING_MVC_LIBRARY = "spring-mvc"; public static final String SPRING_MVC_LIBRARY = "spring-mvc";
public static final String SPRING_CLOUD_LIBRARY = "spring-cloud"; public static final String SPRING_CLOUD_LIBRARY = "spring-cloud";
public static final String IMPLICIT_HEADERS = "implicitHeaders"; public static final String IMPLICIT_HEADERS = "implicitHeaders";
public static final String SWAGGER_DOCKET_CONFIG = "swaggerDocketConfig";
protected String title = "swagger-petstore"; protected String title = "swagger-petstore";
protected String configPackage = "io.swagger.configuration"; protected String configPackage = "io.swagger.configuration";
@ -43,6 +44,7 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
protected boolean useTags = false; protected boolean useTags = false;
protected boolean useBeanValidation = true; protected boolean useBeanValidation = true;
protected boolean implicitHeaders = false; protected boolean implicitHeaders = false;
protected boolean swaggerDocketConfig = false;
public SpringCodegen() { public SpringCodegen() {
super(); super();
@ -72,6 +74,7 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
cliOptions.add(CliOption.newBoolean(USE_TAGS, "use tags for creating interface and controller classnames")); cliOptions.add(CliOption.newBoolean(USE_TAGS, "use tags for creating interface and controller classnames"));
cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations")); cliOptions.add(CliOption.newBoolean(USE_BEANVALIDATION, "Use BeanValidation API annotations"));
cliOptions.add(CliOption.newBoolean(IMPLICIT_HEADERS, "Use of @ApiImplicitParams for headers.")); cliOptions.add(CliOption.newBoolean(IMPLICIT_HEADERS, "Use of @ApiImplicitParams for headers."));
cliOptions.add(CliOption.newBoolean(SWAGGER_DOCKET_CONFIG, "Generate Spring Swagger Docket configuration class."));
supportedLibraries.put(DEFAULT_LIBRARY, "Spring-boot Server application using the SpringFox integration."); supportedLibraries.put(DEFAULT_LIBRARY, "Spring-boot Server application using the SpringFox integration.");
supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration."); supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration.");
@ -176,18 +179,22 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
this.setImplicitHeaders(Boolean.valueOf(additionalProperties.get(IMPLICIT_HEADERS).toString())); this.setImplicitHeaders(Boolean.valueOf(additionalProperties.get(IMPLICIT_HEADERS).toString()));
} }
if (additionalProperties.containsKey(SWAGGER_DOCKET_CONFIG)) {
this.setSwaggerDocketConfig(Boolean.valueOf(additionalProperties.get(SWAGGER_DOCKET_CONFIG).toString()));
}
typeMapping.put("file", "Resource"); typeMapping.put("file", "Resource");
importMapping.put("Resource", "org.springframework.core.io.Resource"); importMapping.put("Resource", "org.springframework.core.io.Resource");
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
if (this.interfaceOnly && this.delegatePattern) { if (this.interfaceOnly && this.delegatePattern) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
String.format("Can not generate code with `%s` and `%s` both true.", DELEGATE_PATTERN, INTERFACE_ONLY)); String.format("Can not generate code with `%s` and `%s` both true.", DELEGATE_PATTERN, INTERFACE_ONLY));
} }
if (!this.interfaceOnly) { if (!this.interfaceOnly) {
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
if (library.equals(DEFAULT_LIBRARY)) { if (library.equals(DEFAULT_LIBRARY)) {
supportingFiles.add(new SupportingFile("homeController.mustache", supportingFiles.add(new SupportingFile("homeController.mustache",
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "HomeController.java")); (sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "HomeController.java"));
@ -235,6 +242,9 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
supportingFiles.add(new SupportingFile("swaggerDocumentationConfig.mustache", supportingFiles.add(new SupportingFile("swaggerDocumentationConfig.mustache",
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "SwaggerDocumentationConfig.java")); (sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "SwaggerDocumentationConfig.java"));
} }
} else if ( this.swaggerDocketConfig && !library.equals(SPRING_CLOUD_LIBRARY)) {
supportingFiles.add(new SupportingFile("swaggerDocumentationConfig.mustache",
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "SwaggerDocumentationConfig.java"));
} }
if ("threetenbp".equals(dateLibrary)) { if ("threetenbp".equals(dateLibrary)) {
@ -586,6 +596,10 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
this.implicitHeaders = implicitHeaders; this.implicitHeaders = implicitHeaders;
} }
public void setSwaggerDocketConfig(boolean swaggerDocketConfig) {
this.swaggerDocketConfig = swaggerDocketConfig;
}
@Override @Override
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) { public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
super.postProcessModelProperty(model, property); super.postProcessModelProperty(model, property);

View File

@ -20,6 +20,7 @@ public class SpringOptionsProvider extends JavaOptionsProvider {
public static final String USE_TAGS = "useTags"; public static final String USE_TAGS = "useTags";
public static final String USE_BEANVALIDATION = "false"; public static final String USE_BEANVALIDATION = "false";
public static final String IMPLICIT_HEADERS = "false"; public static final String IMPLICIT_HEADERS = "false";
public static final String SWAGGER_DOCKET_CONFIG = "false";
@Override @Override
public String getLanguage() { public String getLanguage() {
@ -42,6 +43,7 @@ public class SpringOptionsProvider extends JavaOptionsProvider {
options.put(SpringCodegen.USE_TAGS, USE_TAGS); options.put(SpringCodegen.USE_TAGS, USE_TAGS);
options.put(SpringCodegen.USE_BEANVALIDATION, USE_BEANVALIDATION); options.put(SpringCodegen.USE_BEANVALIDATION, USE_BEANVALIDATION);
options.put(SpringCodegen.IMPLICIT_HEADERS, IMPLICIT_HEADERS); options.put(SpringCodegen.IMPLICIT_HEADERS, IMPLICIT_HEADERS);
options.put(SpringCodegen.SWAGGER_DOCKET_CONFIG, SWAGGER_DOCKET_CONFIG);
return options; return options;
} }

View File

@ -75,6 +75,8 @@ public class SpringOptionsTest extends JavaClientOptionsTest {
times = 1; times = 1;
clientCodegen.setImplicitHeaders(Boolean.valueOf(SpringOptionsProvider.IMPLICIT_HEADERS)); clientCodegen.setImplicitHeaders(Boolean.valueOf(SpringOptionsProvider.IMPLICIT_HEADERS));
times = 1; times = 1;
clientCodegen.setSwaggerDocketConfig(Boolean.valueOf(SpringOptionsProvider.SWAGGER_DOCKET_CONFIG));
times = 1;
}}; }};
} }
} }

View File

@ -34,7 +34,7 @@ public class ModelApiResponse {
**/ **/
@ApiModelProperty(value = "") @ApiModelProperty(value = "")
@Valid
public Integer getCode() { public Integer getCode() {
return code; return code;
} }
@ -54,7 +54,7 @@ public class ModelApiResponse {
**/ **/
@ApiModelProperty(value = "") @ApiModelProperty(value = "")
@Valid
public String getType() { public String getType() {
return type; return type;
} }
@ -74,7 +74,7 @@ public class ModelApiResponse {
**/ **/
@ApiModelProperty(value = "") @ApiModelProperty(value = "")
@Valid
public String getMessage() { public String getMessage() {
return message; return message;
} }