[Spring] Add an option to return success code (#1197)

* Returns status code which defined at Response Object

* Tweak indent

Remove the spacer "{{#async}} ... {{/async}}" "{{^async}} ... {{/async}}"

* Update samples^

* Fix broken indentation

* Update samples

* Revert methodBody.mustache

* Revert "Fix broken indentation"
  * This reverts commit 95b6a00f8b.
* Revert "Tweak indent"
  * This reverts commit ba2cedc897.
* Revert "Returns status code which defined at Response Object"
  * This reverts commit f676a89e23.

* Example contains status code

* Update samples

./bin/spring-all-pestore.sh

* Fix syntax error

* Update samples

* Run bin/utils/ensure-up-to-date

* Make the changes an option `returnSuccessCode`

* Run bin/spring-all-pestore.sh to update samples

* Run ./bin/utils/export_docs_generators.sh
This commit is contained in:
Akihito Nakano
2018-11-29 11:51:25 +09:00
committed by GitHub
parent 777bf1f3aa
commit f7c857cc39
11 changed files with 78 additions and 41 deletions

View File

@@ -2365,7 +2365,13 @@ public class DefaultCodegen implements CodegenConfig {
}
// 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")) {
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);

View File

@@ -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;
@@ -54,7 +55,20 @@ public class ExampleGenerator {
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;

View File

@@ -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";
@@ -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();
@@ -131,6 +133,7 @@ public class SpringCodegen extends AbstractJavaCodegen
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.");
@@ -277,6 +280,10 @@ public class SpringCodegen extends AbstractJavaCodegen
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");
@@ -721,6 +728,10 @@ public class SpringCodegen extends AbstractJavaCodegen
this.hateoas = hateoas;
}
public void setReturnSuccessCode(boolean returnSuccessCode) {
this.returnSuccessCode = returnSuccessCode;
}
@Override
public void postProcessModelProperty(CodegenModel model, CodegenProperty property) {
super.postProcessModelProperty(model, property);