Generated Javascript APIs ignore query parameter of type "array" and collectionFormat "csv" and use multi instead
Description
For a query parameter of type "array" and collectionFormat "csv", the generated Javascript API treats the parameter as "multi" instead.
Swagger-codegen version
2.2.0
Swagger declaration file content or url
The following Swagger spec demonstrates this behaviour:
{
"swagger": "2.0",
"info": {
"title": "test spec",
"version": "1.0"
},
"parameters": {
"testParam": {
"collectionFormat": "csv",
"in": "query",
"items": {
"type": "string"
},
"name": "select",
"type": "array
}
},
"paths": {
"/test": {
"get": {
"description": "test get",
"parameters": [
{
"$ref": "#/parameters/testParam"
}
],
"references": {
}
}
}
}
}
In the generated TestApi.js,
this.getTest = function(opts) {
...
var queryParams = {
'select': this.apiClient.buildCollectionParam(opts['select'], 'multi')
};
...
}
Command line used for generation
java -jar swagger-codegen-cli.jar generate -i swagger.json -l javascript -o jsclient -c config.json
where config.json is:
{
"usePromises": true
}
Steps to reproduce
- Generate and build the Java API for the above specification.
- Create a new DefaultApi instance.
- Call the testGet function with several strings an array passed to the test parameter.
testClient.getTest(
{
select: ['string1', 'string2']
})
.then(res => {
console.log(res);
})
.catch(error => {
console.log('fail', error);
});
The API will send an HTTP request with a URI looking like the following:
/test?select=string1&select=string2
instead of
/test?select=string1,string2
Related issues
After manually change TestApi.js to use 'csv', I also see comma that separating the array element being encoded in the url as stated in https://github.com/swagger-api/swagger-codegen/issues/3638.
Suggest a Fix
@mzhang444 the latest master seems to have better support for collection format in JS API client:
grep -R -i collectionformat modules/swagger-codegen/src/main/resources/Javascript/*
modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache: exports.CollectionFormatEnum = {
modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache: * @param {module:{{#invokerPackage}}{{invokerPackage}}/{{/invokerPackage}}ApiClient.CollectionFormatEnum} collectionFormat The array element separator strategy.
modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache: * <code>param</code> as is if <code>collectionFormat</code> is <code>multi</code>.
modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache:{{/emitJSDoc}} exports.prototype.buildCollectionParam = function buildCollectionParam(param, collectionFormat) {
modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache: switch (collectionFormat) {
modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache: throw new Error('Unknown collection format: ' + collectionFormat);
modules/swagger-codegen/src/main/resources/Javascript/api.mustache: '<baseName>': <#collectionFormat>this.apiClient.buildCollectionParam(<#required><paramName></required><^required>opts['<paramName>']</required>, '<collectionFormat>')</collectionFormat><^collectionFormat><#required><paramName></required><^required>opts['<paramName>']</required></collectionFormat><#hasMore>,</hasMore></queryParams>
modules/swagger-codegen/src/main/resources/Javascript/api.mustache: '<baseName>': <#collectionFormat>this.apiClient.buildCollectionParam(<#required><paramName></required><^required>opts['<paramName>']</required>, '<collectionFormat>')</collectionFormat><^collectionFormat><#required><paramName></required><^required>opts['<paramName>']</required></collectionFormat><#hasMore>,</hasMore></formParams>
Can you pull the latest master to give it a try?
I tested with the latest code from master, collectionFormat 'csv' in swagger.json is properly recognized. The generated code for the query parameter looks like this: var queryParams = { 'select': this.apiClient.buildCollectionParam(opts['select'], 'csv') };
However, when the API sends the HTTP request, the URI looks like this:
/test?select=string1%2Cstring2
instead of
/test?select=string1,string2
Should the comma in csv format not to be encoded in the URI according to swagger spec?
I've submitted a PR fixing this issue: #4696
Per the discussion in #4696, created a new PR: #4725
Thanks for the PR by @michaelkourlas
@mzhang444 please pull the latest 2.3.0 to give it a try.