question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG][typescript-fetch] When using a openapi.json with AnyOf different date formats, invalid TS is generated

See original GitHub issue

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:

  1. This line: 'datetime': string | DateFromJSON(json['datetime']), is invalid. string is a type not a value.
  2. DateFromJSON and DateToJSON 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

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:14
  • Comments:6

github_iconTop GitHub Comments

3reactions
hcocommented, Aug 6, 2021

Does anyone have a workaround for this?

0reactions
tusharchutanicommented, Jun 30, 2021

any updates on this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

oneOf, anyOf, allOf, not - Swagger
The following JSON object is not valid against both schemas, ... OpenAPI lets you combine and extend model definitions using the allOf keyword....
Read more >
OpenAPI generator: wrong format for the example value of ...
I found out that definitions containing datatypes of date with example values produce differently formatted values in the output Java models.
Read more >
Using with TypeScript - Ajv JSON schema validator
JSONSchemaType will type check unions where each union element is fully specified as an element of an anyOf array or oneOf array. Additionally,...
Read more >
TypeScript Type Safety with AJV Standalone
Another Json Validator (AJV) uses JSON Schema to define the types. ... can generate/export TS native types from entities like an OpenAPI ......
Read more >
Model | Ts.ED - A Node.js and TypeScript Framework on top of ...
The Model will generate a JsonSchema which can be used by modules ... Numbers can be restricted to a multiple of a given...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found