对象数组作为招摇的输入参数
Array of objects as an input parameter in swagger
我正在尝试用 swagger 描述以下 post 参数:
{
"sources": [
{
"id": 101,
"parentId": 201
},{
"id": 102,
"parentId": 201
},{
"id": 102,
"parentId": 202
}
],
"destinationId": 301,
"param1": "value 1",
"param2": "value 2",
}
问题是 sources
是一个对象数组,swagger 似乎不支持。这是我尝试过的:
paths:
/bulk-action:
post:
parameters:
- name: sources
in: formData
type: array
enum:
$ref: '#/definitions/BulkSource'
- name: destinationId
in: formData
type: integer
- name: param1
in: formData
type: string
- name: param2
in: formData
type: string
definitions:
BulkSource:
type: object
properties:
id:
type: integer
parentId:
type: integer
知道如何解决这个限制吗?
如果我理解正确的话,你对 post 的请求正文是一个 json
对象而不是 form
。在这种情况下,您的 swagger 文档需要修改如下:
- 当请求体为json时,使用
in: body
的参数代替in: formData
的多个参数。
- 如果
in
是 body
,则需要一个 schema
对象。
- 在
schema
下定义了 json 属性。如果 属性 type
是 array
,则需要 items
对象。
示例如下:
paths:
/bulk-action:
post:
consumes:
- application/json
parameters:
- name: body
in: body
schema:
properties:
sources:
type: array
items:
$ref: '#/definitions/BulkSource'
destinationdId:
type: integer
responses:
200:
description: OK
definitions:
BulkSource:
type: object
properties:
id:
type: integer
parentId:
type: integer
我正在尝试用 swagger 描述以下 post 参数:
{
"sources": [
{
"id": 101,
"parentId": 201
},{
"id": 102,
"parentId": 201
},{
"id": 102,
"parentId": 202
}
],
"destinationId": 301,
"param1": "value 1",
"param2": "value 2",
}
问题是 sources
是一个对象数组,swagger 似乎不支持。这是我尝试过的:
paths:
/bulk-action:
post:
parameters:
- name: sources
in: formData
type: array
enum:
$ref: '#/definitions/BulkSource'
- name: destinationId
in: formData
type: integer
- name: param1
in: formData
type: string
- name: param2
in: formData
type: string
definitions:
BulkSource:
type: object
properties:
id:
type: integer
parentId:
type: integer
知道如何解决这个限制吗?
如果我理解正确的话,你对 post 的请求正文是一个 json
对象而不是 form
。在这种情况下,您的 swagger 文档需要修改如下:
- 当请求体为json时,使用
in: body
的参数代替in: formData
的多个参数。 - 如果
in
是body
,则需要一个schema
对象。 - 在
schema
下定义了 json 属性。如果 属性type
是array
,则需要items
对象。
示例如下:
paths:
/bulk-action:
post:
consumes:
- application/json
parameters:
- name: body
in: body
schema:
properties:
sources:
type: array
items:
$ref: '#/definitions/BulkSource'
destinationdId:
type: integer
responses:
200:
description: OK
definitions:
BulkSource:
type: object
properties:
id:
type: integer
parentId:
type: integer