OpenAPITools / OpenAPITools/openapi-generator
[BUG][typescript-fetch] Missing type mappers if `generateAliasAsModel` is set to `true`
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
In an openapi.yml file we have schemas that are arrays of other schemas. If we set the option generateAliasAsModel to true, the typescript-fetch generator's generated output no longer maps the array's items, as it does with any other JSON, but just returns the raw JSON. This is a problem especially if the JSON contains date string values that should be mapped to a Date object in TypeScript.
openapi-generator version
I tried it with 7.1.0 and 7.2.0, and also with the latest master (at this time) of this repository.
OpenAPI declaration file content or url
I uploaded an openapi.yml to PasteBin https://pastebin.com/etjz4Uhs
Generation Details
openapi-generator-cli generate -i ./openapi.yml -o ./sdk/src -g typescript-fetch --additional-properties=disallowAdditionalPropertiesIfNotPresent=false --additional-properties=supportsES6=true --additional-properties=generateAliasAsModel=true
Steps to reproduce
Execute the generator command from above. The generated file sdk/src/models/CarList.ts contains this code:
export function CarListFromJSONTyped(json: any, ignoreDiscriminator: boolean): CarList {
return json;
}
This function should not return the JSON as-is, but should call the function CarListItemFromJSON() from CarListItem.ts for every item of the array.
If you remove the generateAliasAsModel option from the generate command, the getCarsRaw() function from DefaultApi.ts does this itself.
Related issues/PRs
I didn't find any related Issue or PR for this.
Suggest a fix
I think the bug is in the file modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache.
Lines 100 to 102 contain this code:
{{^hasVars}}
return json;
{{/hasVars}}
As far as I understand this code, it is correct that the generator enters the not hasVars case. But inside this case, there should be another check if the content of the JSON is an array and if that array contains only primitive values (string, number, bool) or other objects. In the case of objects the generated code should call the specific mapper for the specific type.
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
Start with modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache and reproduce the issue with the documented openapi-generator-cli command. Inspect the generated sdk/src/models/CarList.ts and compare it with the output when generateAliasAsModel is omitted. Done means array items use CarListItemFromJSON() rather than returning raw JSON.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 52/100