OpenAPITools / OpenAPITools/openapi-generator

[BUG] Issue with TypeScript type generation for multiple status codes

Open
#18,610 0 comments 2 reactions 1 assignee View on GitHub

@kay-schecker is already working on this.

Since Feb 1, 2024.

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

Description

I’m using openapi-generator-cli to generate TypeScript types from an openapi.json file. My API has an operation that can return two different status codes (200 and 201), each with a different response type. The status code 200 returns an application/octet-stream file, while the 201 returns a JSON object.

When I run openapi-generator-cli generate -i ./openapi.json -g typescript-axios -o ./src/api, only the type for the status code 200 is generated. If the operation returns a 201, TypeScript thinks the response is void.

Here is the definition of my responses in openapi.json:

"responses": {
  "200": {
    "description": "Successful response",
    "content": {
      "application/octet-stream": { "example": "Just a file" }
    }
  },
  "404": { "description": "Not found" },
  "201": {
    "description": "Resource created",
    "content": {
      "application/json": {
        "schema": { 
          "$ref": "#/components/schemas/JSONResponse" 
        }
      }
    }
  },
}

And here is the JSONResponse schema:

"JSONResponse": {
  "type": "object",
  "properties": {
    "data64": {
      "type": "string"
    }
  }
}

I’m using version 7.2.0 of openapi-generator-cli. Is there a way to correctly generate types for both status codes? Because when I try to access the data64 property of the response when the status code is 201, I get a TypeScript error. Any help on this would be appreciated.

Thank you for your help.

Expected behavior

I think that response type should be void | JSONResponse

Screenshots

If applicable, add screenshots to help explain your problem.

Operation System (please complete the following information):
  • OS: MacOS
  • Version 14.2.1

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.