RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.67k stars 1.23k forks source link

OpenApi C# Generator - How to control query string format for array parameters in generated client code #4736

Open xantari opened 7 months ago

xantari commented 7 months ago

When generating the C# client using NSwagStudio or the MSBuild nswag generator it would be beneficial if we can control how query string array parameters are generated.

Currently array parameters are generated for an array query string as follows:

http://someapi/Users?user_status=active&user_status=pending

However, in PHP the default for these kinds of arrays coming into the API is as follows:

http://someapi/Users?user_status[]=active&user_status[]=pending

Here is an example api YML:

/users:
  get:
    tags:
      - User
    summary: List site users
    description: Multiple search values can be provided to narrow down the results
    operationId: listUsers
    parameters:
      - name: user_status
        in: query
        description: Status values that need to be considered for filter
        required: false
        schema:
          title: listUsersParamUserArrayStatus
          type: array
          items:
            type: string
            title: listUsersParamUserStatus
            enum:
              - active
              - pending
              - imported
              - inactive
            default: active

This seems related to https://github.com/RicoSuter/NSwag/issues/1451 and https://github.com/RicoSuter/NSwag/discussions/4377 and https://github.com/RicoSuter/NSwag/issues/3620 and https://github.com/RicoSuter/NSwag/issues/1703 and https://github.com/RicoSuter/NSwag/issues/1842 (array enum issues)

PHP examples to consider from here: https://stackoverflow.com/questions/6243051/how-to-pass-an-array-within-a-query-string

?cars[]=Saab&cars[]=Audi (Best way- PHP reads this into an array) ?cars=Saab&cars=Audi (Bad way- PHP will only register last value) ?cars=Saab,Audi (Haven't tried this)

How can we control the array parameter generation in nswag studio, and the NSwag.MSBuild package?

natanaelfiorilla commented 2 months ago

Any feedback on this? For .Net, only works like http://someapi/Users?user_status[0]=active&user_status[1]=pending

Balder1840 commented 1 month ago

Any updates?