OpenAPITools / OpenAPITools/openapi-generator

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

Open
#20,561 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

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.

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.

Research direction

Start with toEnumVarName in DefaultCodegen.java and AbstractTypeScriptClientCodegen.java, then reproduce the issue with the supplied v1.json and run npm run update-api. Inspect src/v1/models/index.ts; done means the generated enum has a valid key for the single-space value and the TypeScript build succeeds.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, typescript
Domain
devtools, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
44/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.