OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
20.59k stars 6.29k forks source link

[typescript-fetch] - Fix Nullable Types #18887

Closed l0gicgate closed 3 weeks ago

l0gicgate commented 3 weeks ago

https://github.com/OpenAPITools/openapi-generator/pull/17798 introduced a breaking change where the distinction between undefined and nullable was lost.

The original contributor of this PR explained his rationale here for doing this, which is wrong: https://github.com/OpenAPITools/openapi-generator/pull/17798#discussion_r1490651282

Take the instance where we do a PATCH request and I want to partially update an entity. That means I would be able to pass in a partial object with subset of the fields.

Here's a definition of what a patch could look like, you can optionally pass any of the parameters. In this case, the subTitle field is also nullable, which would make it null in the database if I wanted to erase it:

export interface ArticlePatch {
    title?: string;
    subTitle?: string | null;
}

Scenario 1: Patching title only, I don't pass in subTitle

PATCH /articles/1
{
  "title": "My title",
}

Scenario 2: Patching subTitle only, making it null. Now I can't do this, because the types are broken:

PATCH /articles/1
{
  "subTitle": null,
}

subTitle is nullable: true in swagger.json:

{
  "subTitle": {
    "type": "string",
    "nullable": true
  }
}

In previous versions 7.3.0 and down the output was this:

export interface ArticlePatch {
    title?: string;
    subTitle?: string | null;
}

Since this PR has been merged:

export interface ArticlePatch {
    title?: string;
    subTitle?: string; // No longer nullable
}

PR checklist