OpenAPITools / OpenAPITools/openapi-generator
[BUG] [typescript-fetch] regression: modelToJSON method has incorrect type signature
Nobody has claimed this yet.
- 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:
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:
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:
modelEnum has the reverse issue: ToJSON is correct, but ToJSONTyped has the wrong type information:
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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