swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.88k stars 6.02k forks source link

[JavaScript] Invalid date parsing for all browsers. #3069

Open Danielku15 opened 8 years ago

Danielku15 commented 8 years ago
Description

We are using the JavaScript code generator in combination with browserify. It brings us some benefits to have some "static code"-generation for JavaScript. The problem is that the date parsing function is not working for all browsers. Not all browsers have proper ISO-8601 parsing capabilities. Only Chrome parses the dates correctly.

https://github.com/swagger-api/swagger-codegen/blob/8f258b9a485076333c2dcf358675ed113ecdb906/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache#L413

Swagger-codegen version

2.1.6

Swagger declaration file content or url

Any date-time parameter. e.g. http://petstore.swagger.io/#!/store/getOrderById

Command line used for generation
java -jar swagger-codegen.jar generate -i http://petstore.swagger.io/v2/swagger.json -l JavaScript - o jsclient 
cd jsclient
npm install
cd ..
browserify jsclient\src\index.js -o client.js --standalone PetStoreClient

https://github.com/swagger-api/swagger-codegen/blob/8f258b9a485076333c2dcf358675ed113ecdb906/samples/client/petstore/javascript/src/ApiClient.js#L397

Steps to reproduce
  1. Generate the client
  2. Use the client.js in the browser to make a call to getOrderById
  3. Inspect the shipDate
    Related issues

No

Suggest a Fix

One solution could be to use moment.js to parse the date. An other solution might be to parse the ISO date manually with some regex. https://www.safaribooksonline.com/library/view/javascript-cookbook/9781449390211/ch03s05.html

Considering only IE9 and higher all browsers support out of the box ISO8601 parsing. This means even new Date(str) would work.

wing328 commented 8 years ago

@Danielku15 thanks for reporting the issue with details and suggesting a few solutions to tackle the problem

From what I read (http://stackoverflow.com/questions/5802461/javascript-which-browsers-support-parsing-of-iso-8601-date-string-with-date-par), seems like the implementation is not consistent across browsers and therefore I prefer moment.js.

May I know if you've cycle to contribute the enhancements?

Ref: https://github.com/swagger-api/swagger-codegen/blob/master/CONTRIBUTING.md

Danielku15 commented 8 years ago

ISO8601 date parsing became part of browsers in ECMAScript version 5. Considering this source and take the Date.prototype.toISOString as indicactor on ISO date parsing support, all browser should support it with normal new Date("str") instead of the T removal which breaks the parsing on most of the browsers.

http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.15 http://caniuse.com/#feat=es5 http://kangax.github.io/compat-table/es5/ http://stackoverflow.com/questions/10005374/ecmascript-5-date-parse-results-for-iso-8601-test-cases

wing328 commented 8 years ago

@Danielku15 if you feel more comfortable with the new Date("str") solution, please go with that.

wing328 commented 8 years ago

@Danielku15 may I know if you're working on this particular issue? Do you need any help from the community?

YetAnotherMinion commented 8 years ago

@Danielku15 If you are not working on this I'd like to go ahead and take a crack at it.

wing328 commented 7 years ago

@YetAnotherMinion may I know if you've started working on this? I can show you some good starting points.

YetAnotherMinion commented 7 years ago

I had not started on this (unusual suspect of work) but I have free time now to look at it. Thanks for the reminder.

On Feb 15, 2017, at 9:01 AM, wing328 notifications@github.com wrote:

@YetAnotherMinion may I know if you've started working on this? I can show you some good starting points.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

wing328 commented 7 years ago

@YetAnotherMinion no problem :)

Just let us (the community) know if you need anything from us.

eerne commented 7 years ago

We we just ran into the same issue, as the 'T' removal returns an Invalid Date and breaks Safari 10.0.3 (Mac OSX 10.12.3)

new Date("2017-12-31T00:00:00".replace(/T/i, ' '))
// returns Invalid Date

https://github.com/swagger-api/swagger-codegen/blob/8f258b9a485076333c2dcf358675ed113ecdb906/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache#L414

wing328 commented 7 years ago

@eerne a workaround is to modify the template and pass the customized templates via the -t option.

Would you have time to contribute the enhancement using moment.js? If yes, I can work with you on that one.

wing328 commented 7 years ago

@ibwhite's suggestion to use date-fns instead: https://github.com/swagger-api/swagger-codegen/pull/5417#issuecomment-296295765

mrdj07 commented 6 years ago

Hi, wondering if there's any news about this issue. I don't really why we should keep the stripping of the "T" character in the date for JS.

wing328 commented 6 years ago

@mrdj07 I don't think anyone is working on it. Please feel free to submit a PR with date-fns or other solutions. Let us know if you need any help.

dan3721 commented 6 years ago

7822 JS ApiClient Date Parsing

heartforit commented 5 years ago

Hi @All, I've got a very simple fix for this. Someone still need this?

Best,

binaryangel-noa commented 5 years ago

Hi @ALL, I've got a very simple fix for this. Someone still need this?

Best,

Please do so... i just ran into this issue on Safari. Its most shocking that it has not been fixed after 3 years.

heartforit commented 5 years ago

Hi @ALL, I've got a very simple fix for this. Someone still need this? Best,

Please do so... i just ran into this issue on Safari. Its most shocking that it has not been fixed after 3 years.

Hi, I created gist`s for you:

Place this file in an directory called "template" https://gist.github.com/heart4hackers/babbdf0cab93231f465dbd9012b470a7

Here after run this command: https://gist.github.com/heart4hackers/73f025a741f53a56a120b1fa55c99730

If you have any questions - feel free. Super man ist in place.

osmedd commented 5 years ago

Perhaps this is simple minded but is there any reason at this late date to not just do a Date.parse(str) here? That seems to work in recent versions of all the major browsers we have tested, more browsers than I have observed when you strip the T out.

That seems like a win, no?