diff --git a/src/main/java/io/swagger/codegen/v3/generators/java/SpringCodegen.java b/src/main/java/io/swagger/codegen/v3/generators/java/SpringCodegen.java index 551fd10f5f..b42e16f2fe 100644 --- a/src/main/java/io/swagger/codegen/v3/generators/java/SpringCodegen.java +++ b/src/main/java/io/swagger/codegen/v3/generators/java/SpringCodegen.java @@ -161,9 +161,6 @@ public String getHelp() { @Override public void processOpts() { - setUseOas2(true); - additionalProperties.put(CodegenConstants.USE_OAS2, true); - // Process java8 option before common java ones to change the default dateLibrary to java8. if (additionalProperties.containsKey(JAVA8_MODE)) { this.setJava8(Boolean.valueOf(additionalProperties.get(JAVA8_MODE).toString())); @@ -302,7 +299,7 @@ public void processOpts() { if (!this.interfaceOnly) { - if (library.equals(DEFAULT_LIBRARY)) { + if (isDefaultLibrary()) { apiTestTemplateFiles.clear(); supportingFiles.add(new SupportingFile("homeController.mustache", (sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "HomeController.java")); @@ -313,7 +310,8 @@ public void processOpts() { supportingFiles.add(new SupportingFile("application.mustache", ("src.main.resources").replace(".", java.io.File.separator), "application.properties")); } - if (library.equals(SPRING_MVC_LIBRARY)) { + if (isSpringMvcLibrary()) { + forceOas2(); supportingFiles.add(new SupportingFile("webApplication.mustache", (sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "WebApplication.java")); supportingFiles.add(new SupportingFile("webMvcConfiguration.mustache", @@ -325,7 +323,8 @@ public void processOpts() { supportingFiles.add(new SupportingFile("application.properties", ("src.main.resources").replace(".", java.io.File.separator), "swagger.properties")); } - if (library.equals(SPRING_CLOUD_LIBRARY)) { + if (isSpringCloudLibrary()) { + forceOas2(); supportingFiles.add(new SupportingFile("apiKeyRequestInterceptor.mustache", (sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "ApiKeyRequestInterceptor.java")); supportingFiles.add(new SupportingFile("clientConfiguration.mustache", @@ -361,7 +360,7 @@ public void processOpts() { 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)) { + } else if ( this.swaggerDocketConfig && !isSpringCloudLibrary()) { supportingFiles.add(new SupportingFile("swaggerDocumentationConfig.mustache", (sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "SwaggerDocumentationConfig.java")); } @@ -372,7 +371,7 @@ public void processOpts() { if ("threetenbp".equals(dateLibrary)) { supportingFiles.add(new SupportingFile("customInstantDeserializer.mustache", (sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "CustomInstantDeserializer.java")); - if (library.equals(DEFAULT_LIBRARY) || library.equals(SPRING_CLOUD_LIBRARY)) { + if (isDefaultLibrary() || isSpringCloudLibrary()) { supportingFiles.add(new SupportingFile("jacksonConfiguration.mustache", (sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "JacksonConfiguration.java")); } @@ -447,7 +446,7 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio @Override public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map> operations) { - if((library.equals(DEFAULT_LIBRARY) || library.equals(SPRING_MVC_LIBRARY)) && !useTags) { + if((isDefaultLibrary() || isSpringMvcLibrary()) && !useTags) { String basePath = resourcePath; if (basePath.startsWith("/")) { basePath = basePath.substring(1); @@ -476,7 +475,7 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera @Override public String getArgumentsLocation() { - return null; + return "/arguments/spring.yaml"; } @Override @@ -550,6 +549,10 @@ public Map postProcessOperations(Map objs) { if ("0".equals(resp.code)) { resp.code = "200"; } + if (resp.baseType == null) { + // set vendorExtensions.x-java-is-response-void to true as baseType is set to "Void" + resp.vendorExtensions.put("x-java-is-response-void", true); + } doDataTypeAssignment(resp.dataType, new DataTypeAssigner() { @Override public void setReturnType(final String returnType) { @@ -687,9 +690,29 @@ private void removeHeadersFromContents(List contents) { } } + /** + * Forces Oas2 specification, use it when Oas3 is not supported. + */ + private void forceOas2() { + setUseOas2(true); + additionalProperties.put(CodegenConstants.USE_OAS2, true); + } + + private boolean isSpringCloudLibrary() { + return library.equals(SPRING_CLOUD_LIBRARY); + } + + private boolean isSpringMvcLibrary() { + return library.equals(SPRING_MVC_LIBRARY); + } + + private boolean isDefaultLibrary() { + return library.equals(DEFAULT_LIBRARY); + } + @Override public Map postProcessSupportingFileData(Map objs) { - if(library.equals(SPRING_CLOUD_LIBRARY)) { + if (isSpringCloudLibrary()) { List authMethods = (List) objs.get("authMethods"); if (authMethods != null) { for (CodegenSecurity authMethod : authMethods) { @@ -711,10 +734,10 @@ public String toApiName(String name) { @Override public String toApiTestFilename(String name) { - if(library.equals(SPRING_MVC_LIBRARY)) { + if (isSpringMvcLibrary()) { return toApiName(name) + "ControllerIT"; } - if(library.equals(SPRING_CLOUD_LIBRARY)) { + if (isSpringCloudLibrary()) { return toApiName(name) + "Test"; } return toApiName(name) + "ControllerIntegrationTest"; diff --git a/src/main/resources/arguments/spring.yaml b/src/main/resources/arguments/spring.yaml new file mode 100644 index 0000000000..b4ebb72748 --- /dev/null +++ b/src/main/resources/arguments/spring.yaml @@ -0,0 +1,4 @@ +arguments: + - option: "--use-oas2" + description: "use OpenAPI v2.0 (Swagger 1.5.x) annotations (by default, OpenAPI v3.0 is used)." + type: "boolean" diff --git a/src/main/resources/handlebars/JavaSpring/allowableValuesAndDefaultValue.mustache b/src/main/resources/handlebars/JavaSpring/allowableValuesAndDefaultValue.mustache new file mode 100644 index 0000000000..d3258544f5 --- /dev/null +++ b/src/main/resources/handlebars/JavaSpring/allowableValuesAndDefaultValue.mustache @@ -0,0 +1 @@ +{{#useOas2}}{{#allowableValues}}, allowableValues = "{{#values}}{{{.}}}{{^@last}}, {{/@last}}{{#@last}}{{/@last}}{{/values}}"{{/allowableValues}}{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}{{/useOas2}}{{^useOas2}}, schema=@Schema({{#allowableValues}}allowableValues={ {{#values}}"{{{.}}}"{{^@last}}, {{/@last}}{{/values}}{{^values}}{{/values}} }{{#min}}, minimum="{{.}}"{{/min}}{{#max}}, maximum="{{.}}"{{/max}}{{/allowableValues}}{{#defaultValue}}{{#allowableValues}}, {{/allowableValues}}defaultValue="{{{defaultValue}}}"{{/defaultValue}}){{/useOas2}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaSpring/api.mustache b/src/main/resources/handlebars/JavaSpring/api.mustache index 452d6cde3e..95f058e051 100644 --- a/src/main/resources/handlebars/JavaSpring/api.mustache +++ b/src/main/resources/handlebars/JavaSpring/api.mustache @@ -10,7 +10,19 @@ package {{package}}; {{#jdk8-no-delegate}} import com.fasterxml.jackson.databind.ObjectMapper; {{/jdk8-no-delegate}} +{{#useOas2}} import io.swagger.annotations.*; +{{/useOas2}} +{{^useOas2}} +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.media.ArraySchema; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +{{/useOas2}} {{#jdk8-no-delegate}} import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -53,8 +65,11 @@ import java.util.Optional; {{#async}} import java.util.concurrent.{{^jdk8}}Callable{{/jdk8}}{{#jdk8}}CompletableFuture{{/jdk8}}; {{/async}} + {{>generatedAnnotation}} -@Api(value = "{{{baseName}}}", description = "the {{{baseName}}} API") +{{#useOas2}} + @Api(value = "{{{baseName}}}", description = "the {{{baseName}}} API") +{{/useOas2}} {{#operations}} public interface {{classname}} { {{#jdk8}} @@ -81,6 +96,7 @@ public interface {{classname}} { {{#operation}} {{#contents}} + {{#useOas2}} @ApiOperation(value = "{{{summary}}}", nickname = "{{{operationId}}}", notes = "{{{notes}}}"{{#returnBaseType}}, response = {{{returnBaseType}}}.class{{/returnBaseType}}{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}}{{#hasAuthMethods}}, authorizations = { {{#authMethods}}@Authorization(value = "{{name}}"{{#isOAuth}}, scopes = { {{#each scopes}} @AuthorizationScope(scope = "{{@key}}", description = "{{this}}"){{^@last}},{{/@last}}{{/each}} @@ -96,13 +112,22 @@ public interface {{classname}} { {{/headerParams}} }) {{/implicitHeaders}} + {{/useOas2}} + {{^useOas2}} + @Operation(summary = "{{{summary}}}", description = "{{{notes}}}"{{#hasAuthMethods}}, security = { + {{#authMethods}}@SecurityRequirement(name = "{{name}}"{{#isOAuth}}, scopes = { {{#each scopes}}"{{@key}}"{{^@last}}, {{/@last}}{{/each}} }{{/isOAuth}}){{#hasMore}}, + {{/hasMore}}{{/authMethods}} +}{{/hasAuthMethods}}, tags={ {{#vendorExtensions.x-tags}}"{{tag}}"{{#hasMore}}, {{/hasMore}}{{/vendorExtensions.x-tags}} }) + @ApiResponses(value = { {{#responses}} + @ApiResponse(responseCode = "{{{code}}}", description = "{{{message}}}"{{^vendorExtensions.x-java-is-response-void}}, content = @Content({{^containerType}}schema = @Schema(implementation = {{{baseType}}}.class)){{/containerType}}{{#containerType}}array = @ArraySchema(schema = @Schema(implementation = {{{baseType}}}.class))){{/containerType}}{{/vendorExtensions.x-java-is-response-void}}){{#hasMore}},{{/hasMore}}{{/responses}} }) + {{/useOas2}} @RequestMapping(value = "{{{path}}}",{{#singleContentTypes}}{{#hasProduces}} produces = "{{{vendorExtensions.x-accepts}}}", {{/hasProduces}}{{#hasConsumes}} consumes = "{{{vendorExtensions.x-contentType}}}",{{/hasConsumes}}{{/singleContentTypes}}{{^singleContentTypes}}{{#hasProduces}} produces = { {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }, {{/hasProduces}}{{#hasConsumes}} - consumes = { {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} },{{/hasConsumes}}{{/singleContentTypes}} + consumes = { {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }, {{/hasConsumes}}{{/singleContentTypes}} method = RequestMethod.{{httpMethod}}) - {{#defaultInterfaces}}default {{/defaultInterfaces}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}({{#parameters}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>cookieParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}},{{/hasMore}}{{/parameters}}){{^defaultInterfaces}}{{#throwsException}} throws Exception{{/throwsException}};{{/defaultInterfaces}}{{#defaultInterfaces}}{{#throwsException}} throws Exception{{/throwsException}} { + {{#defaultInterfaces}}default {{/defaultInterfaces}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}({{#parameters}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>cookieParams}}{{>bodyParams}}{{>formParams}}{{#hasMore}}, {{/hasMore}}{{/parameters}}){{^defaultInterfaces}}{{#throwsException}} throws Exception{{/throwsException}};{{/defaultInterfaces}}{{#defaultInterfaces}}{{#throwsException}} throws Exception{{/throwsException}} { {{#delegate-method}} return {{operationId}}({{#parameters}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/parameters}}); } diff --git a/src/main/resources/handlebars/JavaSpring/apiController.mustache b/src/main/resources/handlebars/JavaSpring/apiController.mustache index 194d158b1b..d1bdb898b1 100644 --- a/src/main/resources/handlebars/JavaSpring/apiController.mustache +++ b/src/main/resources/handlebars/JavaSpring/apiController.mustache @@ -8,7 +8,19 @@ package {{package}}; import com.fasterxml.jackson.databind.ObjectMapper; {{/isDelegate}} {{#fullController}} +{{#useOas2}} import io.swagger.annotations.*; +{{/useOas2}} +{{^useOas2}} +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.media.ArraySchema; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +{{/useOas2}} import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; @@ -22,6 +34,9 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestPart; +{{^useOas2}} +import org.springframework.web.bind.annotation.RestController; +{{/useOas2}} import org.springframework.web.multipart.MultipartFile; {{#useBeanValidation}} @@ -50,8 +65,14 @@ import java.util.Map; import java.util.concurrent.Callable; {{/async}} {{/fullController}} + {{>generatedAnnotation}} +{{#useOas2}} @Controller +{{/useOas2}} +{{^useOas2}} +@RestController +{{/useOas2}} {{#operations}} public class {{classname}}Controller implements {{classname}} { diff --git a/src/main/resources/handlebars/JavaSpring/apiDelegate.mustache b/src/main/resources/handlebars/JavaSpring/apiDelegate.mustache index de0a9d809b..cabc81d4e6 100644 --- a/src/main/resources/handlebars/JavaSpring/apiDelegate.mustache +++ b/src/main/resources/handlebars/JavaSpring/apiDelegate.mustache @@ -5,7 +5,19 @@ package {{package}}; {{#jdk8}} import com.fasterxml.jackson.databind.ObjectMapper; {{/jdk8}} +{{#useOas2}} import io.swagger.annotations.*; +{{/useOas2}} +{{^useOas2}} +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.media.ArraySchema; +import io.swagger.v3.oas.annotations.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.security.SecurityRequirement; +{{/useOas2}} {{#jdk8}} import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/resources/handlebars/JavaSpring/apiException.mustache b/src/main/resources/handlebars/JavaSpring/apiException.mustache index f616114770..7eb23b9e9b 100644 --- a/src/main/resources/handlebars/JavaSpring/apiException.mustache +++ b/src/main/resources/handlebars/JavaSpring/apiException.mustache @@ -1,7 +1,7 @@ package {{apiPackage}}; {{>generatedAnnotation}} -public class ApiException extends Exception{ +public class ApiException extends Exception { private int code; public ApiException (int code, String msg) { super(msg); diff --git a/src/main/resources/handlebars/JavaSpring/application.mustache b/src/main/resources/handlebars/JavaSpring/application.mustache index e686a5a0c3..bcf6c9355b 100644 --- a/src/main/resources/handlebars/JavaSpring/application.mustache +++ b/src/main/resources/handlebars/JavaSpring/application.mustache @@ -1,5 +1,7 @@ +{{#useOas2}} springfox.documentation.swagger.v2.path=/api-docs server.contextPath={{^contextPath}}/{{/contextPath}}{{#contextPath}}{{contextPath}}{{/contextPath}} +{{/useOas2}} server.port={{serverPort}} spring.jackson.date-format={{basePackage}}.RFC3339DateFormat spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaSpring/bodyParams.mustache b/src/main/resources/handlebars/JavaSpring/bodyParams.mustache index 7fef9ad2b6..3a837966d3 100644 --- a/src/main/resources/handlebars/JavaSpring/bodyParams.mustache +++ b/src/main/resources/handlebars/JavaSpring/bodyParams.mustache @@ -1 +1 @@ -{{#isBodyParam}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}} {{^isContainer}}{{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{/isContainer}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestBody {{{dataType}}} {{paramName}}{{/isBodyParam}} \ No newline at end of file +{{#isBodyParam}}{{#useOas2}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}} {{^isContainer}}{{#allowableValues}}, allowableValues="{{{allowableValues}}}"{{/allowableValues}}{{/isContainer}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}){{/useOas2}}{{^useOas2}}@Parameter(description = "{{{description}}}" {{#required}},required=true{{/required}}{{>allowableValuesAndDefaultValue}}){{/useOas2}}{{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestBody {{{dataType}}} {{paramName}}{{/isBodyParam}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaSpring/cookieParams.mustache b/src/main/resources/handlebars/JavaSpring/cookieParams.mustache index 5edbd218e7..fa3d66f895 100644 --- a/src/main/resources/handlebars/JavaSpring/cookieParams.mustache +++ b/src/main/resources/handlebars/JavaSpring/cookieParams.mustache @@ -1 +1 @@ -{{#isCookieParam}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}}{{#allowableValues}}, allowableValues="{{#values}}{{{.}}}{{^@last}}, {{/@last}}{{#@last}}{{/@last}}{{/values}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @CookieValue(value="{{baseName}}", required={{#required}}true{{/required}}{{^required}}false{{/required}}) {{>optionalDataType}} {{paramName}}{{/isCookieParam}} \ No newline at end of file +{{#isCookieParam}}{{#useOas2}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}}{{>allowableValuesAndDefaultValue}}){{/useOas2}}{{^useOas2}}@Parameter(description = "{{{description}}}" {{#required}},required=true{{/required}}{{>allowableValuesAndDefaultValue}}){{/useOas2}}@CookieValue(value="{{baseName}}", required={{#required}}true{{/required}}{{^required}}false{{/required}}) {{>optionalDataType}} {{paramName}}{{/isCookieParam}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaSpring/formParams.mustache b/src/main/resources/handlebars/JavaSpring/formParams.mustache index b91da48cfe..597a539807 100644 --- a/src/main/resources/handlebars/JavaSpring/formParams.mustache +++ b/src/main/resources/handlebars/JavaSpring/formParams.mustache @@ -1 +1 @@ -{{#isFormParam}}{{^isBinary}}@ApiParam(value = "{{{description}}}"{{#required}}, required=true{{/required}}{{#allowableValues}}, allowableValues="{{#values}}{{{.}}}{{^@last}}, {{/@last}}{{#@last}}{{/@last}}{{/values}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @RequestParam(value="{{baseName}}"{{#required}}, required=true{{/required}}{{^required}}, required=false{{/required}}) {{{dataType}}} {{paramName}}{{/isBinary}}{{#isBinary}}@ApiParam(value = "file detail") {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestPart("file") MultipartFile {{baseName}}{{/isBinary}}{{/isFormParam}} +{{#isFormParam}}{{#useOas2}}{{^isBinary}}@ApiParam(value = "{{{description}}}"{{#required}}, required=true{{/required}}{{>allowableValuesAndDefaultValue}}) @RequestParam(value="{{baseName}}"{{#required}}, required=true{{/required}}{{^required}}, required=false{{/required}}) {{{dataType}}} {{paramName}}{{/isBinary}}{{#isBinary}}@ApiParam(value = "file detail") {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestPart("file") MultipartFile {{baseName}}{{/isBinary}}{{/useOas2}}{{^useOas2}}{{^isBinary}}@Parameter(description = "{{{description}}}" {{#required}},required=true{{/required}}{{>allowableValuesAndDefaultValue}}) @RequestParam(value="{{baseName}}"{{#required}}, required=true{{/required}}{{^required}}, required=false{{/required}}) {{{dataType}}} {{paramName}}{{/isBinary}}{{#isBinary}}@Parameter(description = "file detail") @RequestPart("file") MultipartFile {{baseName}}{{/isBinary}}{{/useOas2}}{{/isFormParam}} diff --git a/src/main/resources/handlebars/JavaSpring/headerParams.mustache b/src/main/resources/handlebars/JavaSpring/headerParams.mustache index 2c8aa49d7d..fad7f902b2 100644 --- a/src/main/resources/handlebars/JavaSpring/headerParams.mustache +++ b/src/main/resources/handlebars/JavaSpring/headerParams.mustache @@ -1 +1 @@ -{{#isHeaderParam}}@ApiParam(value = "{{{description}}}" {{#required}},required=true{{/required}}{{#allowableValues}}, allowableValues="{{#values}}{{{.}}}{{^@last}}, {{/@last}}{{#@last}}{{/@last}}{{/values}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @RequestHeader(value="{{baseName}}", required={{#required}}true{{/required}}{{^required}}false{{/required}}) {{>optionalDataType}} {{paramName}}{{/isHeaderParam}} \ No newline at end of file +{{#isHeaderParam}}{{#useOas2}}@ApiParam(value = "{{{description}}}" {{#required}}, required=true{{/required}}{{>allowableValuesAndDefaultValue}}){{/useOas2}}{{^useOas2}}@Parameter(description = "{{description}}" {{#required}},required=true{{/required}}{{>allowableValuesAndDefaultValue}}){{/useOas2}}@RequestHeader(value="{{baseName}}", required={{#required}}true{{/required}}{{^required}}false{{/required}}) {{>optionalDataType}} {{paramName}}{{/isHeaderParam}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaSpring/implicitHeader.mustache b/src/main/resources/handlebars/JavaSpring/implicitHeader.mustache index 64d7af2080..7036a3a456 100644 --- a/src/main/resources/handlebars/JavaSpring/implicitHeader.mustache +++ b/src/main/resources/handlebars/JavaSpring/implicitHeader.mustache @@ -1 +1 @@ -{{#isHeaderParam}}@ApiImplicitParam(name = "{{{paramName}}}", value = "{{{description}}}", {{#required}}required=true,{{/required}} dataType = "{{{dataType}}}", paramType = "header"){{#hasMore}},{{/hasMore}}{{/isHeaderParam}} \ No newline at end of file +{{#isHeaderParam}}{{#useOas2}}@ApiImplicitParam(name = "{{{paramName}}}", value = "{{{description}}}", {{#required}}required=true,{{/required}} dataType = "{{{dataType}}}", paramType = "header"){{#hasMore}},{{/hasMore}}{{/useOas2}}{{/isHeaderParam}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/README.mustache b/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/README.mustache index 02d932b8ac..1a4f81a4f7 100644 --- a/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/README.mustache +++ b/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/README.mustache @@ -6,9 +6,9 @@ Spring Boot Server ## Overview This server was generated by the [swagger-codegen](https://github.com/swagger-api/swagger-codegen) project. By using the [OpenAPI-Spec](https://github.com/swagger-api/swagger-core), you can easily generate a server stub. -This is an example of building a swagger-enabled server in Java using the SpringBoot framework. +This is an example of building a swagger-enabled server in Java using the SpringBoot framework. -The underlying library integrating swagger to SpringBoot is [springfox](https://github.com/springfox/springfox) +The underlying library integrating swagger to SpringBoot is {{#useOas2}}[springfox](https://github.com/springfox/springfox){{/useOas2}}{{^useOas2}}[springdoc-openapi](https://github.com/springdoc/springdoc-openapi){{/useOas2}} Start your server as an simple java application diff --git a/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/pom.mustache b/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/pom.mustache index 0ca494d631..8906656dba 100644 --- a/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/pom.mustache +++ b/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/pom.mustache @@ -9,7 +9,12 @@ {{#java8}}1.8{{/java8}}{{^java8}}1.7{{/java8}} ${java.version} ${java.version} + {{#useOas2}} 2.9.2 + {{/useOas2}} + {{^useOas2}} + 1.2.9 + {{/useOas2}} org.springframework.boot @@ -43,6 +48,7 @@ org.springframework.boot spring-boot-starter-tomcat + {{#useOas2}} io.springfox @@ -53,7 +59,16 @@ io.springfox springfox-swagger-ui ${springfox-version} + {{/useOas2}} + {{^useOas2}} + + + org.springdoc + springdoc-openapi-ui + ${springdoc-version} + {{/useOas2}} + {{#withXml}} @@ -85,13 +100,14 @@ 2.6.4 {{/threetenbp}} -{{#useBeanValidation}} - + {{#useBeanValidation}} + + javax.validation validation-api -{{/useBeanValidation}} + {{/useBeanValidation}} {{#notNullJacksonAnnotation}} com.fasterxml.jackson.core diff --git a/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/swagger2SpringBoot.mustache b/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/swagger2SpringBoot.mustache index d329e337fb..4d7d420845 100644 --- a/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/swagger2SpringBoot.mustache +++ b/src/main/resources/handlebars/JavaSpring/libraries/spring-boot/swagger2SpringBoot.mustache @@ -6,10 +6,14 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; +{{#useOas2}} import springfox.documentation.swagger2.annotations.EnableSwagger2; +{{/useOas2}} @SpringBootApplication +{{#useOas2}} @EnableSwagger2 +{{/useOas2}} @ComponentScan(basePackages = { "{{basePackage}}", "{{apiPackage}}" , "{{configPackage}}"}) public class Swagger2SpringBoot implements CommandLineRunner { diff --git a/src/main/resources/handlebars/JavaSpring/pathParams.mustache b/src/main/resources/handlebars/JavaSpring/pathParams.mustache index 6cc92cfe47..b6699ebfb9 100644 --- a/src/main/resources/handlebars/JavaSpring/pathParams.mustache +++ b/src/main/resources/handlebars/JavaSpring/pathParams.mustache @@ -1 +1 @@ -{{#isPathParam}}{{#useBeanValidation}}{{>beanValidationPathParams}}{{/useBeanValidation}}@ApiParam(value = "{{{description}}}"{{#required}},required=true{{/required}}{{#allowableValues}}, allowableValues="{{#values}}{{{.}}}{{^@last}}, {{/@last}}{{#@last}}{{/@last}}{{/values}}"{{/allowableValues}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) @PathVariable("{{baseName}}") {{>optionalDataType}} {{paramName}}{{/isPathParam}} \ No newline at end of file +{{#isPathParam}}{{#useBeanValidation}}{{>beanValidationPathParams}}{{/useBeanValidation}}{{#useOas2}}@ApiParam(value = "{{{description}}}"{{#required}},required=true{{/required}}{{>allowableValuesAndDefaultValue}}) @PathVariable("{{baseName}}") {{>optionalDataType}} {{paramName}}{{/useOas2}}{{^useOas2}}@Parameter(description = "{{{description}}}"{{#required}},required=true{{/required}}{{>allowableValuesAndDefaultValue}}) @PathVariable("{{baseName}}") {{>optionalDataType}} {{paramName}}{{/useOas2}}{{/isPathParam}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaSpring/pojo.mustache b/src/main/resources/handlebars/JavaSpring/pojo.mustache index cfd909946c..6d98e4db86 100644 --- a/src/main/resources/handlebars/JavaSpring/pojo.mustache +++ b/src/main/resources/handlebars/JavaSpring/pojo.mustache @@ -1,7 +1,9 @@ /** * {{#description}}{{.}}{{/description}}{{^description}}{{classname}}{{/description}} */{{#description}} -@ApiModel(description = "{{{description}}}"){{/description}} +{{#useOas2}}@ApiModel(description = "{{{description}}}"){{/useOas2}} +{{^useOas2}}@Schema(description = "{{{description}}}"){{/useOas2}} +{{/description}} {{#useBeanValidation}}@Validated{{/useBeanValidation}} {{>generatedAnnotation}}{{#discriminator}}{{>typeInfoAnnotation}}{{/discriminator}}{{>xmlAnnotation}} {{#notNullJacksonAnnotation}}@JsonInclude(JsonInclude.Include.NON_NULL){{/notNullJacksonAnnotation}} @@ -89,12 +91,14 @@ public class {{classname}} {{#parent}}extends {{{parent}}}{{/parent}} {{#seriali * maximum: {{maximum}} {{/maximum}} * @return {{name}} - **/ + **/ {{#vendorExtensions.extraAnnotation}} {{{vendorExtensions.extraAnnotation}}} {{/vendorExtensions.extraAnnotation}} - @ApiModelProperty({{#example}}example = "{{{example}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}") - {{#useBeanValidation}}{{>beanValidation}}{{/useBeanValidation}} public {{{datatypeWithEnum}}} {{getter}}() { + {{#useOas2}}@ApiModelProperty({{#example}}example = "{{{example}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}value = "{{{description}}}"){{/useOas2}}{{^useOas2}}@Schema({{#example}}example = "{{{example}}}", {{/example}}{{#required}}required = {{required}}, {{/required}}{{#isReadOnly}}readOnly = {{{isReadOnly}}}, {{/isReadOnly}}description = "{{{description}}}"){{/useOas2}} +{{#useBeanValidation}}{{>beanValidation}}{{/useBeanValidation}} + + public {{{datatypeWithEnum}}} {{getter}}() { return {{name}}; } diff --git a/src/main/resources/handlebars/JavaSpring/queryParams.mustache b/src/main/resources/handlebars/JavaSpring/queryParams.mustache index 007d82fb2d..b93cb8aee4 100644 --- a/src/main/resources/handlebars/JavaSpring/queryParams.mustache +++ b/src/main/resources/handlebars/JavaSpring/queryParams.mustache @@ -1 +1 @@ -{{#isQueryParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}@ApiParam(value = "{{{description}}}"{{#required}}, required = true{{/required}}{{#allowableValues}}, allowableValues = "{{#values}}{{{.}}}{{^@last}}, {{/@last}}{{#@last}}{{/@last}}{{/values}}"{{/allowableValues}}{{#defaultValue}}, defaultValue = "{{{defaultValue}}}"{{/defaultValue}}) {{#useBeanValidation}}@Valid{{/useBeanValidation}} @RequestParam(value = "{{baseName}}"{{#required}}, required = true{{/required}}{{^required}}, required = false{{/required}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) {{>optionalDataType}} {{paramName}}{{/isQueryParam}} \ No newline at end of file +{{#isQueryParam}}{{#useBeanValidation}}{{>beanValidationQueryParams}}{{/useBeanValidation}}{{#useOas2}}@ApiParam(value = "{{{description}}}"{{#required}}, required = true{{/required}}{{>allowableValuesAndDefaultValue}}) {{#useBeanValidation}}@Valid{{/useBeanValidation}}{{/useOas2}}{{^useOas2}}@Parameter(description = "{{{description}}}"{{#required}},required=true{{/required}}{{>allowableValuesAndDefaultValue}}) {{#defaultValue}}{{/defaultValue}}{{/useOas2}} @RequestParam(value = "{{baseName}}"{{#required}}, required = true{{/required}}{{^required}}, required = false{{/required}}{{#defaultValue}}, defaultValue="{{{defaultValue}}}"{{/defaultValue}}) {{>optionalDataType}} {{paramName}}{{/isQueryParam}} \ No newline at end of file diff --git a/src/main/resources/handlebars/JavaSpring/swaggerDocumentationConfig.mustache b/src/main/resources/handlebars/JavaSpring/swaggerDocumentationConfig.mustache index a0b2cded91..7464ebd208 100644 --- a/src/main/resources/handlebars/JavaSpring/swaggerDocumentationConfig.mustache +++ b/src/main/resources/handlebars/JavaSpring/swaggerDocumentationConfig.mustache @@ -3,12 +3,20 @@ package {{configPackage}}; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +{{#useOas2}} import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; +{{/useOas2}} +{{^useOas2}} +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Contact; +import io.swagger.v3.oas.models.info.Info; +import io.swagger.v3.oas.models.info.License; +{{/useOas2}} {{#useOptional}} import java.util.Optional; {{/useOptional}} @@ -17,6 +25,7 @@ import java.util.Optional; @Configuration public class SwaggerDocumentationConfig { +{{#useOas2}} ApiInfo apiInfo() { return new ApiInfoBuilder() .title("{{appName}}") @@ -52,5 +61,22 @@ public class SwaggerDocumentationConfig { {{/useOptional}} .apiInfo(apiInfo()); } +{{/useOas2}} +{{^useOas2}} + @Bean + public OpenAPI configure() { + return new OpenAPI() + .info(new Info() + .title("{{appName}}") + .description("{{{appDescription}}}") + .termsOfService("{{infoUrl}}") + .version("{{appVersion}}") + .license(new License() + .name("{{licenseInfo}}") + .url("{{licenseUrl}}")) + .contact(new Contact() + .email("{{infoEmail}}"))); + } +{{/useOas2}} } diff --git a/src/test/java/io/swagger/codegen/v3/generators/java/SpringGeneratorCodegenTest.java b/src/test/java/io/swagger/codegen/v3/generators/java/SpringGeneratorCodegenTest.java index d71ccb3fc4..92714d7bf0 100644 --- a/src/test/java/io/swagger/codegen/v3/generators/java/SpringGeneratorCodegenTest.java +++ b/src/test/java/io/swagger/codegen/v3/generators/java/SpringGeneratorCodegenTest.java @@ -32,8 +32,9 @@ public void testParameterOrders() throws Exception { final String content = FileUtils.readFileToString(petControllerFile); - Assert.assertTrue(content.contains("ResponseEntity updateTest(@ApiParam(value = \"description\",required=true) @PathVariable(\"id\") Long id" + System.lineSeparator() + - ",@ApiParam(value = \"Localized Text object containing updated data.\" ,required=true ) @Valid @RequestBody LocalizedText body" + System.lineSeparator() + + + Assert.assertTrue(content.contains("ResponseEntity updateTest(@Parameter(description = \"description\",required=true, schema=@Schema()) @PathVariable(\"id\") Long id" + System.lineSeparator() + + ", @Parameter(description = \"Localized Text object containing updated data.\" ,required=true, schema=@Schema())@Valid @RequestBody LocalizedText body" + System.lineSeparator() + ");")); this.folder.delete();