Skip to content

[BUG][typescript-fetch] v7.10.0 (and potentially later) - 'oneOf' with primitive types generates calls to missing helper functions (instanceOfnumber, etc.) #21257

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
tobq opened this issue May 9, 2025 · 2 comments

Comments

@tobq
Copy link

tobq commented May 9, 2025

Description

When using openapi-generator-cli version 7.10.0 with the typescript-fetch generator, models generated for OpenAPI schemas that utilize oneOf to define a type as a choice between a primitive (e.g., number, string via $ref) and another primitive (e.g., a direct string enum) result in TypeScript compilation errors.

The generated code (e.g., for models like GenericRateType.ts, GenericIntervalType.ts) calls helper functions such as instanceOfnumber, numberFromJSONTyped, instanceOfstring, stringFromJSONTyped, numberToJSON, stringToJSON. However, these functions are not defined or imported anywhere within the scope of the generated files, leading to TS2304: Cannot find name '...' errors during TypeScript compilation.

This issue forces a switch to typescript-axios (which handles this correctly) or prevents the use of typescript-fetch for schemas with this common oneOf pattern.

openapi-generator version
  • @openapitools/openapi-generator-cli: 7.10.0 (as specified in openapitools.json).
  • This issue is a regression or a persistent bug, as similar behavior has been noted in comments on other issues for this version.
OpenAPI declaration file content or url

The issue occurs with schemas structured like the following (relevant snippets from an OpenAPI v3.0.1 spec, with type names made generic):

# openapi: 3.0.1
# info:
#   title: Example API
#   version: v1
components:
  schemas:
    NumericPercentageValue:
      type: number
      description: A percentage value (0 to 1).
    NumericTimeUnitValue:
      type: number
      description: A measure in a specific time unit.

    GenericRateType: # Problematic model
      description: Represents a rate, which can be a percentage or a sync keyword.
      oneOf:
        - $ref: '#/components/schemas/NumericPercentageValue'
        - type: string
          enum:
            - rate-sync-keyword

    GenericIntervalType: # Problematic model
      description: Represents a custom interval, as a numeric unit or an auto keyword.
      nullable: true # The generated TS type is number | string | null
      oneOf:
        - $ref: '#/components/schemas/NumericTimeUnitValue'
        - type: string
          enum:
            - interval-auto-keyword
Generation Details

The client SDK is generated using a command similar to:

openapi-generator-cli generate \
  -i path/to/your/api-spec.yml \
  -g typescript-fetch \
  -o gen/your-sdk-output-directory

From openapitools.json:

{
  "$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
  "spaces": 2,
  "generator-cli": {
    "version": "7.10.0"
  }
}
Steps to reproduce
  1. Create an OpenAPI (v3.0.1) specification that includes models using oneOf with a $ref to a primitive type (e.g., number) and a direct primitive type (e.g., string enum), as shown in the generic schema snippet above.

  2. Use [email protected] to generate a client library with the typescript-fetch generator.

  3. Attempt to compile the generated TypeScript code (e.g., using tsc as part of a build process: tsc && vite build).

  4. Actual Output: TypeScript compilation fails with TS2304: Cannot find name '...' errors for functions like instanceOfnumber, numberFromJSONTyped, instanceOfstring, stringFromJSONTyped, numberToJSON, stringToJSON in the generated model files (e.g., GenericRateType.ts).

    Example from a generated model file like GenericRateType.ts:

    // ...
    export function GenericRateTypeFromJSONTyped(json: any, ignoreDiscriminator: boolean): GenericRateType { // Generic type name
        if (json == null) {
            return json;
        }
        // ERROR: TS2304: Cannot find name 'instanceOfnumber'.
        if (instanceOfnumber(json)) {
            // ERROR: TS2304: Cannot find name 'numberFromJSONTyped'.
            return numberFromJSONTyped(json, true);
        }
        // ERROR: TS2304: Cannot find name 'instanceOfstring'.
        if (instanceOfstring(json)) {
            // ERROR: TS2304: Cannot find name 'stringFromJSONTyped'.
            return stringFromJSONTyped(json, true);
        }
        // ...
    }
    // ... similar errors for ToJSON functions and in other affected models.
  5. Expected Output: The generated TypeScript code should compile successfully. The necessary helper functions for handling oneOf types should be either included in the generated files or imported from a runtime library provided by the typescript-fetch generator.

Related issues/PRs

This issue appears to be similar to, or a continuation of, problems reported in:

Suggest a fix

The typescript-fetch generator needs to ensure that the helper functions it relies upon for oneOf type resolution and (de)serialization (like instanceOfnumber, numberFromJSONTyped, stringFromJSONTyped, etc.) are correctly included or imported in the generated output. This is crucial for handling oneOf schemas involving primitive types.

The typescript-axios generator, when used with the same OpenAPI schema, does not exhibit this issue and produces compilable code. This suggests that a correct implementation path exists within the broader openapi-generator ecosystem and that the typescript-fetch generator might be missing these specific utilities in its runtime or template for this use case.

@tobq tobq added the Issue: Bug label May 9, 2025
@tobq
Copy link
Author

tobq commented May 9, 2025

Fixed in 7.13.0

@tobq tobq closed this as completed May 9, 2025
@SZenglein
Copy link

This is still a problem with 7.13:

This object used in an API call as a request param

      "FloorplanMultipart": {
        "type": "object",
        "description": "FloorplanMultipart is used to document the payload for creating a floorplan",
        "required": [
          "layout_file",
          "blueprint"
        ],
        "properties": {
          "blueprint": {
            "$ref": "#/components/schemas/FloorplanBlueprint"
          },
          "layout_file": {
            "$ref": "#/components/schemas/BinaryFile"
          }
        }
      },

results in the following snippet using an undefined stringToJSON:

if (requestParameters['blueprint'] != null) {
  formParams.append('blueprint', new Blob([JSON.stringify(stringToJSON(requestParameters['blueprint']))], { type: "application/json", }));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants