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 string date format #3423

Merged

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, ./bin/openapi3/{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\. If contributing template-only or documentation-only changes which will change sample output, be sure to build the project first.
  • Filed the PR against the correct branch: master, 4.1.x, 5.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.

Description of the PR

@TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny

Fair warning that I tested this out more thoroughly using swagger-codegen for this PR - swagger-api/swagger-codegen#9594 - but it looks like the same exact issue.

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.

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.

fixes #2745

Copy link
Member

@macjohnny macjohnny left a comment

Choose a reason for hiding this comment

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

LGTM

@macjohnny
Copy link
Member

@tsiq-jeremy thanks for your PR! can you please re-generate the samples?

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, ./bin/openapi3/{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. If contributing template-only or documentation-only changes which will change sample output, be sure to build the project first.

@macjohnny
Copy link
Member

cc @nicokoenig

@tsiq-jeremy
Copy link
Contributor Author

Hey I just ran ./bin/typescript-axios-petstore-all.sh and did not see any changes when running git status. I looked through the yaml file it runs against and it has date-time, but no date types.

@macjohnny
Copy link
Member

@tsiq-jeremy ok, that's probably because there is no Date-query parameter in the petstore example, so everything is fine.
The CI-failure is unrelated.

@macjohnny macjohnny merged commit 5775813 into OpenAPITools:master Jul 23, 2019
@macjohnny
Copy link
Member

@tsiq-jeremy would you like to fix the same problem in the typescript-fetch generator, i.e. here:

{{#isDateTime}}
queryParameters['{{baseName}}'] = (requestParameters.{{paramName}} as any).toISOString();
{{/isDateTime}}
{{^isDateTime}}
{{#isDate}}
queryParameters['{{baseName}}'] = (requestParameters.{{paramName}} as any).toISOString().substr(0,10);
{{/isDate}}

@macjohnny macjohnny changed the title 2745 typescript date format typescript-axios string date format Jul 23, 2019
@tsiq-jeremy
Copy link
Contributor Author

Hey, let me look into that one. I'm not sure if there is a problem with typescript-fetch b/c in modelGeneric.mustache it has FromJSON and ToJSON that are manipulating the date type.

@tsiq-jeremy tsiq-jeremy deleted the 2745-typescript-date-format branch July 23, 2019 13:52
@macjohnny
Copy link
Member

yes, but I think the From/ToJson functions are used for the body, not for the query parameters, so it should be the same situation

@tsiq-jeremy
Copy link
Contributor Author

Hey I have not tested this out fully, but I think this should not be a problem with typescript-fetch. Used to test:

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

Produced:

export interface GetOrderByIdRequest {
    date: Date;
}

   ....
    async getOrderByIdRaw(requestParameters: GetOrderByIdRequest): Promise<runtime.ApiResponse<Order>> {
       ....
        const queryParameters: runtime.HTTPQuery = {};

        if (requestParameters.date !== undefined) {
            queryParameters['date'] =  (requestParameters.date as any).toISOString().substr(0,10);
        }
       ...
    }

So it looks like that a Date object is getting passed in so that it should not produce an error when calling toISOString.

@macjohnny
Copy link
Member

Ok, alright, thanks for checking!

@wing328 wing328 added this to the 4.1.0 milestone Jul 24, 2019
@wing328
Copy link
Member

wing328 commented Aug 10, 2019

@tsiq-jeremy thanks for the PR, which has been included in the 4.1.0 release: https://twitter.com/oas_generator/status/1160000504455319553

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

Successfully merging this pull request may close these issues.

[typescript-axios] toIsoString() with type string
3 participants