Skip to content

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

Open
@micolous

Description

@micolous

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}};
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions