OpenAPITools / OpenAPITools/openapi-generator

[BUG] [typescipt-axios] Generated code fails with "exactOptionalPropertyTypes" compiler option

Open
#22,153 0 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?
  • 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 TypeScript compiler option exactOptionalPropertyTypes prevents an object property from being assigned an undefined value unless this is specifically indicated in the property type declaration. For example, in an interface declared like:

export interface Example {
    attr1?: string | null;
}

it is legal to omit attr1 from an object, but illegal to set its value to undefined. The interface must instead be declared like:

export interface Example {
    attr1?: string | null | undefined;
}

The generator currently declares optional attributes without including the undefined value, which generates errors when this flag is set in the TypeScript compiler.

openapi-generator version

7.16.0
Probably no regression

OpenAPI declaration file content or url

This occurs on any API definition with optional properties. See below for reproduction steps.

Generation Details
Steps to reproduce
  • modify samples/client/petstore/typescript-axios/builds/with-complex-headers/tsconfig.json and add
    "strictNullChecks": true,
    "exactOptionalPropertyTypes": true,

to the compilerOptions

  • run ./bin/ts-typecheck-all.sh
Related issues/PRs

#14793
#20450

Suggest a fix

Update templates to produce | undefined type values

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 Axios templates that generate optional property types, using samples/client/petstore/typescript-axios/builds/with-complex-headers/tsconfig.json and bin/ts-typecheck-all.sh as the reproduction entry points. Confirm the failure with strictNullChecks and exactOptionalPropertyTypes enabled. Done means the generated sample passes the type-check script without optional-property errors.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.