Skip to content

[Scalatra] collectionFormat=csv not handled correctly #3806

Closed

Description

@jtnystrom
Description

When collectionFormat is explicitly set, incorrect code is generated by the Scalatra generator.
For example, with this specification:

      parameters:
      - name: assayIds
        description: "Assay IDs to retrieve metadata for"
        in: query
        type: array
        collectionFormat: csv

the following code is generated:

      val assayIdsString = params.getAs[String]("assayIds")
      val assayIds = if("csv".equals("default")) {
        assayIdsString match {
          case Some(str) => str.split(",")
          case None => List()
        }
      }
      else
        List()

The main issue here is that the comparison "csv" == "default" will clearly never be true, so the string will never be split. We will always get an empty list back.
According to the swagger specification, supported values for collectionFormat are 'csv', 'tsv', 'ssv', 'pipes' and 'multi'.

This is in the latest github snapshot.

A secondary issue is that the type of assayIds is inferred to be simply Serializable in this case, which is not very helpful (the value becomes unusable). This is because Array[String] and List[Any] do not unify to a common supertype.
This can be solved, for example, by generating either this code (which I tested):

     assayIdsString match {
          case Some(str) => str.split(",").toSeq
          case None => Seq()
        }

Or this:

     assayIdsString match {
          case Some(str) => str.split(",")
          case None => Array[String]()
        }

Activity

  1. wing328 commented on Sep 16, 2016

    @wing328
    Contributor

    @jtnystrom thanks for reporting the issue. Would you please submit a PR so that we can review the change?

  2. added this to the v2.2.2 milestone on Sep 16, 2016
  3. jtnystrom commented on Sep 16, 2016

    @jtnystrom
    ContributorAuthor

    I could, but I'm not confident that I know how the main issue should be addressed.
    Probably, this codegen only supports the csv format so far (?) and I could fix that, but if so, the other formats would remain unsupported.

  4. wing328 commented on Sep 19, 2016

    @wing328
    Contributor

    If there're demand to support other formats, we can ask the contributors to file a fix with reference to the enhancement you're going to make.

  5. jtnystrom commented on Sep 19, 2016

    @jtnystrom
    ContributorAuthor

    OK - I will try to submit something this week. Thanks.

  6. bull-xu commented on Oct 16, 2016

    @bull-xu

    This is also issue for generating Swift code. AlamofireRequestBuilder passes in Alamofire.ParameterEncoding.URL encoding type, and Alamofire ParameterEncoding is using queryComponents("\(key)[]", value) which not a single value of comma separated values foo,bar. described in http://swagger.io/specification/

  7. wing328 commented on Oct 17, 2016

    @wing328
    Contributor

    @bull-xu-stride for the issue with the swift API client, please kindly open a separate issue for tracking.

  8. jtnystrom commented on Dec 19, 2016

    @jtnystrom
    ContributorAuthor

    I've just submitted a PR for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions