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.
0.0.17-4.1.1
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;
}
openapi-generator generate -i swagger/swagger.json -g typescript-axios -o src/api/generated -t swagger/generator-templates/
typescript-axiosAppears to be related to these issues:
https://github.com/OpenAPITools/openapi-generator/issues/926
https://github.com/OpenAPITools/openapi-generator/issues/2745
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!
馃憤 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'])
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