Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle negative property names correctly in case and zod plugins #1919

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

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

stackblitz bot commented Apr 3, 2025

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: ec62470

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 3, 2025 11:09pm

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: ec62470

Copy link

codecov bot commented Apr 4, 2025

Codecov Report

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

Project coverage is 24.14%. Comparing base (c8eb3d6) to head (ec62470).

Files with missing lines Patch % Lines
packages/openapi-ts/src/plugins/zod/plugin.ts 0.00% 10 Missing ⚠️
...ckages/openapi-ts/src/plugins/shared/utils/case.ts 0.00% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1919      +/-   ##
==========================================
- Coverage   24.15%   24.14%   -0.01%     
==========================================
  Files         193      193              
  Lines       26851    26862      +11     
  Branches      787      787              
==========================================
  Hits         6486     6486              
- Misses      20290    20301      +11     
  Partials       75       75              
Flag Coverage Δ
unittests 24.14% <0.00%> (-0.01%) ⬇️

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.

'@hey-api/openapi-ts': patch
---

"Fix handling of negative property names in TypeScript code generation"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
"Fix handling of negative property names in TypeScript code generation"
fix: correctly handle numeric property names with signs

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