acacode / swagger-typescript-api

Generate the API Client for Fetch or Axios from an OpenAPI Specification
MIT License
3.36k stars 361 forks source link

Strange method name #152

Open RoXuS opened 3 years ago

RoXuS commented 3 years ago

Several of our methods has a number after there name:

  searchConsumption1 = (data: { filters?: CreditOperationFilters; pageable?: Pageable }, params?: RequestParams) =>

Ok we have this method in another class but not in the same class. We generate API with modular.

js2me commented 3 years ago

Hello @RoXuS
Can you share part of schema where contains original searchConsumption and "duplicate" searchConsumption ?
And which version of swagger schema are you using ?
Just currently I'm not see a problem with local version.

js2me commented 3 years ago

@RoXuS did you see this message on console output ? Module "{moduleName}" already have method "{methodName}()" ?

RoXuS commented 3 years ago

No we don't have this log. Our schema:


      "post": {
        "tags": [
          "credit-operation-api"
        ],
        "operationId": "searchConsumption",
        "parameters": [
          {
            "name": "organizationId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer",
              "format": "int64"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "filters": {
                    "$ref": "#/components/schemas/CreditOperationFilters"
                  },
                  "pageable": {
                    "$ref": "#/components/schemas/Pageable"
                  }
                }
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/hal+json": {
                "schema": {
                  "$ref": "#/components/schemas/CreditPageCreditConsumptionDto"
                }
              }
            }
          },
          "400": {
            "description": "Bad Request"
          }
        }
      }
    },
js2me commented 3 years ago

@RoXuS I hope that this problem was fixed in 5.0.0 :)
If not please reopen issue or ping me here

RoXuS commented 3 years ago

hey,

with 5.0.0, the error remains, for example

  /**
   * No description
   *
   * @tags credit-operation-api
   * @name SearchConsumption1
   * @request POST:/me/organization/credits/search-consumption
   * @secure
   */
  searchConsumption1 = (
    data: CreditOperationFilters,
    query?: { page?: number; size?: number; sort?: string[] },
    params: RequestParams = {},
  ) => ...

For info our two different schema:

  "/organizations/{organizationId}/credits/search-consumption": {
      "post": {
        "tags": [
          "credit-operation-api"
        ],
        "operationId": "searchConsumption",
        "parameters": [
          {
            "name": "organizationId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer",
              "format": "int64"
            }
          },
          {
            "name": "page",
            "in": "query",
            "description": "Zero-based page index (0..N)",
            "schema": {
              "type": "integer",
              "default": 0
            }
          },
          {
            "name": "size",
            "in": "query",
            "description": "The size of the page to be returned",
            "schema": {
              "type": "integer",
              "default": 20
            }
          },
          {
            "name": "sort",
            "in": "query",
            "description": "Sorting criteria in the format: property(,asc|desc). Default sort order is ascending. Multiple sort criteria are supported.",
            "schema": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreditOperationFilters"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/hal+json": {
                "schema": {
                  "$ref": "#/components/schemas/CreditPageCreditConsumptionDto"
                }
              }
            }
          },
          "400": {
            "description": "Bad Request"
          }
        }
      }
    }
```
"/me/organization/credits/search-consumption": {
  "post": {
    "tags": [
      "credit-operation-api"
    ],
    "operationId": "searchConsumption_1",
    "parameters": [
      {
        "name": "page",
        "in": "query",
        "description": "Zero-based page index (0..N)",
        "schema": {
          "type": "integer",
          "default": 0
        }
      },
      {
        "name": "size",
        "in": "query",
        "description": "The size of the page to be returned",
        "schema": {
          "type": "integer",
          "default": 20
        }
      },
      {
        "name": "sort",
        "in": "query",
        "description": "Sorting criteria in the format: property(,asc|desc). Default sort order is ascending. Multiple sort criteria are supported.",
        "schema": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    ],
    "requestBody": {
      "content": {
        "application/json": {
          "schema": {
            "$ref": "#/components/schemas/CreditOperationFilters"
          }
        }
      },
      "required": true
    },
    "responses": {
      "200": {
        "description": "OK",
        "content": {
          "application/hal+json": {
            "schema": {
              "$ref": "#/components/schemas/CreditPageCreditConsumptionDto"
            }
          }
        }
      },
      "400": {
        "description": "Bad Request"
      }
    }
  }
},
```

Thx for your 5.0.0!
RoXuS commented 3 years ago

other info image

seems to keep increment through the files.

js2me commented 3 years ago

Hi @RoXuS !
Can you message all operations ids ("operationId" field) of this execureSearch operations ?

js2me commented 3 years ago

Just I think ending number it is a part of "operationId" field
like searchConsumption_1

RoXuS commented 3 years ago

Yeah we have

"/me/organization/credits/search-consumption": {
      "post": {
        "tags": [
          "credit-operation-api"
        ],
        "operationId": "searchConsumption_1",
        "parameters": [
          {
          ...

For example.

RoXuS commented 3 years ago

So the issue is on our swagger generator ?

js2me commented 3 years ago

@RoXuS If you don't see this message
image It means that the number endings goes from "operationId" field

I'll try to fix that at next release.
Definetely swagger-typescript-api codegenerator should crop _1, _2, _{n} in "operationId"

Currently, as quick solution you can customize route name with hook onCreateRouteName() (version 5.1.0)

RoXuS commented 3 years ago

We will wait your new version, np.

Thx!!

amankkg commented 3 years ago

Here is my temporary solution as @js2me suggested

onCreateRouteName: (nameInfo, rawInfo) => {
  nameInfo.original = nameInfo.original.replace(/\d+$/, '')
  nameInfo.usage = nameInfo.usage.replace(/\d+$/, '')
},
Grawl commented 2 months ago

Just faced this issue with following config:

import { generateApi } from 'swagger-typescript-api';
import { resolve } from 'node:path';

void generateApi({
  url: 'https://my-domain.tld/api/v3/api-docs',
  output: resolve(process.cwd(), 'src/shared/api'),
  name: 'generated',
  generateClient: true,
  httpClientType: 'axios',
});

solution from @amankkg is working for me