swagger-api / swagger-api/swagger-codegen

[JAVA] Collection attribute serialized as 'csv' in generated client - style: form, explode: true

Open
#8,729 4 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

I have a REST service where one of my methods has a List query parameter, which is described with style = "form"(also default value).
Checking the OpenAPI spec here I see that by default query parameters have form style and for that the default for explode is true.
Also spec states that:

Form style parameters defined by RFC6570. This option replaces collectionFormat with a csv (when explode is false) or multi (when explode is true) value from OpenAPI 2.0.

After generating a Java client, I noticed the following inside the generated DefaultApi.java file:

        if (keywords != null)
        localVarCollectionQueryParams.addAll(apiClient.parameterToPairs("csv", "keywords", keywords));

My understanding is that in this situation the conversion type should be "multi", not "csv".

Another interesting thing is that when converting parameters to CSV, the commas are not escaped, so
an array of "one", "two", "I, Caesar" is converted to "one,two,I Caesar" which is wrong.

Swagger-codegen version

swagger-codegen-cli 3.0.0-SNAPSHOT

Swagger declaration file content or url
  /dv/any:
    get:
      description: Search for records that match one or more of the provided keyword(s). Reason and operator must be provided.
      operationId: searchAny
      parameters:
        - name: keywords
          in: query
          required: true
          style: form
          schema:
            type: array
            items:
              type: string
Command line used for generation

java -jar swagger-codegen-cli.jar generate -i /home/vsvetoslavov/tmp/datavault-openapi.yaml -o /home/vsvetoslavov/tmp/dv-local-3/ -l java

Steps to reproduce
  1. Have a REST service with a GET method that has an array[Stirng] parameter in QUERY part. (My service is on WildFly, using JAX-RS annotations and Swagger annotations).
  2. Generate a client for that service
  3. Use the client in another app to invoke the service
  4. The array values are received by the REST service as a single CSV string and commas in values have not been escaped.
Related issues/PRs
Suggest a fix/enhancement

Use "multi" for arrays in query, that have form style and explode is true.

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 generated Java client from the supplied OpenAPI YAML and inspect DefaultApi.java, especially the parameterToPairs call for keywords. Trace where the collection query serialization choice is produced, then verify that form-style, exploded arrays are serialized as separate query values and that commas in values remain intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
api, backend-api-design
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.