对象数组作为 swagger 中的输入参数 [英] Array of objects as an input parameter in swagger
本文介绍了对象数组作为 swagger 中的输入参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试用 swagger 描述以下 post 参数:
I'm trying to describe the following post parameter in swagger:
{
"sources": [
{
"id": 101,
"parentId": 201
},{
"id": 102,
"parentId": 201
},{
"id": 102,
"parentId": 202
}
],
"destinationId": 301,
"param1": "value 1",
"param2": "value 2",
}
问题在于 sources
是一个对象数组,该 swagger 似乎不支持.这是我尝试过的:
The issue is that the sources
is an array of objects, that swagger does not seem to support. Here is what I tried:
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
知道如何解决这个限制吗?
Any idea on how to work around this limitation?
推荐答案
如果我理解正确,您要发布的请求正文是 json
对象,而不是 form
.在这种情况下,您的 swagger 文档需要修改如下:
If I understand correctly, your request body to post is a json
object instead of form
. In such case, your swagger document need to be modified as follows:
- 请求体为json时,使用
in:body
的参数代替in:formData
的多个参数. - 如果
in
是body
,则需要一个schema
对象. - 在
schema
下定义了 json 属性.如果type
属性为array
,则需要items
对象.
- When request body is json, a parameter with
in: body
is used instead of multiple parameters ofin: formData
. - If
in
isbody
, aschema
object is required. - Defined the json properties under
schema
. If the propertytype
isarray
,items
object is required.
以下是一个例子:
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 中的输入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文