OpenAPITools / OpenAPITools/openapi-generator

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

Open
#21,005 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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:

https://github.com/OpenAPITools/openapi-generator/blob/587fcff85c87c135a691988227fbee3216e4876b/samples/client/petstore/typescript-fetch/builds/default/models/User.ts#L100-L107

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 (587fcff85c87c135a691988227fbee3216e4876b)

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 https://github.com/OpenAPITools/openapi-generator/pull/19524

Suggest a fix

Change this:

https://github.com/OpenAPITools/openapi-generator/blob/587fcff85c87c135a691988227fbee3216e4876b/modules/openapi-generator/src/main/resources/typescript-fetch/modelOneOf.mustache#L44-L46

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:

https://github.com/OpenAPITools/openapi-generator/blob/587fcff85c87c135a691988227fbee3216e4876b/modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache#L111-L113

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

https://github.com/OpenAPITools/openapi-generator/blob/587fcff85c87c135a691988227fbee3216e4876b/modules/openapi-generator/src/main/resources/typescript-fetch/modelEnum.mustache#L22-L28

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Inspect modelOneOf.mustache, modelGeneric.mustache, and modelEnum.mustache in modules/openapi-generator/src/main/resources/typescript-fetch, comparing each ToJSON and ToJSONTyped signature with the generated User.ts example. Regenerate the Petstore typescript-fetch samples and confirm the signatures accept and return the appropriate model or JSON types, including special serialization cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.