Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #7966
Closed
Open
Issue created Nov 17, 2020 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[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
  1. Run generator
  2. Call await DefaultApiAxiosParamCreator().someResourceGet(["relation_1", "relation_2"]).
  3. Observe that called url has include=relation_1,relation_2 in the querystring.
Related issues/PRs
Suggest a fix

The problematic code is here:

https://github.com/OpenAPITools/openapi-generator/blob/v5.0.0-beta2/modules/openapi-generator/src/main/resources/typescript-axios/apiInner.mustache#L191-L194

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]);
    }
}
Assignee
Assign to
Time tracking