Sco*_*mas 6 swagger swagger-2.0 swagger-editor openapi
我正在寻找一种优雅的方式来定义可以使用 JSON 数据和表单数据的 api。以下代码段有效,但它并不优雅,并且需要在后端使用各种丑陋的代码。有没有更好的方法来定义这个?
现在什么工作:
paths:
/pets:
post:
consumes:
- application/x-www-form-urlencoded
- application/json
parameters:
- name: nameFormData
in: formData
description: Updated name of the pet
required: false
type: string
- name: nameJSON
in: body
description: Updated name of the pet
required: false
type: string
Run Code Online (Sandbox Code Playgroud)
我希望它如何工作的基本想法:
paths:
/pets:
post:
consumes:
- application/x-www-form-urlencoded
- application/json
parameters:
- name: name
in:
- formData
- body
description: Updated name of the pet
required: true
type: string
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为该in值必须是字符串,而不是数组。
有什么好主意吗?
在 OpenAPI 2.0 中,无法对此进行描述。表单和正文参数是互斥的,因此操作可以具有表单数据或 JSON 正文,但不能同时具有两者。一种可能的解决方法是拥有两个单独的端点——一个用于表单数据,另一个用于 JSON——如果这在您的场景中是可以接受的。
可以使用 OpenAPI 3.0 描述您的场景。所述requestBody.content.<media-type>关键字被用于定义由操作接受各种媒体类型,如application/json和application/x-www-form-urlencoded以及它们的模式。媒体类型可以具有相同的模式或不同的模式。
openapi: 3.0.0
...
paths:
/pets:
post:
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
application/x-www-form-urlencoded:
schema:
$ref: '#/components/schemas/Pet'
responses:
'200':
description: OK
components:
schemas:
Pet:
type: object
properties:
name:
type: string
description: Updated name of the pet
required:
- name
Run Code Online (Sandbox Code Playgroud)
更多信息:
| 归档时间: |
|
| 查看次数: |
3260 次 |
| 最近记录: |