Skip to content

[Scala] Collection parameters not serializing properly #7174

Open
@bchess

Description

@bchess
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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions