Skip to content

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

@adam-iris

Description

@adam-iris

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions