swagger-api / swagger-api/swagger-codegen

[typscript][jquery] optional array query parameter is not generated correctly

Open
#10,280 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Defining an optional query parameter (configurationRevision in the shown yml) as array, the serialization seems to be wrong, and result in error:
"Cannot read property 'push' of undefined"

Serialized code (api/MeasurementsApi.ts):

if (configurationRevision) {
            configurationRevision.forEach((element: any) => {
                queryParameters['configurationRevision[]'].push(element);
            });
        }
Swagger-codegen version

I am using https://editor.swagger.io/ - Generate Client - typescript-jquery

Swagger declaration file content or url
swagger: '2.0'
info:
  description: Interface of REST Service for the Webclient
  version: 2.0.0
  title: REST Interface for Webclient
basePath: /api
tags:
  - name: configurations
paths:
  /measurements:
    get:
      tags:
        - measurements
      description: Returns all measurements
      parameters:
        - name: configurationId
          in: query
          description: Configuration ID
          required: false
          type: integer
          format: int32
        - name: configurationRevision[]
          in: query
          description: Configuration Revision
          required: false
          type: array
          items:
            type: integer
            format: int32
          collectionFormat: multi
      operationId: measurements
      produces:
        - application/json
      responses:
        '200':
          description: All available measurements
          schema:
            type: array
            items:
              type: integer
        '901':
          description: No valid licence
Command line used for generation
Steps to reproduce

Use above yml and generate typescript-jquery client code with: https://editor.swagger.io/

Related issues/PRs
Suggest a fix/enhancement

Create an emtpy array, to make push available.

if (configurationRevision) {
  queryParameters['configurationRevision[]'] = new Array<number>();
  configurationRevision.forEach((element: any) => {
    queryParameters['configurationRevision[]'].push(element);
  });
 }

[edit] insert collectionFormat: multi, change configurationRevision to configurationRevision[]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue from the supplied Swagger YAML using the typescript-jquery generator and inspect the template responsible for api/MeasurementsApi.ts. Confirm the generated client safely handles the optional array query parameter, then regenerate and verify that the configurationRevision request no longer fails when values are supplied.

Written by the indexing model from the issue text.

Assessment

Tech stack
jquery, typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.