fix: DefaultCodegen now generates an exemple for each status codes (#17603)

The DefaultCodegen now iterates through all api operations.

This allows to access different examples based on the response per status code and content type.
This commit is contained in:
Alexis Couvreur
2024-01-12 22:33:36 -05:00
committed by GitHub
parent 968c6dc418
commit b01b182af6
3 changed files with 91 additions and 12 deletions

View File

@@ -4452,18 +4452,6 @@ public class DefaultCodegen implements CodegenConfig {
}
}
// check skipOperationExample, which can be set to true to avoid out of memory errors for large spec
if (!isSkipOperationExample()) {
// generate examples
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, this.openAPI).generateFromResponseSchema(exampleStatusCode, responseSchema, getProducesInfo(this.openAPI, operation));
}
op.defaultResponse = toDefaultValue(responseSchema);
op.returnType = cm.dataType;
op.returnFormat = cm.dataFormat;
@@ -4642,6 +4630,35 @@ public class DefaultCodegen implements CodegenConfig {
}
}
// check skipOperationExample, which can be set to true to avoid out of memory errors for large spec
if (!isSkipOperationExample() && operation.getResponses() != null) {
// generate examples
ExampleGenerator generator = new ExampleGenerator(schemas, this.openAPI);
List<Map<String, String>> examples = new ArrayList<>();
for (String statusCode : operation.getResponses().keySet()) {
ApiResponse apiResponse = operation.getResponses().get(statusCode);
Schema schema = unaliasSchema(ModelUtils.getSchemaFromResponse(openAPI, apiResponse));
if (schema == null) {
continue;
}
if (apiResponse.getContent() != null) {
Set<String> producesInfo = new ConcurrentSkipListSet<>(apiResponse.getContent().keySet());
String exampleStatusCode = statusCode;
if (exampleStatusCode.equals("default")) {
exampleStatusCode = "200";
}
List<Map<String, String>> examplesForResponse = generator.generateFromResponseSchema(exampleStatusCode, schema, producesInfo);
if (examplesForResponse != null) {
examples.addAll(examplesForResponse);
}
}
}
op.examples = examples;
}
if (operation.getCallbacks() != null && !operation.getCallbacks().isEmpty()) {
operation.getCallbacks().forEach((name, callback) -> {
CodegenCallback c = fromCallback(name, callback, servers);

View File

@@ -988,6 +988,36 @@ public class DefaultCodegenTest {
Assert.assertEquals(codegenParameter2.example, "An example4 value");
}
@Test
public void testExample5MultipleResponses() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/examples.yaml");
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);
String path = "/example5/multiple_responses";
Operation operation = openAPI.getPaths().get(path).getGet();
CodegenOperation codegenOperation = codegen.fromOperation(path, "GET", operation, null);
List<Map<String, String>> examples = codegenOperation.examples;
Assert.assertEquals(examples.size(), 4);
// 200 response example
Assert.assertEquals(examples.get(0).get("contentType"), "application/json");
Assert.assertEquals(examples.get(0).get("example"), "\"a successful response example\"");
Assert.assertEquals(examples.get(0).get("statusCode"), "200");
// 301 response example
Assert.assertEquals(examples.get(1).get("contentType"), "application/json");
Assert.assertEquals(examples.get(1).get("example"), "\"a redirect response example\"");
Assert.assertEquals(examples.get(1).get("statusCode"), "301");
// 404 response example
Assert.assertEquals(examples.get(2).get("contentType"), "application/json");
Assert.assertEquals(examples.get(2).get("example"), "\"a not found response example\"");
Assert.assertEquals(examples.get(2).get("statusCode"), "404");
// 500 response example
Assert.assertEquals(examples.get(3).get("contentType"), "application/json");
Assert.assertEquals(examples.get(3).get("example"), "\"an internal server error response example\"");
Assert.assertEquals(examples.get(3).get("statusCode"), "500");
}
@Test
public void testDiscriminator() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/petstore-with-fake-endpoints-models-for-testing.yaml");

View File

@@ -107,6 +107,38 @@ paths:
responses:
'200':
description: successful operation
/example5/multiple_responses:
get:
operationId: generateFromResponseSchemaWithOneOfModel
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: string
example: a successful response example
'301':
description: permanent redirect
content:
application/json:
schema:
type: string
example: a redirect response example
'404':
description: not found
content:
application/json:
schema:
type: string
example: a not found response example
'500':
description: internal server error
content:
application/json:
schema:
type: string
example: an internal server error response example
components:
schemas:
User: