Skip to content

Conversation

ngalluzzo
Copy link

@ngalluzzo ngalluzzo commented Apr 3, 2025

Fix:

Fixes #1918
Handle negative property names in TypeScript code generation

Problem

When parsing OpenAPI schemas that contain property names with negative numbers (like "-1"), the TypeScript compiler would fail with the error:

Debug Failure. False expression: Negative numbers should be created in combination with createPrefixUnaryExpression

This is because TypeScript's compiler expects negative numeric literals to be created using a specific approach (createPrefixUnaryExpression) rather than directly as negative literals.

Solution

Modified the fieldName function in packages/openapi-ts/src/plugins/shared/utils/case.ts to use string literals for property names containing negative numbers. Also updated the Zod plugin with the same approach for consistency.

  • For negative numbers (e.g., "-1"), we now use string literals (ts.factory.createStringLiteral("-1"))
  • For positive numbers with a "+" sign (e.g., "+1"), we also use string literals for consistent handling

Testing

  1. Added a comprehensive test fixture (packages/openapi-ts-tests/test/spec/3.1.x/negative-property-names.json) that includes a schema with both positive and negative property names
  2. Added a test case to the OpenAPI 3.1.x test suite to verify correct handling
  3. Verified that the generated TypeScript code correctly handles these property names by checking for string literals in the output

All existing tests continue to pass, showing that this fix doesn't introduce any regressions.

Example

Before this fix, the following OpenAPI schema property would cause errors:

"-1": {
  "type": "integer"
}

Now it correctly generates TypeScript with string literal property names:

export type ReactionRollup = {
  // ...
  '-1': number;
  '+1': number;
  // ...
};

Updated the logic in the case and zod plugins to return string literals for negative numeric property names. Added tests and specifications to ensure proper handling of negative property names in the OpenAPI schema.
Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Copy link

changeset-bot bot commented Apr 3, 2025

🦋 Changeset detected

Latest commit: 64b7565

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hey-api/openapi-ts Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Apr 3, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
hey-api-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 27, 2025 6:25am

Copy link
Member

@mrlubos mrlubos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be up for adding snapshots for OpenAPI 2.0 and 3.0 as well? I also see Zod plugin has been updated, can you add snapshots for that?

Copy link

pkg-pr-new bot commented Apr 4, 2025

Open in StackBlitz

@hey-api/client-axios

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/client-axios@1919

@hey-api/client-fetch

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/client-fetch@1919

@hey-api/client-next

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/client-next@1919

@hey-api/client-nuxt

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/client-nuxt@1919

@hey-api/nuxt

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/nuxt@1919

@hey-api/openapi-ts

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/openapi-ts@1919

@hey-api/vite-plugin

npm i https://pkg.pr.new/hey-api/openapi-ts/@hey-api/vite-plugin@1919

commit: 64b7565

Copy link

codecov bot commented Apr 4, 2025

Codecov Report

Attention: Patch coverage is 0% with 10 lines in your changes missing coverage. Please review.

Project coverage is 22.33%. Comparing base (c8eb3d6) to head (64b7565).
Report is 46 commits behind head on main.

Files with missing lines Patch % Lines
packages/openapi-ts/src/plugins/zod/plugin.ts 0.00% 7 Missing ⚠️
...ckages/openapi-ts/src/plugins/shared/utils/case.ts 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1919      +/-   ##
==========================================
- Coverage   24.15%   22.33%   -1.83%     
==========================================
  Files         193      251      +58     
  Lines       26851    21073    -5778     
  Branches      787      783       -4     
==========================================
- Hits         6486     4706    -1780     
+ Misses      20290    16361    -3929     
+ Partials       75        6      -69     
Flag Coverage Δ
unittests 22.33% <0.00%> (-1.83%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mrlubos mrlubos merged commit 6bec672 into hey-api:main Apr 27, 2025
14 of 16 checks passed
@github-actions github-actions bot mentioned this pull request Apr 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

createClient Fails with TypeScript Assertion Error for Numeric-Like Property Names (e.g., "-1", "+1")
2 participants