swagger-api / swagger-api/swagger-codegen
[Typescript] Date-Array query Parameter not serialized correctly
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
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:
https://github.com/swagger-api/swagger-codegen/blob/f00a1ef52c7d478436711b1796eb1cc1aa1de09d/modules/swagger-codegen/src/main/resources/typescript-angular/api.service.mustache#L146-L148
A check is already there for single date-parameters a few lines below:
https://github.com/swagger-api/swagger-codegen/blob/f00a1ef52c7d478436711b1796eb1cc1aa1de09d/modules/swagger-codegen/src/main/resources/typescript-angular/api.service.mustache#L153-L155
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/swagger-codegen/src/main/resources/typescript-angular/api.service.mustache, comparing array serialization around lines 146-148 with the single-date handling around lines 153-155. Locate the typescript-angular generator tests and add coverage for date child elements in listContainer; done means generated query parameters use ISO date strings and the test passes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- angular, openapi, typescript
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100