Shaddix / react-query-swagger

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

Invalid ts code generation for JSON-serialized parameters #24

Closed Rudomitori closed 1 year ago

Rudomitori commented 1 year ago

If you try to generate client with the following command arguments and swagger.json:

react-query-swagger openapi2tsclient /tanstack /input:swagger.json /output:api-client.ts /template:Axios /serviceHost:. /minimal
{
  "openapi": "3.0.0",
  "info": {
    "title": "Bug reproduction",
    "description": "Bug reproduction",
    "version": "1"
  },
  "paths": {
    "/api/v1/Answers": {
      "get": {
        "tags": [
          "Answers"
        ],
        "operationId": "Answers_GetAnswers",
        "parameters": [
          {
            "name": "Tags",
            "in": "query",
            "x-position": 7,
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "additionalProperties": {
                    "type": "string"
                  }
                }
              }
            }
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "content": {
              "application/json": {
                "schema": {}
              }
            }
          }
        }
      }
    }
  }
}

You get the following invalid TypeScript code in api-client/AnswersQuery.ts and api-client/AnswersClient.ts:

let url_ = getBaseUrl() + "/api/v1/Answers?";
if (tags === null)
    throw new Error("The parameter 'tags' cannot be null.");
else if (tags !== undefined)
{
    const content_ = Types.serialize{ [key: string]: string; }(tags);
    url_ += "Tags=" + encodeURIComponent(content_) + "&";
}

I think in this case it should be:

const content_ = JSON.stringify(tags);

react-query-swagger version: 15.7.6

Shaddix commented 1 year ago

Thanks for the report! Fixed in 15.7.9

Rudomitori commented 1 year ago

I've tested it with the latest version and all works fine. Thanks for the quick fix!