chenshuai2144 / openapi2typescript

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

openapi 不能生成嵌套的结构(没有报错 #77

Open tankpanv opened 2 years ago

tankpanv commented 2 years ago

"@umijs/plugin-openapi": "^1.3.3"

我使用openapi 生成接口的时候,我服务端model是结构性的 type ArticleItem struct { CollectedData * ArticleCollectedData

ArticleFormData ArticleFormData Highlight map[string]HighlightItem } 我预期生成一个也是结构性的结构比如 type ArticleItem = { article_form_data?: ArticleFormData; collected_data?: ArticleCollectedData; };

但是通过plugin-openapi生成接口的时候,ArticleItem 结构只得到了 type ArticleItem = ArticleFormData,并没有生成嵌套的结构声明; 执行npm run openapi 命令显示生成成功,并没有报错。我把代码回滚到了老的版本也不行。不知道是不是哪个依赖的变动导致了这个问题

下面是我用openapi生成的typing.d.ts 文件和swagger.json 的结构 ` type ArticleFormData = { /* 摘要 / abstract?: string; / 分类列表 / category?: string[]; / 清理格式后的内容 / clean_content?: string; / content 内容 / content?: string; / 编辑器类型 / editor?: number; / 题材 / genre?: number; / id / id?: number; logo?: string; / 是否转载 / reship_url?: string; source?: number; / 标签列表 / tags?: string[]; / 文章标题 */ title?: string; user_name?: string; };

type ArticleItem = ArticleFormData;`

swagger.json 的model ` { "swagger": "2.0", "info": { "contact": {} }, "paths": {

"/article/detail": {
    "get": {
        "description": "根据id获取文章",
        "consumes": [
            "application/json"
        ],
        "produces": [
            "application/json"
        ],
        "tags": [
            "article"
        ],
        "summary": "根据id获取文章",
        "parameters": [
            {
                "type": "integer",
                "description": " ID",
                "name": "id",
                "in": "query",
                "required": true
            }
        ],
        "responses": {
            "200": {
                "description": "OK",
                "schema": {
                    "$ref": "#/definitions/model.ArticleItem"
                }
            },
            "500": {
                "description": "Internal Server Error",
                "schema": {
                    "$ref": "#/definitions/model.ArticleItem"
                }
            }
        }
    }
},

"model.ArticleCollectedData": {
    "type": "object",
    "properties": {

        "body": {
            "type": "string"
        }
    }
},
"model.ArticleFormData": {
    "type": "object",
    "properties": {
        "category": {
            "description": "分类列表",
            "type": "array",
            "items": {
                "type": "string"
            }
        }
    }
},
"model.ArticleItem": {
    "type": "object",
    "properties": {
        "article_form_data": {
            "description": "提交的变量",
            "$ref": "#/definitions/model.ArticleFormData"

        },
        "collected_data": {
            "$ref": "#/definitions/model.ArticleCollectedData"

        },
        "highlight": {
            "type": "object",
            "additionalProperties": {
                "$ref": "#/definitions/model.HighlightItem"
            }
        }
    }
},

"model.HighlightItem": {
    "type": "object",
    "properties": {
        "pos": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/model.PosItem"
            }
        },
        "rich_text": {
            "type": "string"
        },
        "text": {
            "type": "string"
        }
    }
}

} }`

chenshuai2144 commented 2 years ago

来个pr

greenking19 commented 1 year ago

我也遇到了这个问题 用nestjs/swagger生成的。 我里面弄了嵌套 。 比如下面的这个 pos最终是

interface HighlightItem{
   ...
   pos: string[]
}

swagger-json数据

"model.HighlightItem": {
    "type": "object",
    "properties": {
        "pos": {
            "type": "array",
            "items": {
                "$ref": "#/definitions/model.PosItem"
            }
        },
        "rich_text": {
            "type": "string"
        },
        "text": {
            "type": "string"
        }
    }
}
greenking19 commented 1 year ago

我用的是@nestjs/swagger 这个是我一个接口的swagger-json 我修改了PaginatedDto的list类型为SiteDto

"responses": {
  "200": {
    "description": "获取列表",
    "content": {
      "application/json": {
        "schema": {
          "allOf": [
            { "$ref": "#/components/schemas/HttpResponse" },
            {
              "properties": {
                "data": {
                  "$ref": "#/components/schemas/PaginatedDto",
                  "properties": {
                    "list": {
                      "description": "列表数据",
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/SiteDto"
                      }
                    }
                  }
                }
              }
            }
          ]
        }
      }
    }
  },
  "401": { "description": "权限不够" }
}

在swagger-ui里面显示跟预期的一样

{
  "code": "string",
  "msg": "string",
  "data": {
    "pageSize": 0,
    "pageIndex": 0,
    "total": 0,
    "totalPage": 0,
    "list": [
      {
        "name": "string",
        "createTime": "string"
      }
    ]
  }
}

但是生成的接口.d.ts的文档 因为我的list是范型的,这种情况怎么解决范型的问题呢? HttpResponse 里面的data里面范型倒是没问题了, 但是第二层嵌套好像就不行了

  type PaginatedDto = {
    /** 每页的大小 */
    pageSize: number;
    /** 当前页面 */
    pageIndex: number;
    /** 总条数 */
    total: number;
    /** 总页面数量 */
    totalPage: number;
    /** 页面数据 */
    list: string[];
  };