Shaddix / react-query-swagger

Generates react-query hooks based on Swagger API definitions
MIT License
122 stars 4 forks source link

explode true support #27

Closed erangeles closed 1 year ago

erangeles commented 1 year ago

Hello, I have an API endpoint that has this pattern {url}?env=staging&env=production — i can’t get any of the generated types / clients to support that

   const { data, error } = useGetResources({
    queryParams: {
      env: ['production', 'staging'], <- does not work
    },
  });

   const { data, error } = useGetResources({
    queryParams: {
      env: 'production', 
      env: 'staging', <- does not work
    },
  });

output: {url}/endpoint?env=production%2Cstaging

expected output: {url}/endpoint?env=production&env=staging

erangeles commented 1 year ago

Update: I got this working below

  const queryParams = new URLSearchParams();
  queryParams.append('env', 'production');
  queryParams.append('env', 'staging');
  const { data, error } = useGetResources({
    queryParams,
  });
Shaddix commented 1 year ago

@erangeles could you please share your swagger.json file btw? I tried reproducing it, and got the correct typescript code:

    if (data !== undefined && data !== null)
        data && data.forEach(item => { url_ += "data=" + encodeURIComponent("" + item) + "&"; });
      url_ = url_.replace(/[?&]$/, "");

I used the following swagger (generated automatically by NSwag):

"/query/ArrayInQuery": {
      "get": {
        "tags": ["Query"],
        "operationId": "Query_ArrayInQuery",
        "parameters": [
          {
            "name": "data",
            "in": "query",
            "style": "form",
            "explode": true,
            "schema": {
              "type": "array",
              "nullable": true,
              "items": {
                "type": "string"
              }
            },
            "x-position": 1
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "type": "string"
                  }
                }
              }
            }
          }
        }
      }
    }
erangeles commented 1 year ago

Thanks for the response, here is the swagger json file