简体   繁体   English

使用 Swagger 指定一个数组作为参数

[英]Specify an array as a parameter with Swagger

How do I specify an array as a parameter?如何将数组指定为参数? For instance, post to /persons can be given strings username, firstname, and lastname, and array myArray .例如,可以给 /persons 的 post 提供字符串 username、firstname 和 lastname以及数组 myArray

paths:
  /persons:
    post:
      parameters:
        - name: person_what_is_the_purpose_of_this
          in: body
          description: The person to create.
          schema:
            required:
              - username
            properties:
              firstName:
                type: string
              lastName:
                type: string
              username:
                type: string
              myArray:
                type: array
                  items:
                    properties:
                      myArrayElement:
                        type: string
      responses:
        200:
          description: A list of Person
          schema:
            type: array
            items:
              required:
                - username
              properties:
                firstName:
                  type: string
                lastName:
                  type: string
                username:
                  type: string
swagger: "2.0"
info:
  version: "1.0.0"
  title: Swagger Petstore
host: petstore.swagger.io
basePath: /v2
schemes:
  - http
paths:
  /pets/findByStatus:
    get:
      parameters:
        - in: query
          name: status
          type: array
          items:
            type: string
      responses:
        "200":
          description: successful operation
          schema:
            type: array
            items:
              type: object
              required:
                - name
                - photoUrls
              properties:
                id:
                  type: integer
                  format: int64
                category:
                  type: object
                  properties:
                    id:
                      type: integer
                      format: int64
                    name:
                      type: string
                name:
                  type: string
                  example: doggie
                photoUrls:
                  type: array
                  items:
                    type: string
                tags:
                  type: array
                  items:
                    type: object
                    properties:
                      id:
                        type: integer
                        format: int64
                      name:
                        type: string
        "400":
          description: Invalid status value

You need to specify collectionFormat: multi您需要指定collectionFormat: multi

For your array it would look like this, be sure to put it on the same level as the type:对于您的数组,它看起来像这样,请确保将其与类型放在同一级别:

myArray:
  type: array
  collectionFormat: multi

Documentation about arrays 关于数组的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM