[BUG][TYPESCRIPT] Fix string enum values containing "/*" being corrupted to "/_*"

Merged
IN
21d ago
+117-2

Description

Fixes #24572.

In the TypeScript family, string enum values are emitted inside quoted string literals (e.g. AB: 'a/*b'), but they were escaped with escapeText(), which chains into escapeUnsafeCharacters():

public String escapeUnsafeCharacters(String input) {
    return input.replace("*/", "*_/").replace("/*", "/_*");
}

That rewriting exists so that text interpolated into /** */ block comments cannot terminate the comment early. Enum values, however, only ever appear inside string literals where those sequences are harmless — so the escaping silently corrupts legitimate user data: an enum value foo/* was generated as 'foo/_*', and the generated constant no longer matches the value the server actually sends (e.g. wildcard patterns).

What this PR changes (narrow fix)

  • toEnumValue() in AbstractTypeScriptClientCodegen (covering typescript-fetch, typescript-axios, typescript-angular, typescript-rxjs, typescript-node, typescript-nestjs, and the other generators extending it) and in the legacy TypeScriptClientCodegen (the typescript generator) now escapes the value with the new escapeTextForStringLiteral(): it applies the same string-literal escaping as escapeText() (backslashes, quotes, control characters) but leaves /* and */ untouched.
  • Everything else is unchanged: escapeText() / escapeUnsafeCharacters() still apply wherever text is rendered inside comments — e.g. a model description containing */ is still generated as *_\/ inside the block comment.

This is deliberately not the sweeping "escaping should be context-dependent" redesign explored in the older, still-open PR #19528 / #19553 / #19554: those restructure the whole escaping pipeline and change behavior for every interpolated text. This PR keeps the change scoped to the enum-value call sites only, so comment-safety guarantees elsewhere remain exactly as they are today, and the diff stays reviewable. (The underlying design issue of context-blind escaping is tracked in #23962.)

Tests

  • SharedTypeScriptTest.enumValueWithCommentDelimitersIsNotCorruptedTest — a model with enum values ["a/*b", "*/c", "plain"] must generate the values 'a/*b', '*/c', 'plain' unchanged, across typescript (legacy TypeScriptClientCodegen.toEnumValue), typescript-fetch and typescript-axios (AbstractTypeScriptClientCodegen.toEnumValue), and the corrupted forms ('/_*', '*_/') must not appear.
  • SharedTypeScriptTest.commentDelimiterEscapingIsStillAppliedToCommentsTest — comment-injection guard: for the same model, the description containing */ is still escaped to *_\/ inside the generated block comment (typescript-axios), while the enum values in the same file stay intact — proving escapeUnsafeCharacters() still applies wherever text lands in comments.
  • Full org.openapitools.codegen.typescript test package: 235 tests, 0 failures, 0 errors.

Samples

Regenerated the petstore samples for the affected generators via ./bin/generate-samples.sh bin/configs/typescript-axios-default.yaml bin/configs/typescript-fetch-default.yaml bin/configs/typescript-fetch-default-v3.0.yaml bin/configs/typescript-axios-with-string-enums.yaml bin/configs/typescript-fetch-with-string-enums.yaml: the diff is empty (no petstore enum value contains comment delimiters, and values without them generate byte-identical output).

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
    (Done via Maven in a Docker container; the affected TypeScript petstore samples above were regenerated and the resulting diff is empty, so no sample changes are committed. Docs generators are not affected by this change.)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
    (Left to the maintainers to route to the TypeScript technical committee members, to avoid direct @ mentions.)

Summary by cubic

Fixes #24572: TypeScript string enum values containing /* or */ were silently rewritten to /_* and *_/, corrupting legitimate values like wildcard patterns when comment-escaping meant for block comments was applied to string literals.

  • toEnumValue() in AbstractTypeScriptClientCodegen and the legacy TypeScriptClientCodegen now uses the new escapeTextForStringLiteral(), which keeps all string-literal escaping but leaves comment delimiters untouched.
  • Comment escaping where text lands in block comments (e.g. model descriptions) is unchanged.
  • Adds regression and guard tests, plus a reproducer spec for #24572.