Skip to content

AwkEng/openapi-ts-decimal-varname-repro

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

openapi-ts decimal-varname repro

Standalone reproduction for hey-api/openapi-ts #3941.

Run

npm install
npm run generate
npm run typecheck                  # demonstrates the TS error
npx tsx src/demonstrate-data-loss.ts   # demonstrates the runtime data loss

Expected

tsc --noEmit should pass; generated types.gen.ts should have string-quoted keys for decimal-looking x-enum-varnames; Object.keys(...) at runtime should return the original varnames byte-for-byte.

Actual (openapi-ts 0.97.3, captured)

tsc --noEmit

src/generated/zod.gen.ts(7,57): error TS2345: Argument of type '{ readonly 22.537: 1; readonly 22.567: 2; }' is not assignable to parameter of type 'EnumLike'.
  Property '22.537' is incompatible with index signature.
    Type 'number' is not assignable to type 'string'.

Runtime (demonstrate-data-loss.ts)

Schema x-enum-varnames: ["101.147", "90.20", "90.35"]
Object.keys(...):        ["101.147","90.2","90.35"]

"90.20" preserved? false
"90.2"  present?   true

Generated types.gen.ts

src/generated/types.gen.ts emits unquoted decimal keys:

export const IntEnumWithDecimalVarnames = {
    /** 22.537 */
    22.537: 1,
    /** 22.567 */
    22.567: 2,
} as const;

export const StringEnumWithDecimalVarnames = {
    /** 101.147 */
    101.147: 'FM',
    /** 90.20 */
    90.2: 'PS',          // <-- DATA LOSS: trailing zero dropped
    /** 90.35 */
    90.35: 'B/ILT',
} as const;

Two issues:

  1. z.nativeEnum rejects numeric-keyed objectssrc/generated/zod.gen.ts fails tsc --noEmit with TS2345.
  2. Data loss for string-valued enums — the OpenAPI varname "90.20" is preserved correctly in the JSDoc, then the key beneath it is emitted unquoted, and JS coerces 90.2090.2. If two varnames were "90.20" and "90.2", they would collide silently into one key.

Schema is correct

The input openapi.json has quoted-string varnames ("22.537", "90.20", "90.35"). The defect is in openapi-ts's TS emitter — it knows the original (preserves it in the JSDoc above) but emits the key unquoted.

Suggested fix

Quote the emitted key when the varname isn't a valid JS identifier (or unconditionally — quoted identifier-shaped keys are still legal).

About

Standalone repro for hey-api/openapi-ts #3941: decimal-looking x-enum-varnames emitted as unquoted numeric JS keys, causing data loss and TS errors

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors