56

我有一个要记录的 REST 服务,其中一些接受简单的数组,例如:

[
  { "name":"a" },
  { "name":"b" },
  { "name":"c" }
]

我如何在 Swagger 模型部分对此进行描述?我只能像这样创建“命名数组”

model {
properties: { "arr": { "type":"array", ......

但它描述了这样的数据:

"arr": [
  { "name":"a" },
  { "name":"b" },
  { "name":"c" }
]
4

7 回答 7

22

Tony YUEN 很接近,但没有雪茄。这是在 OpenAPI/Swagger 中使用 YAML 的正确定义:

  /test:
post:
  summary: test 123
  description: test 123
  parameters:
    - name: param1
      in: body
      required: true
      description: test param1
      schema:
          $ref: '#/definitions/stackoverflow'
  responses:
    200:
      description: OK

这会产生:

stackoverflow2[
  {
     name: string
  }
]

托尼的例子产生:

[
  stackoverflow { 
                 name: string
  }
]

以 YAML 形式完成 Swagger/OpenAPI(复制和粘贴)

    swagger: '2.0'

################################################################################
#                              API Information                                 #
################################################################################
info:
  version: "Two-point-Oh!"
  title: Simple objects in array test
  description: |
    Simple objects in array test

################################################################################
#                                   Parameters                                 #
################################################################################

paths:
  /test:
    post:
      summary: Array with named objects
      description: Array with named objects
      parameters:
        - name: param1
          in: body
          required: true
          description: test param1
          schema:
            type: array
            items:
              $ref: '#/definitions/stackoverflow'
      responses:
        200:
          description: OK
  /test2:
    post:
      summary: Array with simpel (nameless) objects
      description: Array with simpel (nameless)  objects
      parameters:
        - name: param1
          in: body
          required: true
          description: test param1
          schema:
              $ref: '#/definitions/stackoverflow2'
      responses:
        200:
          description: OK
definitions:
  stackoverflow:
    type: object
    properties:
      name:
        type: string
        description: name of the object
  stackoverflow2:
    type: array
    items:
      type: object
      properties:
        name:
          type: string
          description: name of the object

这是 Swagger/OpenAPI 的 JSON 版本

    {
  "swagger" : "2.0",
  "info" : {
    "description" : "Simple objects in array test\n",
    "version" : "Two-point-Oh!",
    "title" : "Simple objects in array test"
  },
  "paths" : {
    "/test" : {
      "post" : {
        "summary" : "Array with named objects",
        "description" : "Array with named objects",
        "parameters" : [ {
          "in" : "body",
          "name" : "param1",
          "description" : "test param1",
          "required" : true,
          "schema" : {
            "type" : "array",
            "items" : {
              "$ref" : "#/definitions/stackoverflow"
            }
          }
        } ],
        "responses" : {
          "200" : {
            "description" : "OK"
          }
        }
      }
    },
    "/test2" : {
      "post" : {
        "summary" : "Array with simpel (nameless) objects",
        "description" : "Array with simpel (nameless)  objects",
        "parameters" : [ {
          "in" : "body",
          "name" : "param1",
          "description" : "test param1",
          "required" : true,
          "schema" : {
            "$ref" : "#/definitions/stackoverflow2"
          }
        } ],
        "responses" : {
          "200" : {
            "description" : "OK"
          }
        }
      }
    }
  },
  "definitions" : {
    "stackoverflow" : {
      "type" : "object",
      "properties" : {
        "name" : {
          "type" : "string",
          "description" : "name of the object"
        }
      }
    },
    "stackoverflow2" : {
      "type" : "array",
      "items" : {
        "$ref" : "#/definitions/stackoverflow2_inner"
      }
    },
    "stackoverflow2_inner" : {
      "properties" : {
        "name" : {
          "type" : "string",
          "description" : "name of the object"
        }
      }
    }
  }
}
于 2016-11-14T11:04:12.973 回答
12

将此粘贴到http://editor.swagger.io/#/ 并单击“尝试此操作”

{
  "swagger": "2.0",
  "info": {
    "version": "1.0.0",
    "title": "Privacy-Service API"
  },
  "paths": {
    "/allNames": {
      "post": {
        "consumes": [
          "application/json",
          "application/xml"
        ],
        "produces": [
          "application/json",
          "application/xml"
        ],
        "parameters": [
          {
            "name": "request",
            "in": "body",
            "schema": {
              "$ref": "#/definitions/ArrayOfNames"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "List of names",
            "schema": {
              "type": "array",
              "items": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  },
  "definitions": {
    "ArrayOfNames": {
      "type": "array",
      "items": {
        "minItems": 1,
        "type": "object",
        "required": [
          "name"
        ],
        "properties": {
          "name": {
            "type": "string"
          }
        }
      }
    }
  }
}
于 2016-12-21T21:20:58.427 回答
6

它可能看起来像这样:

    ...
    "parameters" : [{
      "name" : "whatEverThatArrayCalled",
      "type" : "array",
      "items" : {
        "$ref" : "whatEverThatArrayCalled"
      }
      ...
    }],
    "models" : {
   {
    "id" : "whatEverThatArrayCalled",
    "properties": 
        {
       "whatEverThatArrayCalled" :
            {
         "type" : "array",
         "items":{"name":"a",
                  "name":"b",
                  "name":"c"
                  },

             }
         }
    }
 }        

...

于 2015-07-27T22:56:15.527 回答
5

考虑到您提到的数组的格式

[
  { "name":"a" },
  { "name":"b" },
  { "name":"c" }
]

我想可以使用以下格式:

paths:
  /test:
    post:
      description: Test request
      operationId: test
      parameters:
        - in: body
          name: requestParameter
          required: true
          schema:
            type: array
            items:
              properties:
                name:
                  type: string
      responses:
        '200':
          description: Success response
于 2018-04-09T10:05:15.700 回答
3
  parameters:
  - name: "items"
    in: "formData"
    description: "description"
    required: "true"
    type: "array"
    items:
      type: "object"
      additionalProperties:
        properties:
          name:
            type: "string"

根据他们的文档https://swagger.io/docs/specification/data-models/dictionaries/,这应该会产生一个数组,其中的对象具有名为 name 的属性,并且数据类型是字符串。
可以通过请求正文访问它,例如request.body.items

更新:

看起来就足够了(没有额外的属性):

items:
  type: object
  properties:
    name:
      type: string

现在你得到了每个项目都有一个名为 name 的键和一个相应的值。

如果是这样,TO 要求的是什么......

于 2018-01-22T14:44:25.097 回答
2

我尝试了editor.swagger.io中的以下内容,它满足了这个问题的要求并且有效。POST 请求正文需要一个数组。该数组由“stackoverflow”项组成。每个项目都是一个对象,具有 name 属性。

paths:
  /test:
    post:
      summary: test 123
      description: test 123
      parameters:
        - name: param1
          in: body
          required: true
          description: test param1
          schema:
            type: array
            items:
              $ref: '#/definitions/stackoverflow'
      responses:
        200:
          description: OK
definitions:
  stackoverflow:
    type: object
    properties:
      name:
        type: string
        description: name of the object
于 2016-10-06T01:56:48.170 回答
0

如果我的理解是正确的,我认为以下可能是您想要的。

正如你提到的,

其中一些接受简单的数组

然后它将通过一个参数传递。

"parameters" : [{
  "name" : "param_name",
  "type" : "array",
  "items" : {
    "$ref" : "M"
  }
  ...
}]
...

对于模型部分:

"models" : {
  "M": {
    "id" : "M",
    "properties": {
       "name" : {
         "type" : "string"
       }
    }
  }
于 2013-11-29T08:09:37.063 回答