[BUG][typescript-fetch] When using a openapi.json with AnyOf different date formats, invalid TS is generated
Created by: JakeSummers
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
Using this JSON:
{
"openapi": "3.0.2",
"info": {
"title": "My Application",
"version": "0.1.0"
},
"paths": {
"/a/": {
"get": {
"responses": {
"200": {
"description": "Successful Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Point"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Point": {
"title": "Point",
"required": [
"datetime",
"value"
],
"type": "object",
"properties": {
"datetime": {
"title": "Datetime",
"anyOf": [
{
"type": "string",
"format": "date"
},
{
"type": "string",
"format": "date-time"
},
{
"type": "string",
"format": "time"
}
]
},
"value": {
"title": "Value",
"type": "number"
}
}
}
}
}
}
When I run generate --input-spec ./src/generatedApis/openapi.json --generator-name typescript-fetch --output src/generatedApis/typescript-fetch
, it will generate invalid Typescript. This is the TS generated:
// tslint:disable
// eslint-disable
/**
* My Application
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
*
* The version of the OpenAPI document: 0.1.0
*
*
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
* https://openapi-generator.tech
* Do not edit the class manually.
*/
import { exists, mapValues } from '../runtime';
/**
*
* @export
* @interface Point
*/
export interface Point {
/**
*
* @type {string | Date}
* @memberof Point
*/
datetime: string | Date;
/**
*
* @type {number}
* @memberof Point
*/
value: number;
}
export function PointFromJSON(json: any): Point {
return PointFromJSONTyped(json, false);
}
export function PointFromJSONTyped(json: any, ignoreDiscriminator: boolean): Point {
if ((json === undefined) || (json === null)) {
return json;
}
return {
'datetime': string | DateFromJSON(json['datetime']),
'value': json['value'],
};
}
export function PointToJSON(value?: Point | null): any {
if (value === undefined) {
return undefined;
}
if (value === null) {
return null;
}
return {
'datetime': string | DateToJSON(value.datetime),
'value': value.value,
};
}
This has two main problems:
- This line:
'datetime': string | DateFromJSON(json['datetime']),
is invalid.string
is a type not a value. -
DateFromJSON
andDateToJSON
are never imported
Full errors from Typescript Compiler
/../Point.ts
Error:(46, 9) TS2322: Type 'number' is not assignable to type 'string | Date'.
Error:(46, 21) TS2693: 'string' only refers to a type, but is being used as a value here.
Error:(46, 30) TS2304: Cannot find name 'DateFromJSON'.
Error:(60, 21) TS2693: 'string' only refers to a type, but is being used as a value here.
Error:(60, 30) TS2304: Cannot find name 'DateToJSON'.
openapi-generator version
The version of my Jar is: 4.2.1
Command line used for generation
Generated using:
`npm bin`/openapi-generator generate --input-spec ./src/generatedApis/openapi.json --generator-name typescript-fetch --output src/generatedApis/typescript-fetch
Using: https://www.npmjs.com/package/@openapitools/openapi-generator-cli
Related issues/PRs
Maybe related: #4340
This issue changed how anyOf
was handled: #4387