Skip to content

[BUG][typescript-fetch] Missing type mappers if generateAliasAsModel is set to true #17533

@vekunz

Description

@vekunz

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions