[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_CLOUD_LIBRARY = "spring-cloud";
public static final String IMPLICIT_HEADERS = "implicitHeaders";
public static final String SWAGGER_DOCKET_CONFIG = "swaggerDocketConfig";
protected String title = "swagger-petstore";
protected String configPackage = "io.swagger.configuration";
@ -43,6 +44,7 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
protected boolean useTags = false;
protected boolean useBeanValidation = true;
protected boolean implicitHeaders = false;
protected boolean swaggerDocketConfig = false;
public SpringCodegen() {
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_BEANVALIDATION, "Use BeanValidation API annotations"));
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(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()));
}
if (additionalProperties.containsKey(SWAGGER_DOCKET_CONFIG)) {
this.setSwaggerDocketConfig(Boolean.valueOf(additionalProperties.get(SWAGGER_DOCKET_CONFIG).toString()));
}
typeMapping.put("file", "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) {
throw new IllegalArgumentException(
String.format("Can not generate code with `%s` and `%s` both true.", DELEGATE_PATTERN, INTERFACE_ONLY));
}
if (!this.interfaceOnly) {
supportingFiles.add(new SupportingFile("pom.mustache", "", "pom.xml"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
if (library.equals(DEFAULT_LIBRARY)) {
supportingFiles.add(new SupportingFile("homeController.mustache",
(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",
(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)) {
@ -586,6 +596,10 @@ public class SpringCodegen extends AbstractJavaCodegen implements BeanValidation
this.implicitHeaders = implicitHeaders;
}
public void setSwaggerDocketConfig(boolean swaggerDocketConfig) {
this.swaggerDocketConfig = swaggerDocketConfig;
}
@Override
public void postProcessModelProperty(CodegenModel model, CodegenProperty 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_BEANVALIDATION = "false";
public static final String IMPLICIT_HEADERS = "false";
public static final String SWAGGER_DOCKET_CONFIG = "false";
@Override
public String getLanguage() {
@ -42,6 +43,7 @@ public class SpringOptionsProvider extends JavaOptionsProvider {
options.put(SpringCodegen.USE_TAGS, USE_TAGS);
options.put(SpringCodegen.USE_BEANVALIDATION, USE_BEANVALIDATION);
options.put(SpringCodegen.IMPLICIT_HEADERS, IMPLICIT_HEADERS);
options.put(SpringCodegen.SWAGGER_DOCKET_CONFIG, SWAGGER_DOCKET_CONFIG);
return options;
}

View File

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

View File

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