Skip to content

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

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

Closed
ngalluzzo opened this issue Apr 3, 2025 · 1 comment · Fixed by #1919
Labels
bug 🔥 Something isn't working

Comments

@ngalluzzo
Copy link

Description

When generating a client using @hey-api/openapi-ts's createClient function, an internal TypeScript assertion error occurs if the OpenAPI specification contains object schemas with property names that resemble numbers, specifically negative or positive signed numbers like "-1" or "+1".

The error message is:
Debug Failure. False expression: Negative numbers should be created in combination with createPrefixUnaryExpression

This seems to happen because the underlying code generation process attempts to treat the property name string (e.g., "-1") as a numeric literal when interacting with the TypeScript compiler API, which expects negative numbers to be constructed differently (using createPrefixUnaryExpression).

Reproducible example or configuration

Steps to Reproduce:

  1. Define an OpenAPI v3 schema for a component containing properties named "-1" and "+1", similar to the example below.

  2. Use @hey-api/openapi-ts's createClient function, passing this schema as input. Ensure the @hey-api/typescript plugin (and potentially @hey-api/schemas or zod plugins, although the error seems to stem from the core TS generation) is included.

    // Example configuration structure
    import { createClient } from "@hey-api/openapi-ts";
    
    await createClient({
      input: {
        /* path to spec or spec object */
        // Example: path: './spec-with-numeric-props.json'
      },
      output: {
        path: "./generated-client",
      },
      plugins: [
        "@hey-api/typescript",
        // Add other plugins used, e.g.:
        // { name: '@hey-api/schemas', type: 'json' },
        // 'zod',
      ],
    });
  3. Observe the thrown error during generation.

Error Message / Stack Trace:

🔥 Unexpected error occurred. Log saved to /path/to/openapi-ts-error-[timestamp].log
🔥 Unexpected error occurred. Debug Failure. False expression: Negative numbers should be created in combination with createPrefixUnaryExpression
>>> Debug(OpenApiTsSdkGenerator): createClient FAILED: 25407 |     }
25408 |     return update(updated, original);
25409 |   }
25410 |   function createNumericLiteral(value, numericLiteralFlags = 0 /* None */) {
25411 |     const text = typeof value === "number" ? value + "" : value;
25412 |     Debug.assert(text.charCodeAt(0) !== 45 /* minus */, "Negative numbers should be created in combination with createPrefixUnaryExpression");
     Debug Failure. False expression: Negative numbers should be created in combination with createPrefixUnaryExpression
Error: Debug Failure. False expression: Negative numbers should be created in combination with createPrefixUnaryExpression
    at createNumericLiteral (/path/to/node_modules/typescript/lib/typescript.js:25412:11)
    at Df (/path/to/node_modules/@hey-api/openapi-ts/dist/index.js:16:19165)
    # ... (rest of stack trace) ...
    at processTicksAndRejections (native:7:39)

OpenAPI specification (optional)

# openapi: 3.0.x
# ...
components:
  schemas:
    reaction-rollup:
      title: Reaction Rollup
      type: object
      required:
        - url
        - total_count
        - "+1"
        - "-1"
        - laugh
        - confused
        - heart
        - hooray
        - eyes
        - rocket
      properties:
        url:
          type: string
          format: uri
        total_count:
          type: integer
        "+1": # Problematic property name
          type: integer
        "-1": # Problematic property name
          type: integer
        laugh:
          type: integer
        confused:
          type: integer
        heart:
          type: integer
        hooray:
          type: integer
        eyes:
          type: integer
        rocket:
          type: integer

System information (optional)

  • @hey-api/openapi-ts version: ^0.65.0
  • typescript version: 5.2.2
  • Node.js version: [Please fill in]
  • OS: macOS Sonoma 14.5
@ngalluzzo ngalluzzo added the bug 🔥 Something isn't working label Apr 3, 2025
@mrlubos
Copy link
Member

mrlubos commented Apr 3, 2025

Thanks for reporting!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🔥 Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants