Skip to content

Commit 05667eb

Browse files
authored
Merge pull request #772 from swagger-api/rrockx-trifork-issue-444
Rrockx trifork issue 444
2 parents e2e3818 + 0e84992 commit 05667eb

21 files changed

+174
-35
lines changed

src/main/java/io/swagger/codegen/v3/generators/java/SpringCodegen.java

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,6 @@ public String getHelp() {
161161

162162
@Override
163163
public void processOpts() {
164-
setUseOas2(true);
165-
additionalProperties.put(CodegenConstants.USE_OAS2, true);
166-
167164
// Process java8 option before common java ones to change the default dateLibrary to java8.
168165
if (additionalProperties.containsKey(JAVA8_MODE)) {
169166
this.setJava8(Boolean.valueOf(additionalProperties.get(JAVA8_MODE).toString()));
@@ -302,7 +299,7 @@ public void processOpts() {
302299

303300
if (!this.interfaceOnly) {
304301

305-
if (library.equals(DEFAULT_LIBRARY)) {
302+
if (isDefaultLibrary()) {
306303
apiTestTemplateFiles.clear();
307304
supportingFiles.add(new SupportingFile("homeController.mustache",
308305
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "HomeController.java"));
@@ -313,7 +310,8 @@ public void processOpts() {
313310
supportingFiles.add(new SupportingFile("application.mustache",
314311
("src.main.resources").replace(".", java.io.File.separator), "application.properties"));
315312
}
316-
if (library.equals(SPRING_MVC_LIBRARY)) {
313+
if (isSpringMvcLibrary()) {
314+
forceOas2();
317315
supportingFiles.add(new SupportingFile("webApplication.mustache",
318316
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "WebApplication.java"));
319317
supportingFiles.add(new SupportingFile("webMvcConfiguration.mustache",
@@ -325,7 +323,8 @@ public void processOpts() {
325323
supportingFiles.add(new SupportingFile("application.properties",
326324
("src.main.resources").replace(".", java.io.File.separator), "swagger.properties"));
327325
}
328-
if (library.equals(SPRING_CLOUD_LIBRARY)) {
326+
if (isSpringCloudLibrary()) {
327+
forceOas2();
329328
supportingFiles.add(new SupportingFile("apiKeyRequestInterceptor.mustache",
330329
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "ApiKeyRequestInterceptor.java"));
331330
supportingFiles.add(new SupportingFile("clientConfiguration.mustache",
@@ -361,7 +360,7 @@ public void processOpts() {
361360
supportingFiles.add(new SupportingFile("swaggerDocumentationConfig.mustache",
362361
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "SwaggerDocumentationConfig.java"));
363362
}
364-
} else if ( this.swaggerDocketConfig && !library.equals(SPRING_CLOUD_LIBRARY)) {
363+
} else if ( this.swaggerDocketConfig && !isSpringCloudLibrary()) {
365364
supportingFiles.add(new SupportingFile("swaggerDocumentationConfig.mustache",
366365
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "SwaggerDocumentationConfig.java"));
367366
}
@@ -372,7 +371,7 @@ public void processOpts() {
372371
if ("threetenbp".equals(dateLibrary)) {
373372
supportingFiles.add(new SupportingFile("customInstantDeserializer.mustache",
374373
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "CustomInstantDeserializer.java"));
375-
if (library.equals(DEFAULT_LIBRARY) || library.equals(SPRING_CLOUD_LIBRARY)) {
374+
if (isDefaultLibrary() || isSpringCloudLibrary()) {
376375
supportingFiles.add(new SupportingFile("jacksonConfiguration.mustache",
377376
(sourceFolder + File.separator + configPackage).replace(".", java.io.File.separator), "JacksonConfiguration.java"));
378377
}
@@ -447,7 +446,7 @@ public void execute(Template.Fragment fragment, Writer writer) throws IOExceptio
447446

448447
@Override
449448
public void addOperationToGroup(String tag, String resourcePath, Operation operation, CodegenOperation co, Map<String, List<CodegenOperation>> operations) {
450-
if((library.equals(DEFAULT_LIBRARY) || library.equals(SPRING_MVC_LIBRARY)) && !useTags) {
449+
if((isDefaultLibrary() || isSpringMvcLibrary()) && !useTags) {
451450
String basePath = resourcePath;
452451
if (basePath.startsWith("/")) {
453452
basePath = basePath.substring(1);
@@ -476,7 +475,7 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera
476475

477476
@Override
478477
public String getArgumentsLocation() {
479-
return null;
478+
return "/arguments/spring.yaml";
480479
}
481480

482481
@Override
@@ -550,6 +549,10 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
550549
if ("0".equals(resp.code)) {
551550
resp.code = "200";
552551
}
552+
if (resp.baseType == null) {
553+
// set vendorExtensions.x-java-is-response-void to true as baseType is set to "Void"
554+
resp.vendorExtensions.put("x-java-is-response-void", true);
555+
}
553556
doDataTypeAssignment(resp.dataType, new DataTypeAssigner() {
554557
@Override
555558
public void setReturnType(final String returnType) {
@@ -687,9 +690,29 @@ private void removeHeadersFromContents(List<CodegenContent> contents) {
687690
}
688691
}
689692

693+
/**
694+
* Forces Oas2 specification, use it when Oas3 is not supported.
695+
*/
696+
private void forceOas2() {
697+
setUseOas2(true);
698+
additionalProperties.put(CodegenConstants.USE_OAS2, true);
699+
}
700+
701+
private boolean isSpringCloudLibrary() {
702+
return library.equals(SPRING_CLOUD_LIBRARY);
703+
}
704+
705+
private boolean isSpringMvcLibrary() {
706+
return library.equals(SPRING_MVC_LIBRARY);
707+
}
708+
709+
private boolean isDefaultLibrary() {
710+
return library.equals(DEFAULT_LIBRARY);
711+
}
712+
690713
@Override
691714
public Map<String, Object> postProcessSupportingFileData(Map<String, Object> objs) {
692-
if(library.equals(SPRING_CLOUD_LIBRARY)) {
715+
if (isSpringCloudLibrary()) {
693716
List<CodegenSecurity> authMethods = (List<CodegenSecurity>) objs.get("authMethods");
694717
if (authMethods != null) {
695718
for (CodegenSecurity authMethod : authMethods) {
@@ -711,10 +734,10 @@ public String toApiName(String name) {
711734

712735
@Override
713736
public String toApiTestFilename(String name) {
714-
if(library.equals(SPRING_MVC_LIBRARY)) {
737+
if (isSpringMvcLibrary()) {
715738
return toApiName(name) + "ControllerIT";
716739
}
717-
if(library.equals(SPRING_CLOUD_LIBRARY)) {
740+
if (isSpringCloudLibrary()) {
718741
return toApiName(name) + "Test";
719742
}
720743
return toApiName(name) + "ControllerIntegrationTest";
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
arguments:
2+
- option: "--use-oas2"
3+
description: "use OpenAPI v2.0 (Swagger 1.5.x) annotations (by default, OpenAPI v3.0 is used)."
4+
type: "boolean"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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}}

src/main/resources/handlebars/JavaSpring/api.mustache

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,19 @@ package {{package}};
1010
{{#jdk8-no-delegate}}
1111
import com.fasterxml.jackson.databind.ObjectMapper;
1212
{{/jdk8-no-delegate}}
13+
{{#useOas2}}
1314
import io.swagger.annotations.*;
15+
{{/useOas2}}
16+
{{^useOas2}}
17+
import io.swagger.v3.oas.annotations.Operation;
18+
import io.swagger.v3.oas.annotations.Parameter;
19+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
20+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
21+
import io.swagger.v3.oas.annotations.media.ArraySchema;
22+
import io.swagger.v3.oas.annotations.media.Content;
23+
import io.swagger.v3.oas.annotations.media.Schema;
24+
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
25+
{{/useOas2}}
1426
{{#jdk8-no-delegate}}
1527
import org.slf4j.Logger;
1628
import org.slf4j.LoggerFactory;
@@ -53,8 +65,11 @@ import java.util.Optional;
5365
{{#async}}
5466
import java.util.concurrent.{{^jdk8}}Callable{{/jdk8}}{{#jdk8}}CompletableFuture{{/jdk8}};
5567
{{/async}}
68+
5669
{{>generatedAnnotation}}
57-
@Api(value = "{{{baseName}}}", description = "the {{{baseName}}} API")
70+
{{#useOas2}}
71+
@Api(value = "{{{baseName}}}", description = "the {{{baseName}}} API")
72+
{{/useOas2}}
5873
{{#operations}}
5974
public interface {{classname}} {
6075
{{#jdk8}}
@@ -81,6 +96,7 @@ public interface {{classname}} {
8196
{{#operation}}
8297
{{#contents}}
8398

99+
{{#useOas2}}
84100
@ApiOperation(value = "{{{summary}}}", nickname = "{{{operationId}}}", notes = "{{{notes}}}"{{#returnBaseType}}, response = {{{returnBaseType}}}.class{{/returnBaseType}}{{#returnContainer}}, responseContainer = "{{{returnContainer}}}"{{/returnContainer}}{{#hasAuthMethods}}, authorizations = {
85101
{{#authMethods}}@Authorization(value = "{{name}}"{{#isOAuth}}, scopes = { {{#each scopes}}
86102
@AuthorizationScope(scope = "{{@key}}", description = "{{this}}"){{^@last}},{{/@last}}{{/each}}
@@ -96,13 +112,22 @@ public interface {{classname}} {
96112
{{/headerParams}}
97113
})
98114
{{/implicitHeaders}}
115+
{{/useOas2}}
116+
{{^useOas2}}
117+
@Operation(summary = "{{{summary}}}", description = "{{{notes}}}"{{#hasAuthMethods}}, security = {
118+
{{#authMethods}}@SecurityRequirement(name = "{{name}}"{{#isOAuth}}, scopes = { {{#each scopes}}"{{@key}}"{{^@last}}, {{/@last}}{{/each}} }{{/isOAuth}}){{#hasMore}},
119+
{{/hasMore}}{{/authMethods}}
120+
}{{/hasAuthMethods}}, tags={ {{#vendorExtensions.x-tags}}"{{tag}}"{{#hasMore}}, {{/hasMore}}{{/vendorExtensions.x-tags}} })
121+
@ApiResponses(value = { {{#responses}}
122+
@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}} })
123+
{{/useOas2}}
99124
@RequestMapping(value = "{{{path}}}",{{#singleContentTypes}}{{#hasProduces}}
100125
produces = "{{{vendorExtensions.x-accepts}}}", {{/hasProduces}}{{#hasConsumes}}
101126
consumes = "{{{vendorExtensions.x-contentType}}}",{{/hasConsumes}}{{/singleContentTypes}}{{^singleContentTypes}}{{#hasProduces}}
102127
produces = { {{#produces}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/produces}} }, {{/hasProduces}}{{#hasConsumes}}
103-
consumes = { {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} },{{/hasConsumes}}{{/singleContentTypes}}
128+
consumes = { {{#consumes}}"{{{mediaType}}}"{{#hasMore}}, {{/hasMore}}{{/consumes}} }, {{/hasConsumes}}{{/singleContentTypes}}
104129
method = RequestMethod.{{httpMethod}})
105-
{{#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}} {
130+
{{#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}} {
106131
{{#delegate-method}}
107132
return {{operationId}}({{#parameters}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{/parameters}});
108133
}

src/main/resources/handlebars/JavaSpring/apiController.mustache

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,19 @@ package {{package}};
88
import com.fasterxml.jackson.databind.ObjectMapper;
99
{{/isDelegate}}
1010
{{#fullController}}
11+
{{#useOas2}}
1112
import io.swagger.annotations.*;
13+
{{/useOas2}}
14+
{{^useOas2}}
15+
import io.swagger.v3.oas.annotations.Operation;
16+
import io.swagger.v3.oas.annotations.Parameter;
17+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
18+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
19+
import io.swagger.v3.oas.annotations.media.ArraySchema;
20+
import io.swagger.v3.oas.annotations.media.Content;
21+
import io.swagger.v3.oas.annotations.media.Schema;
22+
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
23+
{{/useOas2}}
1224
import org.slf4j.Logger;
1325
import org.slf4j.LoggerFactory;
1426
import org.springframework.http.HttpStatus;
@@ -22,6 +34,9 @@ import org.springframework.web.bind.annotation.RequestBody;
2234
import org.springframework.web.bind.annotation.RequestHeader;
2335
import org.springframework.web.bind.annotation.RequestParam;
2436
import org.springframework.web.bind.annotation.RequestPart;
37+
{{^useOas2}}
38+
import org.springframework.web.bind.annotation.RestController;
39+
{{/useOas2}}
2540
import org.springframework.web.multipart.MultipartFile;
2641

2742
{{#useBeanValidation}}
@@ -50,8 +65,14 @@ import java.util.Map;
5065
import java.util.concurrent.Callable;
5166
{{/async}}
5267
{{/fullController}}
68+
5369
{{>generatedAnnotation}}
70+
{{#useOas2}}
5471
@Controller
72+
{{/useOas2}}
73+
{{^useOas2}}
74+
@RestController
75+
{{/useOas2}}
5576
{{#operations}}
5677
public class {{classname}}Controller implements {{classname}} {
5778

src/main/resources/handlebars/JavaSpring/apiDelegate.mustache

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,19 @@ package {{package}};
55
{{#jdk8}}
66
import com.fasterxml.jackson.databind.ObjectMapper;
77
{{/jdk8}}
8+
{{#useOas2}}
89
import io.swagger.annotations.*;
10+
{{/useOas2}}
11+
{{^useOas2}}
12+
import io.swagger.v3.oas.annotations.Operation;
13+
import io.swagger.v3.oas.annotations.Parameter;
14+
import io.swagger.v3.oas.annotations.responses.ApiResponses;
15+
import io.swagger.v3.oas.annotations.responses.ApiResponse;
16+
import io.swagger.v3.oas.annotations.media.ArraySchema;
17+
import io.swagger.v3.oas.annotations.media.Content;
18+
import io.swagger.v3.oas.annotations.media.Schema;
19+
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
20+
{{/useOas2}}
921
{{#jdk8}}
1022
import org.slf4j.Logger;
1123
import org.slf4j.LoggerFactory;

src/main/resources/handlebars/JavaSpring/apiException.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package {{apiPackage}};
22

33
{{>generatedAnnotation}}
4-
public class ApiException extends Exception{
4+
public class ApiException extends Exception {
55
private int code;
66
public ApiException (int code, String msg) {
77
super(msg);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
{{#useOas2}}
12
springfox.documentation.swagger.v2.path=/api-docs
23
server.contextPath={{^contextPath}}/{{/contextPath}}{{#contextPath}}{{contextPath}}{{/contextPath}}
4+
{{/useOas2}}
35
server.port={{serverPort}}
46
spring.jackson.date-format={{basePackage}}.RFC3339DateFormat
57
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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}}
1+
{{#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}}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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}}
1+
{{#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}}

0 commit comments

Comments
 (0)