swagger-api / swagger-api/swagger-codegen

[Scala] Collection parameters not serializing properly

Open
#7,174 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Scala help wanted Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Collection parameters get serialized via toString(), resulting in URL parameters like List%28123%29 for List(123).

Swagger-codegen version

2.3.0

Swagger declaration file content or url
{
  "swagger": "2.0",
  "info": {
    "version": "1.0.0",
    "title": "Collection parameter issue",
    "description": "An endpoint that takes a collection as a parameter",
    "termsOfService": "http://swagger.io/terms/"
  },
  "host": "example.com",
  "basePath": "/api",
  "schemes": [
    "http"
  ],
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {
    "/hi": {
      "get": {
        "operationId": "hi",
        "parameters": [
          {
            "collectionFormat": "csv",
            "description": "Comma separated list of integers",
            "in": "query",
            "items": {
              "type": "integer"
            },
            "name": "numbers",
            "required": true,
            "type": "array"
          }
        ],
        "responses": {
          "200": {
            "description": "Success"
          }
        },
        "tags": [
          "hi"
        ]
      }
    }
  }
}

Command line used for generation

java -jar swagger-codegen.jar generate --lang scala -i collection-issue.json

Steps to reproduce
val api = new HiApi(defBasePath="http://localhost:8080")
val result = api.hi(List(1, 2, 3))

Start a simple webserver on 8080 python -m SimpleHTTPServer 8080

Observe:


127.0.0.1 - - [13/Dec/2017 15:29:19] "GET /hi?numbers=List%281%2C%202%2C%203%29 HTTP/1.1" 404 -

Related issues/PRs

#6540 implemented support for collections, but I don't think it ever worked properly, at least for parameters. Notably, adding an escape() function for List[String] is useless when invokeApi's queryParams is Map[String, String].

Suggest a fix/enhancement

Serialization should be addressed inside api.mustache, replacing/augmenting param.toString call with a call to escape() instead. Also see java's parameterToString() as another valid approach.

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

Start in the Scala api.mustache template and inspect how query parameters are converted before invokeApi receives its queryParams map. Reproduce the issue with the supplied collection-issue.json, generation command, and Scala call, then compare the approach with Java's parameterToString(). Done means a collection such as List(1, 2, 3) produces the intended serialized query rather than List%281%2C%202%2C%203%29.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.