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] Date-Array query Parameter not serialized correctly #7801

Open
meisterlampe opened this issue Mar 8, 2018 · 1 comment
Open

Comments

@meisterlampe
Copy link

meisterlampe commented Mar 8, 2018

Description

Hi,

I have the following problem when using a Date-Array-Parameter in my swagger file:

The generated client is not serializing the dates correctly and the server is returning Bad request: Validation errors.
For example:
The client generates: createdAfter= Thu%20Mar%2008%202018%2015:56:22%20GMT%2B0100%20(CET), Thu%20Mar%2008%202018%2015:56:22%20GMT%2B0100%20(CET),
instead of;
createdAfter=2018-03-08T15:56:22Z,2018-03-08T15:56:22Z

Everything is working perfectly fine, if I only have a single date-query-parameter.

Swagger-codegen version

2.3.1

Swagger declaration file content or url

This is the relevant part of my swagger file:

parameters:
        - name: createdAfter
          in: query
          required: false
          type: array
          items:
            type: string
            format: date-time
Command line used for generation

java -jar libs/swagger-codegen-cli-2.3.1.jar generate -i src/api/swagger/swagger.yaml -l typescript-angular -o ~/swaggerGeneratedCode/typescript-angular/ --additional-properties=modelPropertyNaming=original

Suggest a fix/enhancement

I think, I already found the problem.
This is my generated code:
queryParameters = queryParameters.set('createdAfter', createdBefore.join(COLLECTION_FORMATS['csv']));

We have to call toISOString(). So it should be something like:
queryParameters = queryParameters.set('createdAfter', createdAfter.map((i) => i.toISOString()).join(COLLECTION_FORMATS['csv']));

This line in the template is generating the wrong code:

{{^isCollectionFormatMulti}}
{{#useHttpClient}}queryParameters = {{/useHttpClient}}queryParameters.set('{{baseName}}', {{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}']));
{{/isCollectionFormatMulti}}

A check is already there for single date-parameters a few lines below:

{{#isDateTime}}
{{#useHttpClient}}queryParameters = {{/useHttpClient}}queryParameters.set('{{baseName}}', <any>{{paramName}}.toISOString());
{{/isDateTime}}

So I guess, in case of date-array-params the template code should be something like:
{{#useHttpClient}}queryParameters = {{/useHttpClient}}queryParameters.set('{{baseName}}', {{paramName}}.map((i) => i.toISOString()).join(COLLECTION_FORMATS['{{collectionFormat}}']));

(just added .map((i) => i.toISOString()))

What is missing for a fix, is a test for the child-elements of the listContainer if they are of type date. I have no idea how to implement that in mustache-templates.

Please help and thanks in advance.

@macjohnny
Copy link
Contributor

@meisterlampe thanks for reporting the issue. It seems valid.

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

No branches or pull requests

2 participants