Openapi-generator: typescript-axios generator string property with date-time format needs to remain a string

Created on 5 Sep 2019  路  4Comments  路  Source: OpenAPITools/openapi-generator

Description

Using typescript-axios generator, I'm getting some undesirable behavior:

For an API with a model property defined as type string with a format of date-time, the generated interface has a type of Date instead of string.

We have serious issues with time zones when converting the strings we receive from and pass to the API to a Date object on the client that are easily avoided by keeping the values as strings.

openapi-generator version

0.0.17-4.1.1

OpenAPI declaration file content or url

Here is the full document: https://gist.github.com/wpatter6/dfe34db36300258fbabf7cbf102a0fff

Here is an example property causing the issue:

{
  "startDate": {
    "type": "string",
    "description": "Beginning of the date range",
    "format": "date-time"
  }
}

this results in

export interface ModelsFakeRequest {
    /**
     * Beginning of the date range
     * @type {Date}
     * @memberof ModelsFakeRequest
     */
    startDate: Date;
}
Command line used for generation

openapi-generator generate -i swagger/swagger.json -g typescript-axios -o src/api/generated -t swagger/generator-templates/

Steps to reproduce
  1. Have open api document with above defined property in a schema
  2. Generate typescript-axios
  3. See output interfaces for undesired results.
Related issues/PRs

Appears to be related to these issues:
https://github.com/OpenAPITools/openapi-generator/issues/926
https://github.com/OpenAPITools/openapi-generator/issues/2745

Suggest a fix/enhancement

Perhaps a CLI flag or config setting could allow defining the desired behavior of the Date fields. I'd like for it to result in startDate?: string or startDate?: Date | string would be fine as well. In any case it's probably useful to allow the consumer to be able to determine the date format string. I believe the fixes in the above issues would need type checking to prevent runtime errors as well.

Thanks!

TypeScript

Most helpful comment

Thanks @macjohnny that got me to what I need, with a little trial and error it looks like the parameter to switch JS Date objects to strings is --type-mappings Date=string. This works for me, closing issue

All 4 comments

馃憤 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.

you could pass a custom type-mapping --type-mappings date=string,date-time=string, see https://openapi-generator.tech/docs/usage#generate

Thanks @macjohnny that got me to what I need, with a little trial and error it looks like the parameter to switch JS Date objects to strings is --type-mappings Date=string. This works for me, closing issue

The funny thing is that even with --type-mappings Date=string mapping functions *FromJSON e.t.c. are still calling for example Date(json['Now'])

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Fredx87 picture Fredx87  路  34Comments

guidoschreuder picture guidoschreuder  路  21Comments

wing328 picture wing328  路  25Comments

MartinDelille picture MartinDelille  路  35Comments

pellyadolfo picture pellyadolfo  路  21Comments