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
21.58k stars 6.52k forks source link

[BUG][TYPESCRIPT FETCH] Nullable dates produce compilation error #3807

Open paunovic-stefan opened 5 years ago

paunovic-stefan commented 5 years ago

Bug Report Checklist

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

auto-labeler[bot] commented 5 years ago

👍 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.

macjohnny commented 5 years ago

would you like to fix this here? https://github.com/OpenAPITools/openapi-generator/blob/d21b3390feeb0cf0eab44e412fd50eb0b66cc1e6/modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache#L119