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

Error with negative numbers in enums #1659

Closed
2 tasks done
fitztrev opened this issue May 13, 2024 · 2 comments
Closed
2 tasks done

Error with negative numbers in enums #1659

fitztrev opened this issue May 13, 2024 · 2 comments
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@fitztrev
Copy link
Contributor

Description

If an enum has a negative number, getting this error. Here is an example of a spec with one of the values

✨ openapi-typescript 7.0.0-next.0
(node:604330) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
/home/trev/code/openapi-typescript/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/typescript.js:21094
      Debug.assert(text.charCodeAt(0) !== 45 /* minus */, "Negative numbers should be created in combination with createPrefixUnaryExpression");
            ^

Error: Debug Failure. False expression: Negative numbers should be created in combination with createPrefixUnaryExpression
    at Object.createNumericLiteral (/home/trev/code/openapi-typescript/node_modules/.pnpm/[email protected]/node_modules/typescript/lib/typescript.js:21094:13)
    at tsLiteral (file:///home/trev/code/openapi-typescript/packages/openapi-typescript/dist/lib/ts.js:177:60)
    at Array.map (<anonymous>)
    at transformSchemaObjectWithComposition (file:///home/trev/code/openapi-typescript/packages/openapi-typescript/dist/transform/schema-object.js:48:44)
    at transformSchemaObject (file:///home/trev/code/openapi-typescript/packages/openapi-typescript/dist/transform/schema-object.js:6:18)
    at transformSchemaObjectCore (file:///home/trev/code/openapi-typescript/packages/openapi-typescript/dist/transform/schema-object.js:269:23)
    at transformSchemaObjectWithComposition (file:///home/trev/code/openapi-typescript/packages/openapi-typescript/dist/transform/schema-object.js:96:28)
    at transformSchemaObject (file:///home/trev/code/openapi-typescript/packages/openapi-typescript/dist/transform/schema-object.js:6:18)
    at transformMediaTypeObject (file:///home/trev/code/openapi-typescript/packages/openapi-typescript/dist/transform/media-type-object.js:7:12)
    at transformRequestBodyObject (file:///home/trev/code/openapi-typescript/packages/openapi-typescript/dist/transform/request-body-object.js:15:15)

Node.js v21.7.3
Name Version
openapi-typescript main @ 08ff135
Node.js 21.7.3
OS + version ubuntu 22.04

Reproduction

git clone https://github.com/lichess-org/api.git
cd api/doc
npm i openapi-typescript@next
npx openapi-typescript specs/lichess-api.yaml -o lichess.d.ts

Checklist

@fitztrev fitztrev added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels May 13, 2024
@fitztrev
Copy link
Contributor Author

This change fixes it for me: main...fitztrev:openapi-typescript:negative-enums

But I'm not familiar with the codebase so probably not in a PR-ready state. I duplicated some code from this section to get it working:

const literal =
value < 0
? ts.factory.createPrefixUnaryExpression(
ts.SyntaxKind.MinusToken,
ts.factory.createNumericLiteral(Math.abs(value)),
)
: ts.factory.createNumericLiteral(value);

@fitztrev
Copy link
Contributor Author

Fixed by #1662

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

No branches or pull requests

1 participant