OpenAPITools / OpenAPITools/openapi-generator

[BUG] [typescript-fetch] Type mappings not applied to "helper functions"

Open
#7,990 4 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: TypeScript 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

When generating the API definition with typescript-fetch and using "--type-mappings=Date=string", the types themselves get mapped correctly, but the helper functions are not adjusted accordingly.

I'd expect the helper functions to be updated to fit the new type.

export interface ApplicationState {
    * @type {string}
    * @memberof ApplicationState
    */
   enteredStateAt: string;
}

export function ApplicationStateFromJSONTyped(json: any, ignoreDiscriminator: boolean): ApplicationState {
   if ((json === undefined) || (json === null)) {
       return json;
   }
   return {
      // TYPESCRIPT COMPLAINS HERE
       'enteredStateAt': (new Date(json['enteredStateAt'])),
   };
}

export function ApplicationStateToJSON(value?: ApplicationState | null): any {
   if (value === undefined) {
       return undefined;
   }
   if (value === null) {
       return null;
   }
   return {
      // TYPESCRIPT COMPLAINS HERE
       'enteredStateAt': (value.enteredStateAt.toISOString()),
   };
}
openapi-generator version

"@openapitools/openapi-generator-cli": "^2.1.5",

Generation Details

openapi-generator generate -i [api.yml url] -g typescript-fetch -o ./src/.api-connector/ --skip-validate-spec --additional-properties modelPropertyNaming=original --type-mappings=Date=string

Steps to reproduce
  1. Generate typescript-fetch API definition with type-mapping Date => string.
  2. See Typescript gloriously complain.

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

Start by running the shown openapi-generator command with the typescript-fetch generator and --type-mappings=Date=string. Inspect the generated ApplicationStateFromJSONTyped and ApplicationStateToJSON helper functions alongside the mapped property type. Done means the generated helpers use the mapped string representation and the generated TypeScript no longer reports the shown type errors.

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
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.