对象数组作为 swagger 中的输入参数 [英] Array of objects as an input parameter in swagger

查看:66
本文介绍了对象数组作为 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:

  1. 请求体为json时,使用in:body的参数代替in:formData的多个参数.
  2. 如果inbody,则需要一个schema 对象.
  3. schema 下定义了 json 属性.如果type属性为array,则需要items对象.
  1. When request body is json, a parameter with in: body is used instead of multiple parameters of in: formData.
  2. If in is body, a schema object is required.
  3. Defined the json properties under schema. If the property type is array, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆