You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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: v1components:
schemas:
NumericPercentageValue:
type: numberdescription: A percentage value (0 to 1).NumericTimeUnitValue:
type: numberdescription: A measure in a specific time unit.GenericRateType: # Problematic modeldescription: Represents a rate, which can be a percentage or a sync keyword.oneOf:
- $ref: '#/components/schemas/NumericPercentageValue'
- type: stringenum:
- rate-sync-keywordGenericIntervalType: # Problematic modeldescription: Represents a custom interval, as a numeric unit or an auto keyword.nullable: true # The generated TS type is number | string | nulloneOf:
- $ref: '#/components/schemas/NumericTimeUnitValue'
- type: stringenum:
- interval-auto-keyword
Generation Details
The client SDK is generated using a command similar to:
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.
Use [email protected] to generate a client library with the typescript-fetch generator.
Attempt to compile the generated TypeScript code (e.g., using tsc as part of a build process: tsc && vite build).
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:
// ...exportfunctionGenericRateTypeFromJSONTyped(json: any,ignoreDiscriminator: boolean): GenericRateType{// Generic type nameif(json==null){returnjson;}// ERROR: TS2304: Cannot find name 'instanceOfnumber'.if(instanceOfnumber(json)){// ERROR: TS2304: Cannot find name 'numberFromJSONTyped'.returnnumberFromJSONTyped(json,true);}// ERROR: TS2304: Cannot find name 'instanceOfstring'.if(instanceOfstring(json)){// ERROR: TS2304: Cannot find name 'stringFromJSONTyped'.returnstringFromJSONTyped(json,true);}// ...}// ... similar errors for ToJSON functions and in other affected models.
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:
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.
The text was updated successfully, but these errors were encountered:
Description
When using
openapi-generator-cli
version 7.10.0 with thetypescript-fetch
generator, models generated for OpenAPI schemas that utilizeoneOf
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 asinstanceOfnumber
,numberFromJSONTyped
,instanceOfstring
,stringFromJSONTyped
,numberToJSON
,stringToJSON
. However, these functions are not defined or imported anywhere within the scope of the generated files, leading toTS2304: Cannot find name '...'
errors during TypeScript compilation.This issue forces a switch to
typescript-axios
(which handles this correctly) or prevents the use oftypescript-fetch
for schemas with this commononeOf
pattern.openapi-generator version
@openapitools/openapi-generator-cli
: 7.10.0 (as specified inopenapitools.json
).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):
Generation Details
The client SDK is generated using a command similar to:
From
openapitools.json
:Steps to reproduce
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.Use
[email protected]
to generate a client library with thetypescript-fetch
generator.Attempt to compile the generated TypeScript code (e.g., using
tsc
as part of a build process:tsc && vite build
).Actual Output: TypeScript compilation fails with
TS2304: Cannot find name '...'
errors for functions likeinstanceOfnumber
,numberFromJSONTyped
,instanceOfstring
,stringFromJSONTyped
,numberToJSON
,stringToJSON
in the generated model files (e.g.,GenericRateType.ts
).Example from a generated model file like
GenericRateType.ts
: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 thetypescript-fetch
generator.Related issues/PRs
This issue appears to be similar to, or a continuation of, problems reported in:
...ToJSONTyped
functions but models referencing still try to import #19858 ([BUG] [typescript-fetch] oneOf models are generated without...ToJSONTyped
functions but models referencing still try to import) - A user specifically commented that this affects v7.10.0.oneOf
handling intypescript-fetch
as discussed in older issues like [BUG] [TypeScript-Fetch] Incorrectly generated models when using oneOf #14763.Suggest a fix
The
typescript-fetch
generator needs to ensure that the helper functions it relies upon foroneOf
type resolution and (de)serialization (likeinstanceOfnumber
,numberFromJSONTyped
,stringFromJSONTyped
, etc.) are correctly included or imported in the generated output. This is crucial for handlingoneOf
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 thetypescript-fetch
generator might be missing these specific utilities in its runtime or template for this use case.The text was updated successfully, but these errors were encountered: