Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generating enums with --enum-values does not work #2163

Open
1 of 2 tasks
mvlanga opened this issue Feb 19, 2025 · 2 comments
Open
1 of 2 tasks

generating enums with --enum-values does not work #2163

mvlanga opened this issue Feb 19, 2025 · 2 comments
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@mvlanga
Copy link

mvlanga commented Feb 19, 2025

openapi-typescript version

7.0.0 & 7.6.1 & 6.7.6

Node.js version

22.10.0

OS + version

macOS 15.3

Description

We are trying to generate javascript enums values with the cli flag --enum-values like the documentation says.

We use the command npx openapi-typescript --enum-values api-spec/api.json -o src-generated/api.d.ts

When we use only the flag --enum, the types are generated successful.

When we use --enujm-values OR --dedupe-enums the cli tool doesn't return anything, it just keeps loading.

{
  "openapi": "3.1.0",
  "info": {
    "title": "OpenAPI definition",
    "version": "v0",
    "license": {
      "name": "Author",
      "identifier": "its-me"
    }
  },
  "servers": [
    {
      "url": "https://example.com",
      "description": "Generated server url"
    }
  ],
  "paths": {
    "/api/plants/add": {
      "post": {
        "operationId": "addPlant",
        "summary": "Add a plant with providing plantType",
        "security": [],
        "parameters": [
          {
            "name": "plantType",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "JUNGLE",
                "DESERT"
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "*/*": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  }
}

Reproduction

  1. Create a api.json file with the following content:
{
  "openapi": "3.1.0",
  "info": {
    "title": "OpenAPI definition",
    "version": "v0",
    "license": {
      "name": "Author",
      "identifier": "its-me"
    }
  },
  "servers": [
    {
      "url": "https://example.com",
      "description": "Generated server url"
    }
  ],
  "paths": {
    "/api/plants/add": {
      "post": {
        "operationId": "addPlant",
        "summary": "Add a plant with providing plantType",
        "security": [],
        "parameters": [
          {
            "name": "plantType",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "JUNGLE",
                "DESERT"
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "*/*": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  }
}
  1. run npx openapi-typescript --enum-values api.json -o api.d.ts

Expected result

I would expect that the generated types contain something like that:

export const plantType = {
    JUNGLE: "JUNGLE",
    DESERT: "DESERT",
} as const

Required

  • My OpenAPI schema is valid and passes the Redocly validator (npx @redocly/cli@latest lint)

Extra

@mvlanga mvlanga added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Feb 19, 2025
@mvlanga
Copy link
Author

mvlanga commented Feb 19, 2025

Update: The --enum-values flag also doesn't work with an provided example: https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-typescript/examples/simple-example.yaml

@theo-staizen
Copy link

theo-staizen commented Feb 26, 2025

I cannot seem to reproduce your issue, using v7.6.1, when i run the cli with the example json above

npx openapi-typescript foo.api.json -o foo.types.ts  --enum-values

I get the following enum generated at the end of the file, which i dont find very useful (and also causes an error, because query can be undefined, so it's not sure about plantType)

type ReadonlyArray<T> = [
    Exclude<T, undefined>
] extends [
    any[]
] ? Readonly<Exclude<T, undefined>> : Readonly<Exclude<T, undefined>[]>;
export const pathsApiPlantsAddPostParametersQueryPlantTypeValues: ReadonlyArray<paths["/api/plants/add"]["post"]["parameters"]["query"]["plantType"]>
= ["JUNGLE", "DESERT"];

When using the --enum flag instead I get this, which is more useful (even though its an enum)

export enum PathsApiPlantsAddPostParametersQueryPlantType {
    JUNGLE = "JUNGLE",
    DESERT = "DESERT"
}

AFAIK, there is no way using this tool to generate the "const obj as const" notation that you expect.


Also, just a note, when generating enums, you shouldn't use .d.ts extension, because enums (or array or objects) compile to actual JS code, so you can't keep them in a definition file, which is meant only for TS code that generate no JS code (i.e erasable syntax).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

No branches or pull requests

2 participants