-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
[Question] [typescript-fetch] The date format (RFC 3339: full-date) has a time zone gap when parsing from JSON and when converting to JSON #5932
Comments
👍 Thanks for opening this issue! The team will review the labels and make any necessary changes. |
Hi @m-doi2, Two other solutions are the following : Write a "from scratch"method in runtime.js to convert Javascript "Date" type to ISO date string, maybe using this pattern : https://stackoverflow.com/a/25159403 Write a "hacky" method in runtime.js which will use toLocalDateString and transform to align with ISO format :
EDIT : Added some undefined/null check in the method. Yours faithfully, |
Thank you for your good solution! I don't have a philosophy on how to implement it at this time, but here are my own thoughts from a general perspective. Since this is an auto-generated code, I think it's these three points that are important.
That's why I like the method of adding a simple method to runtime. On the other hand, we don't want to use large libraries like moment.js as much as possible. I hope that a solution to this challenge will be implemented in the generator. |
Here is the relevant code line: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-fetch/modelGeneric.mustache#L107 Sadly i´m dont know how fix this line. At the moment i temporary fix the generated code with following lines of code: from
|
target: typescript-fetch
openapi-generator version: 4.3.0
For example, if we generate from the definition of requestBody like this:
Such a code will be generated in the models directory:
Since the date format of OpenAPI conforms to RFC 3339
full-date
, the time zone is not included in the original data string. ex: '2020-04-15'When converting this original date string with the
RequestBodyFromJSONTyped
method, specify this string directly in the parameter of new Date, so it will be optimized in the local time zone.I don't think this behavior is a problem.
But, when converting to JSON with the
RequestBodyToJSON
method, since it is converted to a string by toISOString, the UTC date is output.With this, the value changes between conversion and restoration.
I think it should stringify with the date in the timezone of the Date instance.
For example:
`${value.date.getFullYear()}-${(value.date.getMonth() + 1).toString().padStart(2, '0')}-${value.date.getDate().toString().padStart(2, '0')}`
Is this my perception correct? Or am I doing something wrong?
The text was updated successfully, but these errors were encountered: