swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

Generated Javascript APIs ignore query parameter of type "array" and collectionFormat "csv" and use multi instead

Open mzhang444 opened this issue 9 years ago • 5 comments

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
  1. Generate and build the Java API for the above specification.
  2. Create a new DefaultApi instance.
  3. 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 avatar Jan 25 '17 18:01 mzhang444

@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?

wing328 avatar Jan 30 '17 10:01 wing328

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?

mzhang444 avatar Jan 31 '17 18:01 mzhang444

I've submitted a PR fixing this issue: #4696

michaelkourlas avatar Feb 02 '17 19:02 michaelkourlas

Per the discussion in #4696, created a new PR: #4725

michaelkourlas avatar Feb 06 '17 14:02 michaelkourlas

Thanks for the PR by @michaelkourlas

@mzhang444 please pull the latest 2.3.0 to give it a try.

wing328 avatar Feb 06 '17 15:02 wing328