Repository navigation
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)?
- 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: trueCommand 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
auto-labeler commentedon Mar 23, 2020
π 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.
iliastsa commentedon Jul 25, 2023
I can confirm this problem is still present
fix(typescript-fetch): optional nullable fields return null instead oβ¦
fix(typescript-fetch): optional nullable fields return null instead oβ¦
fix(typescript-fetch): optional nullable fields return null instead oβ¦
fix(typescript-fetch): optional nullable fields return null instead oβ¦