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
3 changes: 3 additions & 0 deletions docs/generators/spring.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ CONFIG OPTIONS for spring
hateoas
Use Spring HATEOAS library to allow adding HATEOAS links (Default: false)

returnSuccessCode
Generated server returns 2xx code (Default: false)

library
library template (sub-template) to use (Default: spring-boot)
spring-boot - Spring-boot Server application using the SpringFox integration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2365,7 +2365,13 @@ public CodegenOperation fromOperation(String path,
}

// generate examples
op.examples = new ExampleGenerator(schemas, openAPI).generateFromResponseSchema(responseSchema, getProducesInfo(openAPI, operation));
String exampleStatusCode = "200";
for (String key : operation.getResponses().keySet()) {
if (operation.getResponses().get(key) == methodResponse && !key.equals("default")) {
Copy link
Copy Markdown
Contributor Author

@ackintosh ackintosh Nov 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

methodResponse is "2xx" or "default" so exampleStatusCode will be 2xx.

exampleStatusCode = key;
}
}
op.examples = new ExampleGenerator(schemas, openAPI).generateFromResponseSchema(exampleStatusCode, responseSchema, getProducesInfo(openAPI, operation));
op.defaultResponse = toDefaultValue(responseSchema);
op.returnType = cm.dataType;
op.hasReference = schemas != null && schemas.containsKey(op.returnBaseType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class ExampleGenerator {
private static final String NONE = "none";
private static final String URL = "url";
private static final String URI = "uri";
private static final String STATUS_CODE = "statusCode";

protected Map<String, Schema> examples;
private OpenAPI openAPI;
Expand All @@ -54,7 +55,20 @@ public ExampleGenerator(Map<String, Schema> examples, OpenAPI openAPI) {
this.random = new Random("ExampleGenerator".hashCode());
}

public List<Map<String, String>> generateFromResponseSchema(Schema responseSchema, Set<String> producesInfo) {
public List<Map<String, String>> generateFromResponseSchema(String statusCode, Schema responseSchema, Set<String> producesInfo) {
List<Map<String, String>> examples = generateFromResponseSchema(responseSchema, producesInfo);
if (examples == null) {
return null;
}

for (Map<String, String> example : examples) {
example.put(STATUS_CODE, statusCode);
}

return examples;
}

private List<Map<String, String>> generateFromResponseSchema(Schema responseSchema, Set<String> producesInfo) {
if (responseSchema.getExample() == null && StringUtils.isEmpty(responseSchema.get$ref()) && !ModelUtils.isArraySchema(responseSchema)) {
// no example provided
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public class SpringCodegen extends AbstractJavaCodegen
public static final String OPENAPI_DOCKET_CONFIG = "swaggerDocketConfig";
public static final String API_FIRST = "apiFirst";
public static final String HATEOAS = "hateoas";
public static final String RETURN_SUCCESS_CODE = "returnSuccessCode";

protected String title = "OpenAPI Spring";
protected String configPackage = "org.openapitools.configuration";
Expand All @@ -98,6 +99,7 @@ public class SpringCodegen extends AbstractJavaCodegen
protected boolean useOptional = false;
protected boolean virtualService = false;
protected boolean hateoas = false;
protected boolean returnSuccessCode = false;

public SpringCodegen() {
super();
Expand Down Expand Up @@ -131,6 +133,7 @@ public SpringCodegen() {
cliOptions.add(CliOption.newBoolean(API_FIRST, "Generate the API from the OAI spec at server compile time (API first approach)", apiFirst));
cliOptions.add(CliOption.newBoolean(USE_OPTIONAL,"Use Optional container for optional parameters", useOptional));
cliOptions.add(CliOption.newBoolean(HATEOAS, "Use Spring HATEOAS library to allow adding HATEOAS links", hateoas));
cliOptions.add(CliOption.newBoolean(RETURN_SUCCESS_CODE, "Generated server returns 2xx code", returnSuccessCode));

supportedLibraries.put(SPRING_BOOT, "Spring-boot Server application using the SpringFox integration.");
supportedLibraries.put(SPRING_MVC_LIBRARY, "Spring-MVC Server application using the SpringFox integration.");
Expand Down Expand Up @@ -277,6 +280,10 @@ public void processOpts() {
this.setHateoas(Boolean.valueOf(additionalProperties.get(HATEOAS).toString()));
}

if (additionalProperties.containsKey(RETURN_SUCCESS_CODE)) {
this.setReturnSuccessCode(Boolean.valueOf(additionalProperties.get(RETURN_SUCCESS_CODE).toString()));
}

typeMapping.put("file", "Resource");
importMapping.put("Resource", "org.springframework.core.io.Resource");

Expand Down Expand Up @@ -721,6 +728,10 @@ public void setHateoas(boolean hateoas) {
this.hateoas = hateoas;
}

public void setReturnSuccessCode(boolean returnSuccessCode) {
this.returnSuccessCode = returnSuccessCode;
}

@Override
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
super.postProcessModelProperty(model, property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ return CompletableFuture.supplyAsync(()-> {
{{#jdk8}}
{{#async}} {{/async}} });
{{/jdk8}}
{{#async}} {{/async}} return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
{{#async}} {{/async}} return new ResponseEntity<>({{#returnSuccessCode}}HttpStatus.valueOf({{{statusCode}}}){{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}});
{{#jdk8}}
{{#async}}
}, Runnable::run);
Expand All @@ -25,14 +25,14 @@ return CompletableFuture.supplyAsync(()-> {
{{/-last}}
{{/examples}}
{{^examples}}
return {{#jdk8}}{{#async}}CompletableFuture.completedFuture({{/async}}{{/jdk8}}new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED){{#jdk8}}{{#async}}){{/async}}{{/jdk8}};
return {{#jdk8}}{{#async}}CompletableFuture.completedFuture({{/async}}{{/jdk8}}new ResponseEntity<>({{#returnSuccessCode}}HttpStatus.OK{{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}}){{#jdk8}}{{#async}}){{/async}}{{/jdk8}};
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be HttpStatus.valueOf({{{statusCode}}}) instead of HttpStatus.OK

{{/examples}}
{{/reactive}}
{{#reactive}}
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
Mono<Void> result = Mono.empty();
Mono<Void> result = Mono.empty();
{{#examples}}
{{#-first}}
exchange.getResponse().setStatusCode({{#returnSuccessCode}}HttpStatus.valueOf({{{statusCode}}}){{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}});
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
{{/-first}}
if (mediaType.isCompatibleWith(MediaType.valueOf("{{{contentType}}}"))) {
Expand All @@ -43,5 +43,8 @@ exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
}
{{/-last}}
{{/examples}}
{{^examples}}
exchange.getResponse().setStatusCode({{#returnSuccessCode}}HttpStatus.OK{{/returnSuccessCode}}{{^returnSuccessCode}}HttpStatus.NOT_IMPLEMENTED{{/returnSuccessCode}});
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be HttpStatus.valueOf({{{statusCode}}}) instead of HttpStatus.OK ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it should be HttpStatus.OK as the status code is contained by example. The code above is in {{^examples}} section.

{{/examples}}
return result.then(Mono.empty());
{{/reactive}}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ default Optional<NativeWebRequest> getRequest() {
*/
default Mono<ResponseEntity<Client>> call123testSpecialTags(Mono<Client> client,
ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
Mono<Void> result = Mono.empty();
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
result = ApiUtil.getExampleResponse(exchange, "{ \"client\" : \"client\"}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ default Optional<NativeWebRequest> getRequest() {
*/
default Mono<ResponseEntity<Boolean>> fakeOuterBooleanSerialize(Mono<Boolean> body,
ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
Mono<Void> result = Mono.empty();
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
return result.then(Mono.empty());

}
Expand All @@ -51,8 +51,8 @@ default Mono<ResponseEntity<Boolean>> fakeOuterBooleanSerialize(Mono<Boolean> bo
*/
default Mono<ResponseEntity<OuterComposite>> fakeOuterCompositeSerialize(Mono<OuterComposite> outerComposite,
ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
Mono<Void> result = Mono.empty();
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
if (mediaType.isCompatibleWith(MediaType.valueOf("*/*"))) {
result = ApiUtil.getExampleResponse(exchange, "{ \"my_string\" : \"my_string\", \"my_number\" : 0.80082819046101150206595775671303272247314453125, \"my_boolean\" : true}");
Expand All @@ -68,8 +68,8 @@ default Mono<ResponseEntity<OuterComposite>> fakeOuterCompositeSerialize(Mono<Ou
*/
default Mono<ResponseEntity<BigDecimal>> fakeOuterNumberSerialize(Mono<BigDecimal> body,
ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
Mono<Void> result = Mono.empty();
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
return result.then(Mono.empty());

}
Expand All @@ -79,8 +79,8 @@ default Mono<ResponseEntity<BigDecimal>> fakeOuterNumberSerialize(Mono<BigDecima
*/
default Mono<ResponseEntity<String>> fakeOuterStringSerialize(Mono<String> body,
ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
Mono<Void> result = Mono.empty();
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
return result.then(Mono.empty());

}
Expand All @@ -90,8 +90,8 @@ default Mono<ResponseEntity<String>> fakeOuterStringSerialize(Mono<String> body,
*/
default Mono<ResponseEntity<Void>> testBodyWithFileSchema(Mono<FileSchemaTestClass> fileSchemaTestClass,
ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
Mono<Void> result = Mono.empty();
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
return result.then(Mono.empty());

}
Expand All @@ -102,8 +102,8 @@ default Mono<ResponseEntity<Void>> testBodyWithFileSchema(Mono<FileSchemaTestCla
default Mono<ResponseEntity<Void>> testBodyWithQueryParams(String query,
Mono<User> user,
ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
Mono<Void> result = Mono.empty();
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
return result.then(Mono.empty());

}
Expand All @@ -113,8 +113,8 @@ default Mono<ResponseEntity<Void>> testBodyWithQueryParams(String query,
*/
default Mono<ResponseEntity<Client>> testClientModel(Mono<Client> client,
ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
Mono<Void> result = Mono.empty();
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
result = ApiUtil.getExampleResponse(exchange, "{ \"client\" : \"client\"}");
Expand Down Expand Up @@ -143,8 +143,8 @@ default Mono<ResponseEntity<Void>> testEndpointParameters(BigDecimal number,
String password,
String paramCallback,
ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
Mono<Void> result = Mono.empty();
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
return result.then(Mono.empty());

}
Expand All @@ -161,8 +161,8 @@ default Mono<ResponseEntity<Void>> testEnumParameters(List<String> enumHeaderStr
List<String> enumFormStringArray,
String enumFormString,
ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
Mono<Void> result = Mono.empty();
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
return result.then(Mono.empty());

}
Expand All @@ -177,8 +177,8 @@ default Mono<ResponseEntity<Void>> testGroupParameters(Integer requiredStringGro
Boolean booleanGroup,
Long int64Group,
ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
Mono<Void> result = Mono.empty();
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
return result.then(Mono.empty());

}
Expand All @@ -188,8 +188,8 @@ default Mono<ResponseEntity<Void>> testGroupParameters(Integer requiredStringGro
*/
default Mono<ResponseEntity<Void>> testInlineAdditionalProperties(Mono<String> requestBody,
ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
Mono<Void> result = Mono.empty();
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
return result.then(Mono.empty());

}
Expand All @@ -200,8 +200,8 @@ default Mono<ResponseEntity<Void>> testInlineAdditionalProperties(Mono<String> r
default Mono<ResponseEntity<Void>> testJsonFormData(String param,
String param2,
ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
Mono<Void> result = Mono.empty();
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
return result.then(Mono.empty());

}
Expand All @@ -213,8 +213,8 @@ default Mono<ResponseEntity<ModelApiResponse>> uploadFileWithRequiredFile(Long p
MultipartFile requiredFile,
String additionalMetadata,
ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
Mono<Void> result = Mono.empty();
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
result = ApiUtil.getExampleResponse(exchange, "{ \"code\" : 0, \"type\" : \"type\", \"message\" : \"message\"}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ default Optional<NativeWebRequest> getRequest() {
*/
default Mono<ResponseEntity<Client>> testClassname(Mono<Client> client,
ServerWebExchange exchange) {
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
Mono<Void> result = Mono.empty();
exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
result = ApiUtil.getExampleResponse(exchange, "{ \"client\" : \"client\"}");
Expand Down
Loading