Skip to content

[BUG][TYPESCRIPT] Enum with single space value fails to generate a valid TS enum #20561

Open
@bgroff-trdrev

Description

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Enumeration containing a single char space as a valid value fails to generate valid Typescript enum.

openapi-generator version

latest

"@openapitools/openapi-generator-cli": "^2.15.3"
OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Unnamed Service
  version: v1
paths: {}
components:
  schemas:
    "Enum.Values":
        enum:
          - " "
          - "*"
          - "9"
          - "A"
          - "B"
          - "C"
          - "D"
          - "E"
          - "F"
          - "G"
          - "H"
          - "I"
          - "J"
          - "K"
          - "L"
          - "M"
          - "N"
          - "O"
          - "P"
          - "Q"
          - "R"
          - "T"
          - "U"
          - "V"
          - "W"
          - "X"
          - "Y"
          - "Z"
        type: "string"
Generation Details

v1.json file schema (see schema file above, converted to JSON)

package.json - using node version 20.12.2, npm 10.5.0

{
    "name": "openapi-gen-testbed",
    "version": "",
    "description": "API $api_version client for $repo",
    "main": "dist/index.js",
    "types": "dist/index.d.ts",
    "exports": {
        "." : "./dist/index.js",
        "./v1": "./dist/v1/index.js"
    },
    "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1",
        "generate": "openapi-generator-cli generate --generator-key v1",
        "update-api": "npm run generate && npm run build",
        "build": "rm -rf dist/ && npx tsc"
    },
    "author": "",
    "license": "ISC",
    "devDependencies": {
        "@openapitools/openapi-generator-cli": "^2.15.3",
        "@tsconfig/node20": "^20.1.4",
        "typescript": "^5.4.5"
    },
    "engines": {
        "node": "20.12.2"
    }
}

openapitools.json - I've tried this with various combinations of additionalProperties

  • enumNameMappings
  • nameMapping
  • enumPropertyNamingReplaceSpecialChar
  • enumAsString
    the outcome is the same regardless, the enum generated is invalid
{
  "$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
  "spaces": 2,
  "generator-cli": {
    "version": "7.10.0",
    "generators": {
      "v1": {
        "inputSpec": "#{cwd}/v1.json",
        "generatorName": "typescript-fetch",
        "output": "#{cwd}/src/v1",
        "skipValidateSpec": true,
        "typeMappings": {
          "string+date-time":"string"
        },
        "additionalProperties": {
          "supportsES6": true,
          "withInterfaces": true,
          "typescriptThreePlus": true,
          "withoutRuntimeChecks": true,
          "enumNameMappings": {
            " ": "space"
          },
          "nameMapping": {
            " ": "space"
          },
          "enumPropertyNamingReplaceSpecialChar": false
        }
      }
    }  
  }
}
Steps to reproduce

Setup files above:

| v1.json // convert the yaml schema to json
| package.json
| openapitools.json
npm i
npm run update-api

during the build command, the code will fail to compile. It will point to a fault in the TS code where the enum generated a map with no key.

Actual Output

src/v1/models/index.ts

export const EnumValues = {
    : ' ',
    Star: '*',
    _9: '9',
  // ...
}
Expected Output
export const EnumValues = {
    " " : ' ',
    Star: '*',
    _9: '9',
  // ...
}

OR

export const EnumValues = {
    Space : ' ',
    Star: '*',
    _9: '9',
  // ...
}
Related issues/PRs

N/A

Suggest a fix

specialCharReplacements within DefaultCodegen.java corrects the '*' case, but this is presumably not a safe place to put a " " text replacement..

toEnumVarName method within either DefaultCodegen.java and/or AbstractTypeScriptClientCodegen.java looks like it might be the place where this would come from.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions