Skip to content

[TypeScript][Fetch] only call toISOString() if date object#9594

Merged
HugoMario merged 5 commits intoswagger-api:masterfrom
tsiq-jeremy:typescript-date-format
May 8, 2020
Merged

[TypeScript][Fetch] only call toISOString() if date object#9594
HugoMario merged 5 commits intoswagger-api:masterfrom
tsiq-jeremy:typescript-date-format

Conversation

@tsiq-jeremy
Copy link
Contributor

@tsiq-jeremy tsiq-jeremy commented Jul 22, 2019

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\.
  • Filed the PR against the correct branch: 3.0.0 branch for changes related to OpenAPI spec 3.0. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.

Description of the PR

@mhardorf @leonyu @roni-frantchi @bherila

Related to this issue. When using TypeScript Fetch and passing parameters to a "Date" field, they expect a type of "string", but then toISOString() is called on it, which causes the function to fail. Same issue on swagger-codegen.

Example json swagger:

        "parameters": [
          {
            "name": "date",
            "in": "query",
            "type": "string",
            "format": "date"
          }
        ]

in In `AbstractTypeScriptClientCodegen.java the Typings map:

typeMapping.put("date", "string");
typeMapping.put("DateTime", "Date");

template issue:

{{#isDate}}
queryParameters['{{baseName}}'] = (requestParameters.{{paramName}} as any).toISOString();
{{/isDate}}

requestParameters.{{paramName}} can never be a Date, since it has to be a string.

@tsiq-jeremy
Copy link
Contributor Author

Is there a technical committee to review this?

@fabricio-godoi
Copy link

Is there any chance that this PR would be merged any time soon? I would contribute myself if I had not seen this PR, but since it is already created I would like to see this feature in production.

I'm still getting this error from the editor.swagger.io client generator.

{{#isDate}}
localVarQueryParameter['{{baseName}}'] = ({{paramName}} as any).toISOString();
localVarQueryParameter['{{baseName}}'] = ({{paramName}} as any instanceof Date) ?
({{paramName}} as any).toISOString().substr(0,10) :

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a notice, if the param type is "datetime" the string length should surpass 10 characters since it contains the timestamp as well.
For instance: "2014-01-01T23:28:56.782Z" or even "2014-02-01T09:28:56.321-10:00"

I suggest checking the type for date if you want to keep the substr(0,10) or else this would be problematic for datetime types.

@HugoMario HugoMario self-assigned this May 8, 2020
@HugoMario
Copy link
Contributor

hey guys, thanks for PR, please ping me once this is ready to be merged

@HugoMario HugoMario merged commit f871209 into swagger-api:master May 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants