Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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"));
Expand All @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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"));
}
Expand All @@ -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"));
}
Expand Down Expand Up @@ -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<String, List<CodegenOperation>> 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);
Expand Down Expand Up @@ -476,7 +475,7 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera

@Override
public String getArgumentsLocation() {
return null;
return "/arguments/spring.yaml";
}

@Override
Expand Down Expand Up @@ -550,6 +549,10 @@ public Map<String, Object> postProcessOperations(Map<String, Object> 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) {
Expand Down Expand Up @@ -687,9 +690,29 @@ private void removeHeadersFromContents(List<CodegenContent> 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<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
if(library.equals(SPRING_CLOUD_LIBRARY)) {
if (isSpringCloudLibrary()) {
List<CodegenSecurity> authMethods = (List<CodegenSecurity>) objs.get("authMethods");
if (authMethods != null) {
for (CodegenSecurity authMethod : authMethods) {
Expand All @@ -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";
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/arguments/spring.yaml
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -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}}
31 changes: 28 additions & 3 deletions src/main/resources/handlebars/JavaSpring/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}}
Expand All @@ -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}}
Expand All @@ -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}});
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/resources/handlebars/JavaSpring/apiController.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}}
Expand Down Expand Up @@ -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}} {

Expand Down
12 changes: 12 additions & 0 deletions src/main/resources/handlebars/JavaSpring/apiDelegate.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/handlebars/JavaSpring/application.mustache
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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}}
{{#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}}
Original file line number Diff line number Diff line change
@@ -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}}
{{#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}}
Original file line number Diff line number Diff line change
@@ -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}}
Original file line number Diff line number Diff line change
@@ -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}}
{{#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}}
Loading