Skip to content

Handle negative numeric enum values #1601

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

Merged
merged 1 commit into from
Mar 30, 2024

Conversation

raurfang
Copy link
Contributor

Changes

Previous behavior:

When there is no name explicitly provided for them, numeric enum member names are constructed by prefixing the number with a Value string
The code did not take negative numbers into account, which resulted in enum members like:

enum ErrorCode {
  _1 = -1,
  Value1 = 1
}

Context

Negative numeric enum members are not unusual in C# projects, and when combined with tools autogenerating OpenAPI schemas (i.e. Swashbuckle, NSwag), it is easy to hit the issue mentioned above.

Checklist

  • Unit tests updated
  • docs/ updated (if necessary)
  • pnpm run update:examples run (only applicable for openapi-typescript)

- append a `ValueMinus` string to numeric enum member names, if a name is not explicitly provided.
- create negative numeric enum member values using `ts.factory.createPrefixUnaryExpression()`

Previous behavior:

When there is no name explicitly provided for them, numeric enum member names are
constructed by prefixing the number with a `Value` string.
The code did not take negative numbers into account, which resulted in enum members like:
`_100 = -100`.

Additionally, since Typescript 5.4 (see microsoft/TypeScript#56570),
the `tsEnumMember` function failes on negative enum members.
Copy link

changeset-bot bot commented Mar 30, 2024

⚠️ No Changeset found

Latest commit: 3ba534d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@drwpow
Copy link
Contributor

drwpow commented Mar 30, 2024

Awesome fix, thank you so much!

No changeset needed for this because it’s in the 7.x release; this will go out in the next preview. Thanks!

@drwpow drwpow merged commit 677e04a into openapi-ts:main Mar 30, 2024
@drwpow
Copy link
Contributor

drwpow commented Apr 23, 2024

Released in the latest next version

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.

2 participants