Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

typescript-axios generator string property with date-time format needs to remain a string #3842

Closed
wpatter6 opened this issue Sep 5, 2019 · 5 comments

Comments

@wpatter6
Copy link

wpatter6 commented Sep 5, 2019

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:
#926
#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!

@auto-labeler
Copy link

auto-labeler bot commented Sep 5, 2019

👍 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
Copy link
Member

macjohnny commented Sep 9, 2019

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

@wpatter6
Copy link
Author

wpatter6 commented Sep 9, 2019

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

@wpatter6 wpatter6 closed this as completed Sep 9, 2019
@sherlock1982
Copy link

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'])

@Leshe4ka
Copy link

Leshe4ka commented Apr 27, 2022

Anyone solved this problem? I have got a lot of warnings about mapping functions *FromJSON, after submitting --type-mappings=DateTime=string flag

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants