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

Typescript client - generated method parameter not typed as array for array query params #3529

Open
B0SS opened this issue Jun 29, 2021 · 3 comments

Comments

@B0SS
Copy link

B0SS commented Jun 29, 2021

Hello,

I have an issue with my ts client generation.

When i try to generate a service method which uses array query params, the parameter is correctly processed as an array (a foreach is applied to the parameter) but the parameter is not typed as an array.

Here is the swagger json spec for the api endpoint, we can see that the query param "sort" is of type array:

    "/api/forms": {
      "get": {
        "tags": [
          "FormsController"
        ],
        "summary": "Return forms visible",
        "operationId": "getForms",
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "page",
            "in": "query",
            "required": false,
            "type": "integer",
            "minimum": 1,
            "exclusiveMinimum": false,
            "format": "int32"
          },
          {
            "name": "size",
            "in": "query",
            "required": false,
            "type": "integer",
            "minimum": 1,
            "exclusiveMinimum": false,
            "format": "int32"
          },
          {
            "name": "sort",
            "in": "query",
            "description": "Zero, one or multiple values",
            "required": false,
            "type": "array",
            "items": {
              "type": "string"
            },
            "collectionFormat": "multi",
            "default": "status ASC,code DESC",
            "enum": [
              "code ASC",
              "code DESC",
              "status ASC",
              "status DESC",
            ]
          },
        ],
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/FormDto"
              }
            }
          },
          "401": {
            "description": "Unauthorized"
          },
          "403": {
            "description": "Forbidden"
          },
          "404": {
            "description": "Not Found"
          }
        }
      }
    },

Here is the generated code, as you can see, foreach is applied to the parameter "sort" but it's not typed as array,:

   getForms(page: number | null | undefined, size: number | null | undefined, sort: Sort | null | undefined): Promise<FormDto[]> {
       let url_ = this.baseUrl + "/api/forms?";
       if (page !== undefined && page !== null)
           url_ += "page=" + encodeURIComponent("" + page) + "&";
       if (size !== undefined && size !== null)
           url_ += "size=" + encodeURIComponent("" + size) + "&";
       if (sort !== undefined && sort !== null)
           sort && sort.forEach(item => { url_ += "sort=" + encodeURIComponent("" + item) + "&"; });
       url_ = url_.replace(/[?&]$/, "");

       let options_ = <RequestInit>{
           method: "GET",
           headers: {
               "Accept": "application/json"
           }
       };

       return this.http.fetch(url_, options_).then((_response: Response) => {
           return this.processGetForms(_response);
       });
   }
@RogueJay
Copy link

RogueJay commented Sep 6, 2021

What are you trying to achieve for your sort URL?

I'm trying to do something similar, in order get URLs for the format
?filter[aField]=value&filter[otherField]=otherValue

I have set up the schema in the following format. Note, the top level object and deepObject properties are required so that Swagger.UI interprets these options as well. Using array at the top level doesn't work

        - name: filter
          in: query
          style: deepObject
          schema:
            type: object
            additionalProperties:
              type: string

or

        - name: filter
          in: query
          style: deepObject
          collectionFormat: multi
          schema:
            type: object
            items:
              type: string 

Setting collectionFormat makes the generator realise it's an array, and produces the foreach specified above. However, I think the code in Client.RequestUrl.liquid needs to actually do something like

{%     elseif parameter.IsArray -%}
    {{ parameter.VariableName }} && {{ parameter.VariableName }}.forEach((item, index) => {
     	url_ += "{{ parameter.Name }}[" + index + "]" + "=" + encodeURIComponent("" + item) + "&";
    });

Probably need something similar for
{% if parameter.IsDateOrDateTimeArray

@RogueJay
Copy link

RogueJay commented Sep 6, 2021

I realised that what I'm doing is perhaps a little different, so I raised a new ticket

@B0SS
Copy link
Author

B0SS commented Sep 27, 2021

@RicoSuter any news on this issue ?

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

No branches or pull requests

3 participants