swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.99k stars 6.03k forks source link

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

Open vsvetoslavov opened 6 years ago

vsvetoslavov commented 6 years ago
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.

obildtsen commented 5 years ago

Seeing the same behavior with version 3.0.4:

    compile 'io.swagger.codegen.v3:swagger-codegen:3.0.4'
    compile 'io.swagger.codegen.v3:swagger-codegen-generators:1.0.4'
obildtsen commented 5 years ago

Any ideas on workarounds...?

obildtsen commented 5 years ago

This issue also affects generated PHP code.

skrysmanski commented 5 years ago

I've fixed this through swagger-api/swagger-codegen-generators#309. Thus this should be fixed in generators:1.0.5/codegen:3.0.6.