openapi-generator
openapi-generator copied to clipboard
[Spring] can't handle multiple responses
Description
We have an OpenAPI spec file where we've defined multiple possible responses for an operation, however when the Spring code is generated the generated @RequestMapping method is only capable of returning the first response defined.
openapi-generator version
3.2.3
OpenAPI declaration file content or url
openapi: "3.0.0"
info:
title: "Example"
version: "1.0.0"
servers:
- url: "/api/v1"
- url: "/api/"
tags:
- name: "example"
paths:
"/example":
get:
tags:
- "example"
operationId: "examples"
summary: "Get examples that match criteria"
parameters:
- name: "id"
in: query
description: "the identifier to get"
required: true
responses:
200:
description: "Success"
content:
application/json:
schema:
$ref: "#components/schemas/ExampleObject"
404:
description: "Failed to find the Example Object"
content:
application/json:
schema:
$ref: "#components/schemas/Errors"
components:
schemas:
"ExampleObject":
type: object
properties:
"property1":
type: string
"property2":
type: string
"Errors":
type: object
properties:
"code":
type: integer
format: int32
"severity":
type: string
"message":
type: string
Command line used for generation
<pluginExecutor>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>3.2.3</version>
</plugin>
<goal>generate</goal>
<configuration>
<inputSpec>api-def/spec.yml</inputSpec>
<generatorName>spring</generatorName>
<output>api-gen</output>
<modelNamePrefix>Api</modelNamePrefix>
<configOptions>
<basePackage>package</modelPackage>
<apiPackage>package</apiPackage>
<groupId>group</groupId>
<artifactId>api-gen</artifactId>
<artifactVersion>${project.version}</artifactVersion>
<artifactDescription>Automatically generated interfaces for APIs</artifactDescription>
<interfaceOnly>true</interfaceOnly>
</configOptions>
</configuration>
</pluginExecutor>
Steps to reproduce
Generate the Spring code using the specified spec file and maven configuration. The following @RequestMapping is generated:
@ApiOperation(value = "Get examples that match criteria", nickname = "examples", notes = "", response = ApiExampleObject.class, tags={ "example", })
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success", response = ApiExampleObject.class),
@ApiResponse(code = 404, message = "Failed to find the Example Object", response = ApiErrors.class) })
@RequestMapping(value = "/example",
produces = { "application/json" },
method = RequestMethod.GET)
default ResponseEntity<ApiExampleObject> examples(@NotNull @ApiParam(value = "the identifier to get", required = true) @Valid @RequestParam(value = "id", required = true) ) {
getRequest().ifPresent(request -> {
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
ApiUtil.setExampleResponse(request, "application/json", "{ \"property2\" : \"property2\", \"property1\" : \"property1\"}");
break;
}
}
});
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
The return type is of ResponseEntity<ApiExampleObject> which means I can't return an ApiErrors object.
Related issues/PRs
Sounds similar to #316
Suggest a fix/enhancement
Get the method to return an object that can encapsulate one of the types defined in the response.
Or a simpler option is to get the method to return a ResponseEntity<Object>.