NSwag icon indicating copy to clipboard operation
NSwag copied to clipboard

Add support for collectionFormat "csv"

Open kimbell opened this issue 7 years ago • 5 comments

I'm consuming a swagger spec that has collectionFormat csv defined for a query string parameter. I've extracted the minimum spec to reproduce.

{
    "swagger": "2.0",
    "info": {
      "version": "1.9.0",
      "title": "Dummy",
      "description": ""
    },
    "host": "locahost",
    "basePath": "/V1",
    "schemes": [
      "https"
    ],
    "parameters": {
      "travelerIds": {
        "name": "travelerIds",
        "description": "List of traveler identifiers",
        "required": true,
        "in": "query",
        "type": "array",
        "items": {
          "type": "string",
          "pattern": "[a-zA-Z0-9-]{1,20}"
        },
        "collectionFormat": "csv"
      }
    },
    "paths": {
      "/test": {
        "delete": {
          "parameters": [
            {
              "$ref": "#/parameters/travelerIds"
            }
          ],
          "responses": {
            "204": {
              "description": "No content"
            }
          }
        }
      }
    },
    "definitions": {
    }
  }

NSWag produces the following code

foreach (var item_ in travelerIds) { urlBuilder_.Append("travelerIds=").Append(System.Uri.EscapeDataString(ConvertToString(item_, System.Globalization.CultureInfo.InvariantCulture))).Append("&"); }

It adds the travelerIds multiple times instead of combining them into a single comma separated value.

Based on the information https://swagger.io/docs/specification/2-0/describing-parameters/ csv should be the default. The current code seems to match collectionformat multi.

kimbell avatar Nov 01 '18 07:11 kimbell

This feature is not yet implemented.

Not all OpenAPI/Swagger features are yet implemented - the focus is mainly on features which are actually generated by the NSwag spec generator...

RicoSuter avatar Nov 01 '18 07:11 RicoSuter

Not sure if this is planned, but I think it's a valuable feature. The ampersand syntax is very lossy. Open API v3 spec also includes more options dealing with arrays and complex objects.

See also: https://github.com/aspnet/AspNetCore/issues/6906

MikaelUmaN avatar Jan 23 '19 08:01 MikaelUmaN

I think for now it's best to implement a custom operation process which changes the parameter to csv - i'm not sure if we can find out the actual serialization (e.g. csv, comma, etc.) via asp.net core api explorer?

RicoSuter avatar Jan 23 '19 09:01 RicoSuter

For the typescript client generation, the template can be overridden here:

https://github.com/RicoSuter/NSwag/blob/785a95ae34c9c679e07a0f99cf99c2fe763cc33a/src/NSwag.CodeGeneration.TypeScript/Templates/Client.RequestUrl.liquid#L55

with the following:

url_ += "{{ parameter.Name }}=" + encodeURIComponent({{ parameter.VariableName }}.join(",")) + "&";

pwfcurry avatar Jun 26 '19 10:06 pwfcurry

Please i need this implementation, i think is not so difficult to implement it. Thanks

ilguala avatar Mar 17 '25 10:03 ilguala