OpenAPITools / OpenAPITools/openapi-generator

[BUG] [typescript-fetch] request body containing array of objects generates typescript not allowed in strict-mode

Open
#14,792 2 comments 0 reactions 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 code generated for an API POST-call having an array of objects as its body, is not valid if strict mode is enabled in typescript.

openapi-generator version

v6.4.0

OpenAPI declaration file content or url

https://gist.github.com/SimonSimCity/e3d86c8f4a0f16d0e329dbf2d593bf94

Generation Details
{
  "$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
  "spaces": 2,
  "generator-cli": {
    "version": "6.3.0",
    "generators": {
      "v2.0": {
        "generatorName": "typescript-fetch",
        "inputSpec": "./swagger.json",
        "output": "./generated/api"
      }
    }
  }
}
Steps to reproduce

Run the following commands in a directory containing the provided swagger.json file and the openapitools.json:

  1. npx openapi-generator-cli generate
  2. npx tsc --strict --target ESNext --moduleResolution node generated/api/index.ts
Related issues/PRs

--

Suggest a fix

The value of the property requestParameters.statisticsTrackPlayedPostRequestInner might be undefined, which could be solved by using optional chaining by adding a question mark before .map(. I just don't know whether this would still be compatible with the api specification or if the problem rather is that this actually can be undefined as of its definition ...

        const response = await this.request({
            path: `/Statistics/track/played`,
            method: 'POST',
            headers: headerParameters,
            query: queryParameters,
            body: requestParameters.statisticsTrackPlayedPostRequestInner.map(StatisticsTrackPlayedPostRequestInnerToJSON),
            //                                                           ^ Changing to "?.map(" might solve the issue already
        }, initOverrides);

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

Run the commands from the issue with swagger.json and openapitools.json, then inspect generated/api/index.ts and the typescript-fetch generator output around the request body mapping. Confirm the generated TypeScript fails under npx tsc --strict --target ESNext --moduleResolution node; done means the same generated client compiles successfully in strict mode.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.