Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Array parameters in URL (#3529) #3980

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Pankraty
Copy link

@Pankraty Pankraty commented Apr 16, 2022

Hi!

We faced with the issue described in #3529 when the TypeScript client ignores the style attribute of a parameter passed in URL. For example, we have a schema defining input argument like this:

  "parameters": [
  {
    "name": "eventTypes",
    "in": "query",
    "style": "form",
    "explode": false,
    "schema": {
        "type": "array",
        "items": {
        "$ref": "#/components/schemas/EventTypes"
        },
        "nullable": true
    }
  },
  ...

and Swagger UI does build the URL as xxx?eventTypes=EventA,EventB which is exactly what we are expecting. But NSwag - both TypeScript and C# version - serializes parameters differently: xxx?eventTypes=EventA&eventTypes=EventB and so on.

I would like to contribute to the library and make necessary changes, and here is the first draft. It is not a completed work, for example, there should be additional checks for IsDateOrDateTimeArray. Additionally, I am going to make similar changes to C# generators and write tests. But before I spent too much time on this I wanted to get a confirmation from you that this is the right way to go.

With these changes, I was able to produce such code for my TypeScript client:

  1. When style=matrix
    republishEvents(eventTypes: EventTypes[] | null | undefined): Promise<number> {
        let url_ = this.baseUrl + "/api/v1/data/republishevents?";
        if (eventTypes !== undefined && eventTypes !== null)
        {
            eventTypes.forEach(item => { url_ += "eventTypes=" + encodeURIComponent("" + item) + ";"; });
            url_ = url_.replace(/;$/, "");
        }
        url_ = url_.replace(/[?&]$/, "");

  1. When style=form
    republishEvents(eventTypes: EventTypes[] | null | undefined): Promise<number> {
        let url_ = this.baseUrl + "/api/v1/data/republishevents?";
        if (eventTypes !== undefined && eventTypes !== null)
            if (eventTypes.length)
            {
                url_ += "eventTypes=";
                eventTypes.forEach(item => { url_ += encodeURIComponent("" + item) + ","; });
                url_ = url_.replace(/,$/, "");
            }
        url_ = url_.replace(/[?&]$/, "");

Maybe not the best JS code I ever saw, but it seems to do what we need.

Looking forward to hearing back from you, @RicoSuter!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant