OpenAPITools / OpenAPITools/openapi-generator

[BUG] Defining different type for each response status

Open
#14,053 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Hi,

I'm generating code for Java. I want my service to return a different type for each response status. If request is successful (status 200), I want it to return an object of type ResponseGetAllOrders, but if there's something wrong with the input parameters I want to respond with status 400 and return an object of type ResponseError.

But the generated code only allows my code to return the ResponseGetAllOrders object type.

swagger.yaml:

  /order:
    get:
      tags:
        - Orders
      summary: getAllOrders
      operationId: getAllOrders
      responses:
        200:
          $ref: '#/components/responses/ResponseGetAllOrders'
        400:
          $ref: '#/components/responses/ResponseError'

this is the generated code in the class OrdersApi:

    @ApiOperation(value = "getAllOrders", nickname = "getAllOrders", notes = "", response = ResponseGetAllOrders.class, authorizations = {
            @Authorization(value = "Bearer"),
            @Authorization(value = "basicAuth")
             }, tags={ "Orders", })
        @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Get All Orders response", response = ResponseGetAllOrders.class),
            @ApiResponse(code = 400, message = "Common error", response = ResponseError.class) })
        @RequestMapping(
            method = RequestMethod.GET,
            value = "/orders",
            produces = { "application/json" }
        )
        default ResponseEntity<ResponseGetAllOrdersObjectDto> getAllOrders() {
            return getDelegate().getAllOrders();
        }

Is there anyway to achieve what I'm trying to do? Is there some hack I could do?

If there is no way to do it, does anyone have any recommendations ir order to get my code clean? I don't want my Orders object to have any garbage properties like errorMessage or anything like that. That should go in the ResponseError object.

Thanks!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with swagger.yaml and the generated OrdersApi method to compare the documented 200 and 400 responses with the generated return type. Then trace the Java generator's response handling and related tests; done means establishing supported behavior for different response schemas or documenting the limitation and a clean workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.