fix(typescript): do not import enum literals of anyOf params as models

TH
5h ago
+82-0

Since SIMPLIFY_ONEOF_ANYOF_ENUM (on by default) turns anyOf of enum refs into one inline enum, param type becomes literal union like 'value1' | 'value2'. toModelImportMap splits it on | and every member passes needToImport, so api file gets import { 'value1' } from ... (fetch alos html-escapes it to 'value1'), integer enums give import { 1 } from '../models/1'. Generated client does not compile. Not an escaping issue, these imports should not be there at all

needToImport in AbstractTypeScriptClientCodegen now skips literal members (quote anywhere, or leading digit), they are never model names. Fixes typescript-fetch, typescript-angular, typescript-node and typescript, all of them go through this method

Added test in TypeScriptFetchClientCodegenTest with string and integer enums, it fails on master, other typescript tests pass. Regenerated all 93 typescript configs, no sample changes. For the issue spec generated fetch client passes tsc --strict, on master it gives 90 errors

Fixes #22682

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
    
    Commit all changed files.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov @davidgamero @mkusaka @joscha @KannaKim


Summary by cubic

Fixes TypeScript code generators emitting broken model imports for inlined enum literal values. When anyOf combines enum schemas, SIMPLIFY_ONEOF_ANYOF_ENUM inlines them into literal unions, so the generated API file previously got import { 1 } from '../models/1' or an HTML-escaped 'value1' import, neither of which compiles. Fixes #22682.

Bug Fixes

  • needToImport in AbstractTypeScriptClientCodegen now skips unions whose members are all literal values; a model next to a literal in a union is still imported.
  • Applies to typescript, typescript-angular, typescript-fetch, and typescript-node, which all share this code path.
  • Adds regression tests in TypeScriptFetchClientCodegenTest covering string and integer enums and the mixed literal-with-model union; generated samples for all 93 TypeScript configs are unchanged.