chenshuai2144 / openapi2typescript

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

allOf 生成类型声明有问题 #89

Closed hlouis closed 1 year ago

hlouis commented 1 year ago

版本号:1.7.1 测试Yaml:

{
  "components": {
    "schemas": {
      "Batch": {
        "allOf": [
          {
            "$ref": "#/components/schemas/Response"
          },
          {
            "$ref": "#/components/schemas/Batch_allOf"
          }
        ]
      },
      "BatchList": {
        "allOf": [
          {
            "$ref": "#/components/schemas/Response"
          },
          {
            "properties": {
              "data": {
                "items": {
                  "$ref": "#/components/schemas/Batch"
                },
                "type": "array"
              }
            },
            "type": "object"
          }
        ]
      },
      "Batch_allOf": {
        "properties": {
          "data": {
            "type": "object"
          }
        },
        "type": "object"
      },
      "Response": {
        "description": "所有API的返回数据,如果业务处理失败,success必然是false,\n如果业务处理成功,success为True,并添加data字段携带需要的数据\n",
        "properties": {
          "errorCode": {
            "description": "业务约定的错误码",
            "type": "string"
          },
          "errorMessage": {
            "description": "业务上的错误信息",
            "type": "string"
          },
          "success": {
            "description": "业务上的请求是否成功",
            "type": "boolean"
          }
        },
        "required": [
          "success"
        ],
        "type": "object"
      }
    }
  },
  "info": {
    "title": "NSFW Account API",
    "version": "1.0.0"
  },
  "openapi": "3.0.1",
  "paths": {},
  "servers": [
    {
      "url": "http://localhost:1234/"
    }
  ]
}

测试代码

  await openAPI.generateService({
    schemaPath: `${__dirname}/nsfw-api.json`,
    serversPath: './servers-nsfw',
  })

输出的typings.d.ts内容

declare namespace API {
  type Batch = {
    ''?: Response;
  } & {
    ''?: BatchAllOf;
  };

  type BatchAllOf = {
    data?: Record<string, any>;
  };

  type BatchList = {
    ''?: Response;
  } & {
    data?: Batch[];
  };

  type Response = {
    /** 业务约定的错误码 */
    errorCode?: string;
    /** 业务上的错误信息 */
    errorMessage?: string;
    /** 业务上的请求是否成功 */
    success: boolean;
  };
}

期望的内容

declare namespace API {
  type Batch = Response & BatchAllOf;

  type BatchAllOf = {
    data?: Record<string, any>;
  };

  type BatchList = Response & {
    data?: Batch[];
  };

  type Response = {
    /** 业务约定的错误码 */
    errorCode?: string;
    /** 业务上的错误信息 */
    errorMessage?: string;
    /** 业务上的请求是否成功 */
    success: boolean;
  };
}