openapi-generator-cli icon indicating copy to clipboard operation
openapi-generator-cli copied to clipboard

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

Open jgarmar opened this issue 1 year ago • 0 comments

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

jgarmar avatar Feb 01 '24 13:02 jgarmar