OpenAPITools / OpenAPITools/openapi-generator
[BUG][TYPESCRIPT FETCH] Nullable dates produce compilation error
Nobody has claimed this yet.
- 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 (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
generated code failed to compile with the following error
Object is possibly 'null'. TS2531
'documentTypeName': value.documentTypeName,
'documentNo': value.documentNo,
'issueDate': value.issueDate === undefined ? undefined : value.issueDate.toISOString(),
^
};
}
the generated interface for the value variable is as follows
export interface BillingDocumentReferenceMetadata {
/**
* Document type, e.g. invoice, credit note, order, etc.
* @type {string}
* @memberof BillingDocumentReferenceMetadata
*/
documentType?: BillingDocumentReferenceMetadataDocumentTypeEnum;
/**
* Document type name, e.g. invoice, credit note, order, etc.
* @type {string}
* @memberof BillingDocumentReferenceMetadata
*/
documentTypeName?: string;
/**
* Document number.
* @type {string}
* @memberof BillingDocumentReferenceMetadata
*/
documentNo?: string;
/**
* Document issue date (optional).
* @type {Date}
* @memberof BillingDocumentReferenceMetadata
*/
issueDate?: Date | null;
}
export function BillingDocumentReferenceMetadataToJSON(value?: BillingDocumentReferenceMetadata): any {
if (value === undefined) {
return undefined;
}
return {
'documentType': value.documentType,
'documentTypeName': value.documentTypeName,
'documentNo': value.documentNo,
'issueDate': value.issueDate === undefined ? undefined : value.issueDate.toISOString(),
};
}
openapi-generator version
version 4.1.1
OpenAPI declaration file content or url
Part of the swagger.json file that describes the BillingDocumentReferenceMetadata
"BillingDocumentReferenceMetadata": {
"type": "object",
"properties": {
"documentType": {
"enum": [
"Invoice",
"CreditNote",
"Order",
"Unspecified"
],
"type": "string",
"description": "Document type, e.g. invoice, credit note, order, etc."
},
"documentTypeName": {
"type": "string",
"description": "Document type name, e.g. invoice, credit note, order, etc."
},
"documentNo": {
"type": "string",
"description": "Document number."
},
"issueDate": {
"type": "string",
"description": "Document issue date (optional).",
"format": "date-time",
"nullable": true
}
},
"additionalProperties": false,
"description": "Metadata of the reference to another business document."
},
Command line used for generation
npx openapi-generator generate -i http://localhost:5003/swagger/v1/swagger.json -g typescript-fetch -o ./src/ApiClient
Steps to reproduce
Generate a typescript fetch client from a json file that contains a nullable date
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by generating a typescript-fetch client with the nullable date schema and run TypeScript compilation on the generated output. Inspect the generated BillingDocumentReferenceMetadataToJSON function and verify that the nullable issueDate case compiles successfully while preserving the reported serialization behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- openapi, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100