OpenAPITools / OpenAPITools/openapi-generator
[BUG][typescript-fetch] When using a openapi.json with AnyOf different date formats, invalid TS is generated
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
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.stringis a type not a value. DateFromJSONandDateToJSONare 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
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
Reproduce the report with the supplied OpenAPI document and the typescript-fetch generator, then inspect the generated Point.ts output and its TypeScript compiler errors. The fix is complete when the anyOf date field produces valid TypeScript and the required date conversion helpers are available in the generated file.
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
- Clearly specified
- Newbie friendliness
- 45/100