Skip to content

[BUG] [typescript-fetch] null output shadowed for optional, nullable valuesΒ #5670

Description

@samuelallan72

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

When the api spec object has a field that is not required and is x-nullable, the generator produces a definition that allows null, but will never set null. For example, the included spec.yaml (below) will produce a Profile definition as follows:

export interface Profile {
    dateOfBirth?: Date | null;
}

export function ProfileFromJSONTyped(json: any, ignoreDiscriminator: boolean): Profile {
    if ((json === undefined) || (json === null)) {
        return json;
    }
    return {
        
        'dateOfBirth': !exists(json, 'date_of_birth') ? undefined : (json['date_of_birth'] === null ? null : new Date(json['date_of_birth'])),
    };
}

...

exists(x) will return false if x is undefined or null, so ProfileFromJSON will never return a Profile object with dateOfBirth set to null, even though the logic suggests it's trying to.

In 4.0.0, null was not included in the type signature of dateOfBirth, and null and undefined values were converted to undefined always. This was desirable behaviour for us.

openapi-generator version

4.2.3

OpenAPI declaration file content or url

spec.yaml:

swagger: '2.0'
info:
  title: test
  version: v1
basePath: /api/v1
consumes:
  - application/json
produces:
  - application/json
paths:
  /users/profile:
    get:
      operationId: user_profile
      description: Get my profile
      parameters: []
      responses:
        '200':
          description: ''
          schema:
            $ref: '#/definitions/Profile'
definitions:
  Profile:
    type: object
    properties:
      date_of_birth:
        title: Date of birth
        type: string
        format: date
        x-nullable: true
Command line used for generation
Steps to reproduce
Related issues/PRs
Suggest a fix

Ideally, this should revert to the old behaviour of always converting null and undefined values to undefined. If this is undesirable, then the generator should generate an expression like:

'dateOfBirth': json['date_of_birth' === undefined ? undefined : (json['date_of_birth'] === null ? null : new Date(json['date_of_birth'])),

or

'dateOfBirth': !exists(json, 'date_of_birth') ? json['date_of_birth'] : new Date(json['date_of_birth']),

instead of the current:

'dateOfBirth': !exists(json, 'date_of_birth') ? undefined : (json['date_of_birth'] === null ? null : new Date(json['date_of_birth'])),

Because the sequence of checks will never let the null value pass through.

Activity

  1. auto-labeler commented on Mar 23, 2020

    @auto-labeler

    πŸ‘ Thanks for opening this issue!
    🏷 I have applied any labels matching special text in your issue.

    The team will review the labels and make any necessary changes.

  2. iliastsa commented on Jul 25, 2023

    @iliastsa

    I can confirm this problem is still present

  3. added 3 commits that reference this issue on Jul 6, 2026
    d4fd8e4
    f6f6655
    1196727
  4. added a commit that references this issue on Jul 6, 2026
    efb214e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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