chenshuai2144 / openapi2typescript

一个被大范围使用的小工具
312 stars 136 forks source link

生成 mock 出现堆栈溢出 #58

Open phpmaple opened 2 years ago

phpmaple commented 2 years ago

> wanda-sa-h5@0.0.0 openapi /Users/koofrank/Documents/projects/wanda-sa-h5
> vite-node openapi.config.ts

[openAPI]: ✅ 成功生成 service 文件
/Users/koofrank/Documents/projects/wanda-sa-h5/node_modules/.pnpm/@umijs+openapi@1.4.2/node_modules/@umijs/openapi/dist/openAPIParserMock/index.js:98
        this.sampleFromSchema = (schema, propsName) => {
                                ^

RangeError: Maximum call stack size exceeded
    at OpenAPIGeneratorMockJs.sampleFromSchema (/Users/koofrank/Documents/projects/wanda-sa-h5/node_modules/.pnpm/@umijs+openapi@1.4.2/node_modules/@umijs/openapi/dist/openAPIParserMock/index.js:98:33)
    at OpenAPIGeneratorMockJs.sampleFromSchema (/Users/koofrank/Documents/projects/wanda-sa-h5/node_modules/.pnpm/memoizee@0.4.15/node_modules/memoizee/lib/configure-map.js:61:24)
    at OpenAPIGeneratorMockJs.sampleFromSchema (/Users/koofrank/Documents/projects/wanda-sa-h5/node_modules/.pnpm/@umijs+openapi@1.4.2/node_modules/@umijs/openapi/dist/openAPIParserMock/index.js:135:35)
    at OpenAPIGeneratorMockJs.sampleFromSchema (/Users/koofrank/Documents/projects/wanda-sa-h5/node_modules/.pnpm/memoizee@0.4.15/node_modules/memoizee/lib/configure-map.js:61:24)
    at OpenAPIGeneratorMockJs.sampleFromSchema (/Users/koofrank/Documents/projects/wanda-sa-h5/node_modules/.pnpm/@umijs+openapi@1.4.2/node_modules/@umijs/openapi/dist/openAPIParserMock/index.js:119:38)
    at OpenAPIGeneratorMockJs.sampleFromSchema (/Users/koofrank/Documents/projects/wanda-sa-h5/node_modules/.pnpm/memoizee@0.4.15/node_modules/memoizee/lib/configure-map.js:61:24)
    at OpenAPIGeneratorMockJs.sampleFromSchema (/Users/koofrank/Documents/projects/wanda-sa-h5/node_modules/.pnpm/@umijs+openapi@1.4.2/node_modules/@umijs/openapi/dist/openAPIParserMock/index.js:135:35)
    at OpenAPIGeneratorMockJs.sampleFromSchema (/Users/koofrank/Documents/projects/wanda-sa-h5/node_modules/.pnpm/memoizee@0.4.15/node_modules/memoizee/lib/configure-map.js:61:24)
    at OpenAPIGeneratorMockJs.sampleFromSchema (/Users/koofrank/Documents/projects/wanda-sa-h5/node_modules/.pnpm/@umijs+openapi@1.4.2/node_modules/@umijs/openapi/dist/openAPIParserMock/index.js:119:38)
    at OpenAPIGeneratorMockJs.sampleFromSchema (/Users/koofrank/Documents/projects/wanda-sa-h5/node_modules/.pnpm/memoizee@0.4.15/node_modules/memoizee/lib/configure-map.js:61:24)
 ELIFECYCLE  Command failed with exit code 1.
chenshuai2144 commented 2 years ago

文件发出来看看

niujianqiang123 commented 2 years ago
// ============= paths =============

  "/base-server/api/assetsUsageRecord/{id}": {
      "get": {
        "tags": [
          "assets-usage-record-api"
        ],
        "summary": "get",
        "operationId": "getUsingGET_1",
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "id",
            "required": true,
            "type": "integer",
            "format": "int64"
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "schema": {
              "originalRef": "DataResult«AssetsUsageRecordVo»",
              "$ref": "#/definitions/DataResult«AssetsUsageRecordVo»"
            }
          },
          "401": {
            "description": "Unauthorized"
          },
          "403": {
            "description": "Forbidden"
          },
          "404": {
            "description": "Not Found"
          }
        }
      }
    }

 /**  错误:
  cloud-ui/node_modules/@umijs/openapi/dist/openAPIParserMock/index.js:29
    if (name.toLocaleLowerCase().endsWith('id')) {
                                 ^

RangeError: Maximum call stack size exceeded
    at String.endsWith (<anonymous>)
    at getDateByName (cloud-ui/node_modules/@umijs/openapi/dist/openAPIParserMock/index.js:29:34)
*/
niujianqiang123 commented 2 years ago

找到原因了,我的是因为这个报错

"produces": [
          "application/json"
 ]

改成以下即可

"produces": [
          "*/*"
        ],

生成的mock文件没有返回值,都是空对象 {}

fengluoX commented 5 months ago

这个应该是接口定义无限级联的情况,比如后端定义结构体 model.Response,里面有个字段的定义是model.Response,现实情况可能会比这复杂,比如a->b->c->a