对象数组作为swagger中的输入参数

map*_*phe 4 swagger swagger-2.0

我试图在swagger中描述以下post参数:

{
    "sources": [
        {
            "id": 101,
            "parentId": 201
        },{
            "id": 102,
            "parentId": 201
        },{
            "id": 102,
            "parentId": 202
        }
    ],
    "destinationId": 301,
    "param1": "value 1",
    "param2": "value 2",
}
Run Code Online (Sandbox Code Playgroud)

问题是,这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
Run Code Online (Sandbox Code Playgroud)

关于如何解决这个限制的任何想法?

Wil*_*son 14

如果我理解正确,您发布的请求正文是一个json对象而不是form.在这种情况下,您的招摇文档需要修改如下:

  1. 当请求体是json时,使用参数with in: body而不是多个参数in: formData.
  2. 如果inbody,schema则需要一个对象.
  3. 定义了json属性schema.如果属性typearray,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
Run Code Online (Sandbox Code Playgroud)

  • 你能告诉我你在哪里做这些改变吗?或者我想问的是,在哪个文件中编写了上面的代码.我无法在swagger API中找到上述代码. (2认同)