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

[BUG] [typescript-fetch] regression: modelToJSON method has incorrect type signature #21005

Open
5 of 6 tasks
micolous opened this issue Apr 1, 2025 · 0 comments
Open
5 of 6 tasks

Comments

@micolous
Copy link
Contributor

micolous commented Apr 1, 2025

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The typescript-fetch generator builds a ModelToJSON and ModelToJSONTyped method for each model type, for example:

export function UserToJSON(json: any): User {
return UserToJSONTyped(json, false);
}
export function UserToJSONTyped(value?: User | null, ignoreDiscriminator: boolean = false): any {
if (value == null) {
return value;
}

In the example above, UserToJSON calls UserToJSONTyped.

UserToJSONTyped’s signature is correct: it takes a value: User? | null, and returns an any.

UserToJSON’s signature is incorrect: it takes the same arguments and returns the same values, but it is declared as taking a json: any (rather than value: User? | null) and returning a User.

While in the simple case, User and the “JSON User” models are equivalent, this is not true if a field needs special handling when serialising or deserialising. For example, a Date field expressed as an ISO 7816 timestamp (string) or integer timestamp in JSON.

openapi-generator version

Current HEAD (587fcff)

OpenAPI declaration file content or url

Petstore example in openapi-generator repository

Generation Details

Visible in all of the example typescript-fetch builds in the openapi-generator repository.

Steps to reproduce

As above

Related issues/PRs

This regression seems like it was introduced in #19524

Suggest a fix

Change this:

export function {{classname}}ToJSON(json: any): any {
return {{classname}}ToJSONTyped(json, false);
}

To:

export function {{classname}}ToJSON(value?: {{classname}} | null ): any {
    return {{classname}}ToJSONTyped(value, false);
}

And change this in a similar way, copying its ToJSONTyped method signature:

export function {{classname}}ToJSON(json: any): {{classname}} {
return {{classname}}ToJSONTyped(json, false);
}

modelEnum has the reverse issue: ToJSON is correct, but ToJSONTyped has the wrong type information:

export function {{classname}}ToJSON(value?: {{classname}} | null): any {
return value as any;
}
export function {{classname}}ToJSONTyped(value: any, ignoreDiscriminator: boolean): {{classname}} {
return value as {{classname}};
}

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

1 participant