[BUG][typescript-axios] Multi-value query params are incorrectly encoded as a comma separated string
Created by: sambernard
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
Have you searched for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
The generated code improperly converts query list params to comma-separated strings.
If passing an array to a query param - eg include=["relation_1", "relation_2"]
I expect it would output include=relation_1&include=relation_2
in the query string. Instead, it returns include=relation_1,relation_2
.
openapi-generator version
5.0.0-beta2
OpenAPI declaration file content or url
If you post the code inline, please wrap it with
info:
title: API
version: 1.0.0
openapi: 3.0.0
paths:
/some-resource:
get:
responses:
'200':
description: Ok
content:
application/json:
schema:
properties:
id:
type: string
type: object
parameters:
- in: query
name: include
required: false
schema:
default: []
type: array
items:
enum:
- relation_1
- relation_2
type: string
Generation Details
Use [typescript-axios] with default options.
generatorName: typescript-axios
Steps to reproduce
- Run generator
- Call
await DefaultApiAxiosParamCreator().someResourceGet(["relation_1", "relation_2"])
. - Observe that called url has
include=relation_1,relation_2
in the querystring.
Related issues/PRs
Suggest a fix
The problematic code is here:
const localVarQueryParameter = {} as any;
if (include) {
localVarQueryParameter['include'] = include;
}
const query = new URLSearchParams(localVarUrlObj.search);
for (const key in localVarQueryParameter) {
query.set(key, localVarQueryParameter[key]);
}
This line query.set(key, localVarQueryParameter[key]);
converts the second argument to a string, so the array gets converted to relation_1,relation_2
.
The proper approach would be to check if the value of the given localVarQueryParameter
is an array, and if so, to loop over it and append, eg:
for (const key in localVarQueryParameter) {
if(Array.isArray(localVarQueryParameter[key])) {
localVarQueryParameter[key].forEach(v => {
query.append(key, v)
})
} else {
query.set(key, localVarQueryParameter[key]);
}
}