Skip to content

[Typescript] Date-Array query Parameter not serialized correctly #7801

@meisterlampe

Description

@meisterlampe
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions