添加openapi编写示例

This commit is contained in:
purplenigma 2021-05-13 12:20:24 +08:00
parent 0aff29b24d
commit 24afed3120
7 changed files with 25 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@ -225,6 +225,31 @@ Django REST Framework 本身提供方便的工具可以查看已有接口的返
页面左边为`openapi.yaml`的内容右边为解析后的接口呈现。可以看到目前已经定义了5个API接口包括其HTTP方法、URL Path、
返回格式等内容。
### 编辑示例
编写openapi 文档涉及以下步骤:
1、明确接口需求如 我们需要查询文章列表,在最近文章页面里展示
2、设计请求url、请求参数、响应内容这里为了让小伙伴们先熟悉openapi已经给小伙伴做好了接口设计大家可以在此基础上修改调整开发中需要前后端根据需求协商确定url、属性名、类型等接口信息
> 可以在前端浏览器如chrome 快捷键F12打开web调试器 查看接口请求和响应信息
![Swagger Editor openapi01](./images/task01-openapi-edit01.png)
![Swagger Editor openapi02](./images/task01-openapi-edit02.png)
3、打开编辑器如swagger editor,编辑器或者插件提供了一些便捷操作辅助我们编写这里我们先插入path
![Swagger Editor openapi03](./images/task01-openapi-edit03.png)
4、再添加操作
![Swagger Editor openapi04](./images/task01-openapi-edit04.png)
5、添加响应信息
![Swagger Editor openapi05](./images/task01-openapi-edit05.png)
> 为了结构清晰和数据复用(相同的内容可以用$ref引用,我们在 components.schemas 下创建 响应对象
![Swagger Editor openapi07](./images/task01-openapi-edit07.png)
6、参考其他接口或者openapi规范 手动调整一下接口,如添加参数等
![Swagger Editor openapi06](./images/task01-openapi-edit06.png)
> 编写时注意yaml语法、tab空格对齐
7、尝试执行如果响应如期正常返回就编写完成了
## 运行mock server