swagger-typescript-api icon indicating copy to clipboard operation
swagger-typescript-api copied to clipboard

[Bug] Enum keys starting with numbers are generated as invalid TypeScript identifiers

Open m-ichizawa opened this issue 3 weeks ago • 0 comments

Description

When generating TypeScript enums from a Swagger/OpenAPI spec with x-enumNames containing names that start with numbers, the generated enum keys are invalid TypeScript identifiers.

Expected Behavior

The generated TypeScript enum should use the x-enumNames values as provided in the OpenAPI spec to ensure valid identifiers, or properly transform them when they don't meet TypeScript identifier requirements.

Actual Behavior

The generated enum uses the enum values instead of x-enumNames when generating key names, resulting in invalid TypeScript code when the values start with numbers.

Reproduction Steps

Given the following OpenAPI definition:

"Config_TimeInterval": {
  "type": "string",
  "description": "Time interval configuration (\"s\": seconds, \"m\": minutes, \"h\": hours, \"no-release\": disabled)",
  "x-enumNames": [
    "_1s",
    "_5m",
    "_10m",
    "_1h",
    "no_release"
  ],
  "enum": [
    "1s",
    "5m",
    "10m",
    "1h",
    "no-release"
  ]
}

The current generated code is:

export enum ConfigTimeInterval {
  1S = "1s",           // ✗ Invalid: identifier cannot start with a number
  5M = "5m",           // ✗ Invalid: identifier cannot start with a number
  10M = "10m",         // ✗ Invalid: identifier cannot start with a number
  1H = "1h",           // ✗ Invalid: identifier cannot start with a number
  NoRelease = "no-release"  // ✓ Correct
}

Expected Output

The expected generated code should respect the x-enumNames and be valid TypeScript:

export enum ConfigTimeInterval {
  _1S = "1s",
  _5M = "5m",
  _10M = "10m",
  _1H = "1h",
  NoRelease = "no-release"
}

Version Information

  • swagger-typescript-api version: ^13.2.16
  • TypeScript version: ^5.4.5
  • Node version: 22.11.0

m-ichizawa avatar Dec 01 '25 06:12 m-ichizawa