OpenAPITools / OpenAPITools/openapi-generator

[BUG] [typescript-fetch] Generated code not compatible with typescript flag "exactOptionalPropertyTypes"

Open
#14,793 2 comments 8 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

Typescript has various flags, some of which help you to write code many of which add more type-safety. One of which is the option exactOptionalPropertyTypes which, if enabled, ensures that properties of an object, if optional, are not allowed to get assigned undefined unless it's explicitly stated in their type-definition.

The generator typescript-fetch generates code which is incompatible with this flag, that means, if an object property is defined as optional with the type null | string, the value undefined is assigned to it. When this flag is enabled, the types should either be changed to undefined | null | string or the assignment should be changed to set the value to null instead.

openapi-generator version

v6.4.0

OpenAPI declaration file content or url
{
  "$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
  "spaces": 2,
  "generator-cli": {
    "version": "6.4.0",
    "generators": {
      "v2.0": {
        "generatorName": "typescript-fetch",
        "inputSpec": "./swagger.json",
        "output": "./generated/api"
      }
    }
  }
}
Generation Details

The problem occurs for any swagger file, because even the basic infrastructure provided by the generator generates code not compliant with this rule.

I've also found it sometimes generating invalid code for types within type-definitions. A sample of which can be found by using the following swagger.json file: https://gist.github.com/SimonSimCity/350234540ab5363a7bef7244842cfb57

Steps to reproduce
Related issues/PRs

None found.

Suggest a fix

Because the usage of the flag exactOptionalPropertyTypes is recommended (see: https://www.typescriptlang.org/tsconfig#exactOptionalPropertyTypes) this generator should IMO comply with this rule.

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 with the typescript-fetch generator and reproduce the issue using the referenced swagger.json configuration and sample declaration from the linked gist, with TypeScript's exactOptionalPropertyTypes enabled. Check both the generated infrastructure and type definitions; done means generated code compiles without assigning undefined to optional properties unless their types explicitly allow it.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.