The Wayback Machine - https://web.archive.org/web/20200704070854/https://github.com/YMFE/yapi/issues/1311
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

通过 apicurio + swagger 导入插件实现基于 model 的 restful api 快速定义方法 #1311

Open
ghostbody opened this issue Aug 20, 2019 · 4 comments

Comments

@ghostbody
Copy link

@ghostbody ghostbody commented Aug 20, 2019

简介

通过 yapi 定义出接口其实是一件很 challenge 和重复的事情。

一般的我们的基于“资源”概念的 restful 接口会对某个数据模型(model)实现 CRUD 接口 (增删改查)。

假设我们一个基本的管理用户的需求。

  • 获取用户列表 GET /api/v1/users?_num=10&_page=1
  • 获取某个用户 GET /api/v1/users/:id
  • 新增一个用户 POST /api/v1/users
  • 删除某个用户 DELETE /api/v1/users/:id
  • 更新某个用户 PUT /api/v1/users/:id

对于 yapi,意味着我们需要至少定义 5个接口,这些接口的 request 和 response 存在很大需要复用数据模型的需求。

最近找到一个工具,可以结合起来定义相关模型。

http://apicur.io

定义和使用

  1. 定义数据模型。这里有个好处就是,我们输入一个我们接口的示例 json,就能生成模型

    image

  2. 选择生成 Rest Resource

image

  1. 所有的 CRUD 接口全部生成好了

    image

    image

  2. 然后我们使用导出 swagger 功能,导出 openid spec。然后再导入到 yapi。则 yapi 的 api 全部定义好了

    image

  3. 另外,我们还能利用 yapi swagger 订阅功能实现一定的自动更新(前提是要将这个 swagger 写入到一个可通过 http 获取的地方)

    image

后续

感觉有时间可以写个插件内置到 yapi 这部分功能。(咕咕咕)

@tangcent
Copy link
Contributor

@tangcent tangcent commented Aug 21, 2019

我一直有个疑问,都用yapi了,为什么还是很多人继续使用swagger?
或者说swagger的优势在哪里?哪些特点是yapi无法满足的?

@ghostbody
Copy link
Author

@ghostbody ghostbody commented Aug 22, 2019

简单来说:因为 swagger 更加强大。

  1. swagger 拥有 open api spec (开放 API 声明规范)
  2. 已 open api spec 为基础,swagger 提供对整个API的开发周期都提供了相应的解决方案,是一个非常庞大的项目(包括设计、编码和测试,几乎支持所有语言)
  3. open api spec 被广泛使用,有一套自己成熟的生态,社区比较活跃。而 yapi 目前的生态只有几个插件。目前可以利用 open api 生态,在社区找到各种各样的解决方案
  4. yapi 导出的那份 json 格式你除了 yapi,哪里都不能用;而 swagger 定义的格式可以在 swagger-ui、导入 yapi、数据校验、postman(以前可以,现在 postman 似乎也是自己在干,暗中竞争)。既然一次定义可以多个地方使用,为何我不用 swagger
  5. 项目使用感受,yapi 不支持数据模型定义,restful api 需要多次定义重复的数据,维护非常蛋疼,直接劝退项目组大部分同学

@tangcent

@hellosean1025
Copy link
Member

@hellosean1025 hellosean1025 commented Aug 23, 2019

@ghostbody ghostbody 插件写好了吗,期待

@ghostbody
Copy link
Author

@ghostbody ghostbody commented Nov 27, 2019

唉,搞起来极其麻烦。

为什么 yapi 不用开源的 OAS 建模,而要自己设计一套描述接口的数据结构,导致很多东西很难做,而且和开源生态脱离。目前只能数据导入、导出方式实现,非常麻烦。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
You can’t perform that action at this time.