Swagger2.0 规范

Swagger2.0 规范1. 简介2. 版本历史3. 定义3.1. 路径模板3.2. 媒体类型3.3. HTTP状态码4. 规范4.1. 格式4.2. 文件结构4.3. 数据类型4.4. 架构4.4.1. Swagger对象固定字段模式对象4.4.2. Info对象固定字段模式对象Info对象示例:4.4.3. Contact对象固定字段模式对象Contact对象示例:4.4.4. License对象固定字段模式对象License对象示例:4.4.5. Paths对象模式字段Paths对象示例4.4.6. PathItem对象固定字段模式字段PathItem对象示例4.4.7. Operation对象固定字段模式对象Operation对象示例4.4.8. ExternalDocument对象固定字段模式对象ExternalDocument对象示例4.4.9. Parameter对象固定字段模式字段Parameter对象示例Body参数其他参数4.4.10. Items对象固定字段模式对象Items对象示例4.4.11. Responses对象固定字段模式字段Responses对象示例4.4.12. Response对象固定字段模式对象Response对象示例4.4.13. Headers对象模式字段Headers对象示例4.4.14. Example对象模式字段Example对象示例4.4.15. Header对象模式对象Header对象示例4.4.16. Tag对象固定字段模式字段Tag对象示例4.4.17. Reference对象固定字段Reference对象示例引用模式文件示例具有嵌入式架构的相对文件示例4.4.18. Schema对象固定字段模式对象组合和继承(多态)XML建模Schema对象示例原始示例简单模型包含Map/Dictionary属性的模型Model示例组合的模型多态模型支持4.4.19. XML 对象固定字段模式对象XML对象示例非XML元素XML名称替换XML属性、前缀和命名空间XML 数组4.4.20. Definitions对象模式字段Definitions对象示例4.4.21. Parameters Definitions对象模式字段Parameters Definitions义对象示例4.4.22. Responses Definitions对象模式字段Responses Definitions对象示例4.4.23. SecurityDefinitions对象模式字段SecurityDefinitions对象示例4.4.24. Security Scheme对象固定字段模式字段Security Scheme对象示例基本认证示例API Key 示例隐含的 OAuth2 示例4.4.25. Scopes 对象模式字段模式对象Scopes 对象示例4.4.26. 安全需求对象模式字段Security Requirement对象示例非OAuth2安全要求OAuth2 安全需要4.5. 规范扩展4.6. 安全筛选

文档内使用的关键字 "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" 将根据下述规范解释 RFC 2119.

Swagger 规范许可协议 The Apache License, Version 2.0.

1. 简介

Swagger™ 是用于描述和记录RESTful API的项目。

Swagger规范定义了一组描述这样的API所需的文件。 Swagger-UI项目可以使用这些文件来显示API和Swagger-Codegen来生成各种语言的客户端。 附加工具程序还可以使用生成的文件,如测试工具。

2. 版本历史

版本日期事件
2.02014-09-08发布Swagger 2.0
1.22014-03-14发布正式文件
1.12012-08-22发布 Swagger 1.1
1.02011-08-10首次发布Swagger规范

3. 定义

3.1. 路径模板

Path Templating

路径模板是指使用花括号({})将URL路径的一部分标记为可替换的路径参数。

3.2. 媒体类型

Mime Types

MimeType定义分布在多个资源中。 MimeType的定义应该符合RFC 6838.

一些可能的MimeType定义的例子:

  text/plain; charset=utf-8
  application/json
  application/vnd.github+json
  application/vnd.github.v3+json
  application/vnd.github.v3.raw+json
  application/vnd.github.v3.text+json
  application/vnd.github.v3.html+json
  application/vnd.github.v3.full+json
  application/vnd.github.v3.diff
  application/vnd.github.v3.patch

3.3. HTTP状态码

HTTP Status Codes HTTP状态码用于指示执行操作的状态。 可用状态代码由 RFC 7231IANA Status Code Registry构成.

4. 规范

4.1. 格式

根据Swagger规范描述RESTful API的文件被表示为JSON对象,并符合JSON标准。 作为JSON的超集,YAML也可以被使用 代表Swagger规范文件。

例如,如果一个字段被称为具有数组值,则将使用JSON数组表示形式:

 
xxxxxxxxxx
{
   "field" : [...]
}

虽然API是使用JSON描述的,但它并没有将JSON输入/输出强加给API本身。

规范中的所有字段名称均为区分大小写

Swagger的Schema暴露两种类型的字段。 具有声明名称的固定字段和模式字段。它们均声明字段名称的正则表达式模式。 只要每个都有唯一的名称,模式化的字段可以有多个出现。

4.2. 文件结构

API的Swagger表示由单个文件组成。 但是,部分定义可以分为单独的文件,由用户自行决定。 这适用于规范中的$ref字段,如下所示JSON Schema .

按照惯例,Swagger规范文件名为swagger.json

4.3. 数据类型

Swagger规范中的原始数据类型基于所支持的类型 JSON-Schema Draft 4. 使用Schema Object描述模型,该模式是JSON模式草案4的一个子集。

Parameter ObjectResponse Object使用一个额外的原始数据类型"file"将参数类型或响应设置为文件。

基本数据类型有一个可选的修饰符属性format。 Swagger使用几种已知格式更精确地定义正在使用的数据类型。 "format"属性是一个开放的"string"值属性,可以有任何值来支持文档需求。 即使这些规范未定义,仍然可以使用"email","uuid"等来描述格式。 不包含"format"属性的类型遵循JSON模式的定义(除了上面定义的"file"类型)。 Swagger规范定义的格式如下:

通用名称typeformat注释
integerintegerint32有符号 32 bits
longintegerint64有符号 64 bits
floatnumberfloat
doublenumberdouble
stringstring
bytestringbytebase64 编码字符
binarystringbinary任何八进制序列
booleanboolean
datestringdate参考规范的 full-date - RFC3339
dateTimestringdate-time参考规范的 date-time - RFC3339
passwordstringpassword用于提示输入需要被遮罩

4.4. 架构

4.4.1. Swagger对象

这里开始介绍API规范的根文档对象。 它将以前的资源列表和API声明(版本1.2及更早版本)组合在一起成为一个文档。

固定字段
字段名类型描述
swaggerstring必须的. 指定正在使用的Swagger Specification版本。 Swagger UI和其他客户端可以使用它来解释API列表。 值必须为"2.0"`。
infoInfo Object必须的. 提供有关API的元数据。 根据需要客户端可能会使用该元数据。
hoststring主机(名称或ip)服务于API。 这必须是主机,不包括传输协议或子路径。 它可以包括一个端口。 如果不包含"主机",则将使用提供文档的主机(包括端口)。 host不支持 path templating.
basePathstring提供API的基本路径,它相对于host。 如果不包括API,则直接在"主机"下提供。 值必须以前面的斜杠(/)开头。 basePath不支持path template
schemes[string]API的传输协议。 值必须来自列表:"http""https""ws""wss"。 如果不包括"scheme",则使用的默认方案是用于访问Swagger定义本身的默认方案。
consumes[string]API可以使用的MimeType的列表。 再根对象这里全局定义会影响所有的API,但可以在特定的API调用上覆盖。 值必须如下所述 Mime Types.
produces[string]API可以生成的MimeType的列表。 这是所有API的全局,但可以在特定的API调用上被覆盖。 值必须如下所述 Mime Types.
pathsPaths Object必须的. API的可用路径和操作。
definitionsDefinitions Object定义对象,用于保存操作生成和使用的数据类型。
parametersParameters Definitions Object跨操作使用的参数的对象。 此属性为所有操作定义全局参数。
responsesResponses Definitions Object用来保存可以跨操作使用的响应。 此属性定义所有操作的全局响应。
securityDefinitionsSecurity Definitions Object可以在规范中使用的安全方案定义。
security[Security Requirement Object]声明对整个API适用哪些安全方案。 值列表描述了可以使用的替代安全方案(即安全性要求之间是逻辑OR的关系)。 单个的操作可以覆盖此定义。
tags[Tag Object]规范使用的附加元数据的标签列表。 标签的顺序可以用于通过解析工具反映其顺序。 并不是所有的Operation Object使用的标签都必须声明。 未声明的标签可以随机组织或基于工具的逻辑进行组织。 列表中的每个标签名称必须是唯一的。
externalDocsExternal Documentation Object其他外部文档。
模式对象
字段模式类型描述
^x-Any允许扩展到Swagger架构。 字段名必须以x-开头,例如x-internal-id。 该值可以是"null",一个原始数据类型,一个数组或一个对象。 有关详细信息,请参阅供应商扩展

4.4.2. Info对象

该对象提供有关API的元数据。 客户端可以根据需要使用元数据,例如便于在Swagger-UI中显示的元数据。

固定字段
字段名类型描述
titlestring必须的. 应用程序的标题.
descriptionstring应用程序的短描述. GFM syntax 可用于富文本表示。
termsOfServicestringAPI的服务条款.
contactContact Object暴露的API的联系信息。
licenseLicense Object暴露的API的许可证信息。
versionstring必须的. 提供应用程序API的版本(不要与规范版本混淆)。
模式对象
模式字段类型描述
^ x-Any允许扩展到Swagger架构。 字段名必须以x-开头,例如x-internal-id。 该值可以是"null",一个原始数据类型,一个数组或一个对象。 有关详细信息,请参阅供应商扩展
Info对象示例:
 
xxxxxxxxxx
{
  "title": "Swagger Sample App",
  "description": "This is a sample server Petstore server.",
  "termsOfService": "http://swagger.io/terms/",
  "contact": {
    "name": "API Support",
    "url": "http://www.swagger.io/support",
    "email": "support@swagger.io"
  },
  "license": {
    "name": "Apache 2.0",
    "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
  },
  "version": "1.0.1"
}
 
xxxxxxxxxx
title: Swagger Sample App
description: This is a sample server Petstore server.
termsOfService: http://swagger.io/terms/
contact:
  name: API Support
  url: http://www.swagger.io/support
  email: support@swagger.io
license:
  name: Apache 2.0
  url: http://www.apache.org/licenses/LICENSE-2.0.html
version: 1.0.1

4.4.3. Contact对象

暴露的API的联系信息。

固定字段
字段名称类型描述
namestring联系人/组织的识别名称。
urlstring指向联系人信息的URL。 必须是URL的格式。
emailstring联系人/组织的电子邮件地址。 必须采用电子邮件地址格式。
模式对象
模式字段类型描述
^ x-Any允许扩展到Swagger架构。 字段名必须以x-开头,例如x-internal-id。 该值可以是"null",一个原始数据类型,一个数组或一个对象。 有关详细信息,请参阅供应商扩展
Contact对象示例:
 
xxxxxxxxxx
{
  "name": "API Support",
  "url": "http://www.swagger.io/support",
  "email": "support@swagger.io"
}
 
xxxxxxxxxx
name: API Support
url: http://www.swagger.io/support
email: support@swagger.io

4.4.4. License对象

暴露的API的许可信息。

固定字段
字段名称类型描述
namestring必需的. 用于API的许可证名称。
urlstring用于API的许可证的URL。 必须是URL的格式。
模式对象
字段模式类型描述
^ x-Any允许扩展到Swagger架构。 字段名必须以x-开头,例如x-internal-id。 该值可以是"null",一个原始数据类型,一个数组或一个对象。 有关详细信息,请参阅供应商扩展
License对象示例:
 
xxxxxxxxxx
{
  "name": "Apache 2.0",
  "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
}
 
xxxxxxxxxx
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html

4.4.5. Paths对象

描述各个端点的相对路径。 该路径附加到basePath,以构造完整的URL。 由于ACL约束,路径可能为空。

模式字段
字段模式类型描述
/{path}Path Item Object单个端点的相对路径。 字段名必须以斜杠开头。 该路径附加到basePath,以构造完整的URL。 路径模板是允许的。
^ x-Any允许扩展到Swagger架构。 字段名必须以x-开头,例如x-internal-id。 该值可以是"null",一个原始数据类型,一个数组或一个对象。 有关详细信息,请参阅供应商扩展
Paths对象示例
 
xxxxxxxxxx
{
  "/pets": {
    "get": {
      "description": "Returns all pets from the system that the user has access to",
      "produces": [
        "application/json"
      ],
      "responses": {
        "200": {
          "description": "A list of pets.",
          "schema": {
            "type": "array",
            "items": {
              "$ref": "#/definitions/pet"
            }
          }
        }
      }
    }
  }
}
 
xxxxxxxxxx
/pets:
  get:
    description: Returns all pets from the system that the user has access to
    produces:
    - application/json
    responses:
      '200':
        description: A list of pets.
        schema:
          type: array
          items:
            $ref: '#/definitions/pet'

4.4.6. PathItem对象

描述单个路径上可用的操作。 由于ACL约束,路径项可能为空。 路径本身仍然暴露在文档查看器中,但是它们不知道哪些操作和参数可用。

固定字段
字段名称类型描述
$refstring允许此路径项的外部定义。引用的结构必须是Path Item Object的格式。如果引用的定义与此Path Item的定义之间存在冲突,则行为为undefined
getOperation Object此路径上的GET操作的定义。
putOperation Object此路径上的PUT操作的定义。
postOperation Object这个路径上POST操作的定义。
deleteOperation Object这个路径上DELETE操作的定义。
optionsOperation Object此路径上OPTIONS操作的定义。
headOperation Object这个路径上的HEAD操作的定义。
patchOperation ObjectPATCH操作在此路径上的定义。
parameter[Parameter Object| Reference Object]适用于此路径下描述的所有操作的参数列表。这些参数可以在操作级别覆盖,但不能在那里删除。列表不得包含重复的参数。唯一参数由namelocation的组合定义。该列表可以使用Reference对象链接到Swagger对象的参数中定义的参数。最多可以有一个"body"参数。
模式字段
模式字段类型描述
^ x-Any允许扩展到Swagger架构。字段名必须以x-开头,例如x-internal-id。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展
PathItem对象示例
 
xxxxxxxxxx
{
  "get": {
    "description": "Returns pets based on ID",
    "summary": "Find pets by ID",
    "operationId": "getPetsById",
    "produces": [
      "application/json",
      "text/html"
    ],
    "responses": {
      "200": {
        "description": "pet response",
        "schema": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Pet"
          }
        }
      },
      "default": {
        "description": "error payload",
        "schema": {
          "$ref": "#/definitions/ErrorModel"
        }
      }
    }
  },
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "description": "ID of pet to use",
      "required": true,
      "type": "array",
      "items": {
        "type": "string"
      },
      "collectionFormat": "csv"
    }
  ]
}
 
xxxxxxxxxx
get:
  description: Returns pets based on ID
  summary: Find pets by ID
  operationId: getPetsById
  produces:
  - application/json
  - text/html
  responses:
    '200':
      description: pet response
      schema:
        type: array
        items:
          $ref: '#/definitions/Pet'
    default:
      description: error payload
      schema:
        $ref: '#/definitions/ErrorModel'
parameters:
- name: id
  in: path
  description: ID of pet to use
  required: true
  type: array
  items:
    type: string
  collectionFormat: csv

4.4.7. Operation对象

描述路径上的单个API操作。

固定字段
字段名称类型描述
tags[string]API文档控件的标签列表。标签可以用于资源或任何其他限定符的逻辑分组。
summarystring简要介绍。为了在swagger-ui中获得最大的可读性,该字段应该小于120个字符。
descriptionstring操作行为的详细说明。 GFM语法可用于富文本表示。
externalDocsExternal Documentation Object此操作的其他外部文档。
operationIdstring用于识别操作的唯一字符串。在API中描述的所有操作中,ID必须是唯一的。工具和库可以使用operationId来唯一标识操作,因此,建议遵循常用的编程命名约定。
consumes[string]操作可以使用的MimeType列表。这覆盖了Swagger对象的consumes定义。可以使用空值来清除全局定义。值必须按照MimeType中所述。
produces[string]操作可以生成的MimeType列表。这覆盖了Swagger对象的produces定义。可以使用空值来清除全局定义。值必须按照MimeType中所述。
parametersParameter对象Reference对象适用于此操作的参数列表。如果已经在Path Item上定义了一个参数,新定义将覆盖它,但不能将其删除。列表不得包含重复的参数。唯一参数由namelocation的组合定义。该列表可以使用Reference对象链接到Swagger对象的参数中定义的参数。最多可以有一个"body"参数。
responsesResponse对象必需从执行此操作返回可能的响应的列表。
scheme[string]操作的传输协议。值必须来自列表:"http""https""ws""wss"。该值覆盖Swagger对象schemes定义。
deprecatedboolean声明此操作已被弃用。应该禁止使用声明的操作。默认值为"false"。
security[SecurityRequirement对象]声明应用哪种安全方案进行此操作。值列表描述了可以使用的替代安全方案(即安全性要求之间存在逻辑OR)。此定义覆盖任何已声明的顶级security。要删除顶级安全声明,可以使用空数组。
模式对象
模式字段类型描述
^x-Any允许扩展到Swagger架构。 字段名必须以x-开头,例如x-internal-id。 该值可以是"null",一个原始数据类型,一个数组或一个对象。 有关详细信息,请参阅供应商扩展
Operation对象示例
 
xxxxxxxxxx
{
  "tags": [
    "pet"
  ],
  "summary": "Updates a pet in the store with form data",
  "description": "",
  "operationId": "updatePetWithForm",
  "consumes": [
    "application/x-www-form-urlencoded"
  ],
  "produces": [
    "application/json",
    "application/xml"
  ],
  "parameters": [
    {
      "name": "petId",
      "in": "path",
      "description": "ID of pet that needs to be updated",
      "required": true,
      "type": "string"
    },
    {
      "name": "name",
      "in": "formData",
      "description": "Updated name of the pet",
      "required": false,
      "type": "string"
    },
    {
      "name": "status",
      "in": "formData",
      "description": "Updated status of the pet",
      "required": false,
      "type": "string"
    }
  ],
  "responses": {
    "200": {
      "description": "Pet updated."
    },
    "405": {
      "description": "Invalid input"
    }
  },
  "security": [
    {
      "petstore_auth": [
        "write:pets",
        "read:pets"
      ]
    }
  ]
}
 
xxxxxxxxxx
tags:
- pet
summary: Updates a pet in the store with form data
description: ""
operationId: updatePetWithForm
consumes:
- application/x-www-form-urlencoded
produces:
- application/json
- application/xml
parameters:
- name: petId
  in: path
  description: ID of pet that needs to be updated
  required: true
  type: string
- name: name
  in: formData
  description: Updated name of the pet
  required: false
  type: string
- name: status
  in: formData
  description: Updated status of the pet
  required: false
  type: string
responses:
  '200':
    description: Pet updated.
  '405':
    description: Invalid input
security:
- petstore_auth:
  - write:pets
  - read:pets

4.4.8. ExternalDocument对象

允许引用扩展文档的外部资源。

固定字段
字段名类型描述
descriptionstring目标文档的简短描述。 GFM语法可用于富文本表示。
urlstring必需 目标文档的URL,值必须是URL的格式。
模式对象
字段模式类型描述
^x-Any允许扩展到Swagger架构。 字段名必须以x-开头,例如x-internal-id。 该值可以是"null",一个原始数据类型,一个数组或一个对象。 有关详细信息,请参阅供应商扩展
ExternalDocument对象示例
 
xxxxxxxxxx
{
  "description": "Find more info here",
  "url": "https://swagger.io"
}
 
xxxxxxxxxx
description: Find more info here
url: https://swagger.io

4.4.9. Parameter对象

描述单个操作参数。

唯一参数由namelocation的组合定义。

有五种可能的参数类型。

固定字段
字段名类型描述
namestring必需 参数的名称。参数名称区分大小写
  • 如果in"path"name字段必须对应于Paths Object中的path字段的相关路径段。对于所有其他情况,name对应于in属性使用的参数名称。
instring必需参数的位置。可能的值为"query","header","path","formData"或"body"。
descriptionstring参数的简要说明。这可能包含使用的例子。 GFM语法可用于富文本表示。
requiredboolean确定此参数是否是必需的。如果参数是in: "path",则该属性为必需,其值必须为"true"。否则,可以包含属性,其默认值为"false"。

如果in"body"

字段名称类型描述
schemaSchema Object必需定义用于body参数的类型的模式。

如果in是除"body"之外的任何值:

字段名称类型描述
typestring必需参数的类型。由于参数不在请求体上,所以它只限于简单类型(即不是对象)。值必须是"string""number""integer","boolean","array""file"之一。如果consumes"multipart/form-data"application/x-www-form-urlencoded,则参数必须是in"formData"
格式string前面提到的type的扩展格式。有关详细信息,请参阅数据类型格式
allowEmptyValueboolean设置传递空值参数的能力。这只适用于queryformData参数,并允许您发送一个名称或空值的参数。默认值为"false"。
itemsItems Object如果type为"array",则为必需项描述数组中项目的类型。
collectionFormatstring如果使用类型数组,则确定数组的格式。可能的值是:
  • csv - 逗号分隔值foo,bar
  • ssv - 空格分隔值foo bar
  • tsv - 标签分隔的值foo \t bar
  • pipes - 管道分隔值 foo | bar
  • multi - 对应于多个参数实例,而不是单个实例foo=bar&foo=baz的多个值。这只对参数in"query"或"formData"有效。
默认值为csv
default*声明如果没有提供服务器将使用服务器将使用的参数的值,例如,"count"来控制每页的结果数量可能会默认为100,如果不是由请求中的客户端提供的。 (注意:"默认"对于所需的参数没有意义。)请参见https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-6.2 。与JSON Schema不同,此值必须符合此参数定义的type
maximumnumber请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.2
exclusiveMaximumboolean请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.2
minimumnumber请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.3
exclusiveMinimumboolean请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.3
maxLengthinteger请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.2.1
minLengthinteger请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.2.2
patternstring请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.2.3
maxItemsinteger请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.2
minItemsinteger请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.3
uniqueItemsboolean请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.4
enum[*]请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.5.1
multipleOfnumber请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.1
模式字段
模式字段类型描述
^ x-Any允许扩展到Swagger架构。字段名必须以x-开头,例如x-internal-id。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展
Parameter对象示例
Body参数

具有引用模式定义(通常用于模型定义)的body参数:

 
xxxxxxxxxx
{
  "name": "user",
  "in": "body",
  "description": "user to add to the system",
  "required": true,
  "schema": {
    "$ref": "#/definitions/User"
  }
}
 
xxxxxxxxxx
name: user
in: body
description: user to add to the system
required: true
schema:
  $ref: '#/definitions/User'

一个body参数,它是一个字符串值的数组:

 
xxxxxxxxxx
{
  "name": "user",
  "in": "body",
  "description": "user to add to the system",
  "required": true,
  "schema": {
    "type": "array",
    "items": {
      "type": "string"
    }
  }
}
 
xxxxxxxxxx
name: user
in: body
description: user to add to the system
required: true
schema:
  type: array
  items:
    type: string
其他参数

具有64位整数数组的头参数:

 
xxxxxxxxxx
{
  "name": "token",
  "in": "header",
  "description": "token to be passed as a header",
  "required": true,
  "type": "array",
  "items": {
    "type": "integer",
    "format": "int64"
  },
  "collectionFormat": "csv"
}
 
xxxxxxxxxx
name: token
in: header
description: token to be passed as a header
required: true
type: array
items:
  type: integer
  format: int64
collectionFormat: csv

字符串值的路径参数:

 
xxxxxxxxxx
{
  "name": "username",
  "in": "path",
  "description": "username to fetch",
  "required": true,
  "type": "string"
}
 
xxxxxxxxxx
name: username
in: path
description: username to fetch
required: true
type: string

字符串值的可选查询参数,通过重复查询参数允许多个值:

 
xxxxxxxxxx
{
  "name": "id",
  "in": "query",
  "description": "ID of the object to fetch",
  "required": false,
  "type": "array",
  "items": {
    "type": "string"
  },
  "collectionFormat": "multi"
}
 
xxxxxxxxxx
name: id
in: query
description: ID of the object to fetch
required: false
type: array
items:
  type: string
collectionFormat: multi

文件上传的文件类型的表单数据:

 
xxxxxxxxxx
{
  "name": "avatar",
  "in": "formData",
  "description": "The avatar of the user",
  "required": true,
  "type": "file"
}
 
xxxxxxxxxx
name: avatar
in: formData
description: The avatar of the user
required: true
type: file

4.4.10. Items对象

JSON-Schema项目对象的一个有限子集。它由不在in"body"的参数定义使用。

固定字段
字段名称类型描述
typestring必需。数组的内部类型。值必须是"""",""数字"",""整数""""布尔"""或"数组"之一。不允许使用文件和型号。
格式string前面提到的type的扩展格式。有关详细信息,请参阅数据类型格式
项目Items Object如果type为"array",则为必需描述数组中项目的类型。
collectionFormatstring如果使用类型数组,则确定数组的格式。可能的值是:
  • csv - 逗号分隔值foo,bar
  • ssv - 空格分隔值foo bar
  • tsv - 标签分隔的值foo \t bar
  • pipes - 管道分隔值 foo
default*声明服务器将不使用的项目的值。 (注意:"默认"对于所需项目没有意义。)请参见https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-6.2。与JSON Schema不同,此值必须符合数据类型的定义的`type
maximumnumber请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.2
exclusiveMaximumboolean请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.2
minimumnumber请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.3
exclusiveMinimumboolean请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.3
maxLengthinteger请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.2.1
minLengthinteger请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.2.2
patternstring请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.2.3
maxItemsinteger请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.2
minItemsinteger请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.3
uniqueItemsboolean请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.4
enum[*]请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.5.1
multipleOfnumber请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.1
模式对象
模式图案类型描述
^ x-Any允许扩展到Swagger架构。字段名必须以x-开头,例如x-internal-id。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展
Items对象示例

Item必须是字符串类型,最小长度为2个字符:

 
xxxxxxxxxx
{
    "type": "string",
    "minLength": 2
}
 
xxxxxxxxxx
type: string
minLength: 2

一组数组,内部数组为整数,数字必须介于0到63之间(含):

 
xxxxxxxxxx
{
    "type": "array",
    "items": {
        "type": "integer",
        "minimum": 0,
        "maximum": 63
    }
}
 
xxxxxxxxxx
type: array
items:
  type: integer
  minimum: 0
  maximum: 63

4.4.11. Responses对象

用于操作的预期响应的容器。容器将HTTP响应代码映射到预期响应。文档中预计不会涵盖所有可能的HTTP响应代码,因为它们可能不是事先知道的。但是,从文档中可以看到成功的操作响应和任何已知的错误。

"default"可以用作所有HTTP规范的默认响应对象,这些代码不在规范中。

"Response对象"必须包含至少一个响应代码,它应该是成功操作调用的响应。

固定字段
字段名称类型描述
defaultResponse Object | Reference Object除了针对特定HTTP响应代码声明的响应之外的响应的文档。它可用于覆盖未申报的响应。 Reference对象可用于链接到Swagger对象的Responses部分定义的响应。
模式字段
模式字段类型描述
{HTTP Status Code}Response Object| Reference Object任何HTTP状态代码都可以用作属性名称(每个HTTP状态代码一个属性)。描述该HTTP状态代码的预期响应。 Reference对象可用于链接到Swagger对象的Responses部分定义的响应。
^ x-Any允许扩展到Swagger架构。字段名必须以x-开头,例如x-internal-id。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展
Responses对象示例

对于成功的操作和对他人的默认响应的200个响应(意味着错误):

 
xxxxxxxxxx
{
  "200": {
    "description": "a pet to be returned",
    "schema": {
      "$ref": "#/definitions/Pet"
    }
  },
  "default": {
    "description": "Unexpected error",
    "schema": {
      "$ref": "#/definitions/ErrorModel"
    }
  }
}
 
xxxxxxxxxx
'200':
  description: a pet to be returned
  schema:
    $ref: '#/definitions/Pet'
default:
  description: Unexpected error
  schema:
    $ref: '#/definitions/ErrorModel'

4.4.12. Response对象

描述来自API操作的单个响应。

固定字段
字段名称类型描述
descriptionstring必填简短的回复说明。 GFM语法可用于富文本表示。
schemaSchema Object响应结构的定义。它可以是原始数组,数组或对象。如果此字段不存在,则表示不作为响应的一部分返回任何内容。作为Schema Object的扩展,它的根type值也可以是"file"。这应该伴随着相关的"生产"MimeType。
eadersHeaders Object与响应一起发送的标题列表。
examplesExample Object响应消息的示例。
模式对象
模式字段类型描述
^ x-Any允许扩展到Swagger架构。字段名必须以x-开头,例如x-internal-id。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展
Response对象示例

一个复杂类型的数组的响应:

 
xxxxxxxxxx
{
  "description": "A complex object array response",
  "schema": {
    "type": "array",
    "items": {
      "$ref": "#/definitions/VeryComplexType"
    }
  }
}
 
xxxxxxxxxx
description: A complex object array response
schema:
  type: array
  items:
    $ref: '#/definitions/VeryComplexType'

响应字符串类型:

 
xxxxxxxxxx
{
  "description": "A simple string response",
  "schema": {
    "type": "string"
  }
}
 
xxxxxxxxxx
description: A simple string response
schema:
  type: string

回应头:

 
xxxxxxxxxx
{
  "description": "A simple string response",
  "schema": {
    "type": "string"
  },
  "headers": {
    "X-Rate-Limit-Limit": {
      "description": "The number of allowed requests in the current period",
      "type": "integer"
    },
    "X-Rate-Limit-Remaining": {
      "description": "The number of remaining requests in the current period",
      "type": "integer"
    },
    "X-Rate-Limit-Reset": {
      "description": "The number of seconds left in the current period",
      "type": "integer"
    }
  }
}
 
xxxxxxxxxx
description: A simple string response
schema:
  type: string
headers:
  X-Rate-Limit-Limit:
    description: The number of allowed requests in the current period
    type: integer
  X-Rate-Limit-Remaining:
    description: The number of remaining requests in the current period
    type: integer
  X-Rate-Limit-Reset:
    description: The number of seconds left in the current period
    type: integer

没有返回值的响应:

 
xxxxxxxxxx
{
  "description": "object created"
}
 
xxxxxxxxxx
description: object created

4.4.13. Headers对象

列出可作为响应一部分发送的标题。

模式字段
模式字段类型描述
{name}Header Object属性的名称对应于标题的名称。 该值描述标题的类型。
Headers对象示例

速率限制Headers:

 
xxxxxxxxxx
{
    "X-Rate-Limit-Limit": {
        "description": "The number of allowed requests in the current period",
        "type": "integer"
    },
    "X-Rate-Limit-Remaining": {
        "description": "The number of remaining requests in the current period",
        "type": "integer"
    },
    "X-Rate-Limit-Reset": {
        "description": "The number of seconds left in the current period",
        "type": "integer"
    }
}
 
xxxxxxxxxx
X-Rate-Limit-Limit:
  description: The number of allowed requests in the current period
  type: integer
X-Rate-Limit-Remaining:
  description: The number of remaining requests in the current period
  type: integer
X-Rate-Limit-Reset:
  description: The number of seconds left in the current period
  type: integer

4.4.14. Example对象

允许共享操作响应的示例。

模式字段
模式字段类型描述
{mime type}Any属性的名称必须是操作之一产生值(隐式或继承)。 这个值应该是这样一个响应的样子。
Example对象示例

Pet数据类型的application/json mimetype的示例响应:

 
xxxxxxxxxx
{
  "application/json": {
    "name": "Puma",
    "type": "Dog",
    "color": "Black",
    "gender": "Female",
    "breed": "Mixed"
  }
}
 
xxxxxxxxxx
application/json:
  name: Puma
  type: Dog
  color: Black
  gender: Female
  breed: Mixed

4.4.15. Header对象

字段名称类型描述
descriptionstring标题的简短描述。
typestring必需对象的类型。值必须是"string""number""integer""boolean""array"之一。
格式string前面提到的type的扩展格式。有关详细信息,请参阅数据类型格式
itemsItems Object如果type是"array",则需要描述数组中的项目类型。
collectionFormatstring如果使用类型数组,则确定数组的格式。可能的值是:
  • csv - 逗号分隔值foo,bar
  • ssv - 空格分隔值foo bar
  • tsv - 标签分隔的值foo \t bar
  • pipes - 管道分隔值 foo
default*声明如果没有提供服务器将使用的标头的值。 (注意:"默认"对所需的标题没有意义。)请参见https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-6.2 。与JSON Schema不同,此值必须符合标题的定义的type
maximumnumber请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.2
exclusiveMaximumboolean请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.2
minimumnumber请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.3
exclusiveMinimumboolean请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.3
maxLengthinteger请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.2.1
minLengthinteger请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.2.2
patternstring请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.2.3
maxItemsinteger请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.2
minItemsinteger请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.3
uniqueItemsbooleanhttps://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.4
enum[*]请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.5.1
multipleOfnumber请参阅https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.1
模式对象
模式字段类型描述
^ x-Any允许扩展到Swagger架构。字段名必须以x-开头,例如x-internal-id。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展
Header对象示例

一个整数类型的简单标题:

 
xxxxxxxxxx
{
  "description": "The number of allowed requests in the current period",
  "type": "integer"
}
 
xxxxxxxxxx
description: The number of allowed requests in the current period
type: integer

4.4.16. Tag对象

允许将元数据添加到操作对象使用的单个标签中。在这里使用标签对象不是必须的。

固定字段
字段名称类型描述
namestring必填标签的名称。
descriptionstring标签的简短描述。 GFM语法可用于富文本表示。
externalDocs扩展文档对象此标签的其他外部文档。
模式字段
模式字段类型描述
^ x-Any允许扩展到Swagger架构。字段名必须以x-开头,例如x-internal-id。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展
Tag对象示例
 
xxxxxxxxxx
{
    "name": "pet",
    "description": "Pets operations"
}
 
xxxxxxxxxx
name: pet
description: Pets operations

4.4.17. Reference对象

允许在规范中引用其他定义的简单对象。 它可用于引用在顶层定义的参数和响应以供重用。

引用对象是使用JSON ReferenceJSON Pointer作为其值。 对于本规范,仅支持canonical dereferencing

固定字段
字段名称类型描述
$refstring必填参考字符串。
Reference对象示例
 
xxxxxxxxxx
{
    "$ref": "#/definitions/Pet"
}
 
xxxxxxxxxx
$ref: '#/definitions/Pet'
引用模式文件示例
 
xxxxxxxxxx
{
  "$ref": "Pet.json"
}
 
xxxxxxxxxx
$ref: 'Pet.yaml'
具有嵌入式架构的相对文件示例
 
xxxxxxxxxx
{
  "$ref": "definitions.json#/Pet"
}
 
xxxxxxxxxx
$ref: 'definitions.yaml#/Pet'

4.4.18. Schema对象

模式对象允许定义输入和输出数据类型。 这些类型可以是对象,也可以是基本数据类型和数组。 此对象基于JSON模式规范草案4,并使用其预定义的子集。 在此子集之上,本规范提供了扩展,以允许更完整的文档。

有关属性的更多信息,请参见JSON Schema CoreJSON Schema Validation。 除非另有说明,属性定义遵循此处所引用的JSON模式规范。

以下属性直接从JSON模式定义中获取,并遵循相同的规范:

以下属性取自JSON模式定义,但其定义已调整为Swagger规范。 它们的定义与JSON Schema中的定义相同,只有原始定义引用了JSON Schema定义,才使用Schema Object定义。

除了JSON模式子集字段之外,以下字段可用于进一步的模式文档。

固定字段
字段名称类型描述
discriminatorstring增加了对多态性的支持。鉴别器是用于区分继承此模式的其他模式之间的模式属性名称。必须在此模式中定义所使用的属性名称,并且必须在"必需"属性列表中。使用时,值必须是此模式或继承它的任何模式的名称。
readOnlyboolean仅与模式"属性"定义相关。将该属性声明为"只读"。这意味着它可以作为响应的一部分发送,但不能作为请求的一部分发送。标记为"readOnly"为"true"的属性不应该在定义的模式的"必需"列表中。默认值为"false"。
xmlXML Object这只能用于属性模式。它对根模式没有影响。添加额外的元数据来描述此属性的XML表示格式。
externalDocsExternal Documentation Object此模式的其他外部文档。
exampleAny一个自由格式属性,用于包括此模式的实例示例。
模式对象
模式字段类型描述
^ x-Any允许扩展到Swagger架构。字段名必须以x-开头,例如x-internal-id。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展
组合和继承(多态)

Swagger允许使用JSON Schema的"allOf"属性组合和扩展模型定义,实际上提供模型组合。 allOf接受一个独立的验证的对象定义数组,一起构成一个对象。

虽然组合提供模型可扩展性,但并不意味着模型之间的层次结构。为了支持多态,Swagger增加了discriminator字段的支持。使用时,discriminator将是用于决定使用哪个模式定义验证模型结构的属性的名称。因此,discriminator字段必须是必填字段。所选属性的值必须是在definitions属性下赋予模型的友好名称。因此,不具有给定id的内联模式定义不能用于多态。

XML建模

当将XML定义转换为XML时,xml属性允许额外的定义。 XML对象包含有关可用选项的其他信息。

Schema对象示例
原始示例

与以前版本的Swagger不同,Schema定义也可用于描述原语和数组。

 
xxxxxxxxxx
{
    "type": "string",
    "format": "email"
}
 
xxxxxxxxxx
type: string
format: email
简单模型
 
xxxxxxxxxx
{
  "type": "object",
  "required": [
    "name"
  ],
  "properties": {
    "name": {
      "type": "string"
    },
    "address": {
      "$ref": "#/definitions/Address"
    },
    "age": {
      "type": "integer",
      "format": "int32",
      "minimum": 0
    }
  }
}
 
xxxxxxxxxx
type: object
required:
- name
properties:
  name:
    type: string
  address:
    $ref: '#/definitions/Address'
  age:
    type: integer
    format: int32
    minimum: 0
包含Map/Dictionary属性的模型

For a simple string to string mapping:

 
xxxxxxxxxx
{
  "type": "object",
  "additionalProperties": {
    "type": "string"
  }
}
 
xxxxxxxxxx
type: object
additionalProperties:
  type: string

对于要映射的字符串:

 
xxxxxxxxxx
{
  "type": "object",
  "additionalProperties": {
    "$ref": "#/definitions/ComplexModel"
  }
}
 
xxxxxxxxxx
type: object
additionalProperties:
  $ref: '#/definitions/ComplexModel'
Model示例
 
xxxxxxxxxx
{
  "type": "object",
  "properties": {
    "id": {
      "type": "integer",
      "format": "int64"
    },
    "name": {
      "type": "string"
    }
  },
  "required": [
    "name"
  ],
  "example": {
    "name": "Puma",
    "id": 1
  }
}
 
xxxxxxxxxx
type: object
properties:
  id:
    type: integer
    format: int64
  name:
    type: string
required:
- name
example:
  name: Puma
  id: 1
组合的模型
 
xxxxxxxxxx
{
  "definitions": {
    "ErrorModel": {
      "type": "object",
      "required": [
        "message",
        "code"
      ],
      "properties": {
        "message": {
          "type": "string"
        },
        "code": {
          "type": "integer",
          "minimum": 100,
          "maximum": 600
        }
      }
    },
    "ExtendedErrorModel": {
      "allOf": [
        {
          "$ref": "#/definitions/ErrorModel"
        },
        {
          "type": "object",
          "required": [
            "rootCause"
          ],
          "properties": {
            "rootCause": {
              "type": "string"
            }
          }
        }
      ]
    }
  }
}
 
xxxxxxxxxx
definitions:
  ErrorModel:
    type: object
    required:
    - message
    - code
    properties:
      message:
        type: string
      code:
        type: integer
        minimum: 100
        maximum: 600
  ExtendedErrorModel:
    allOf:
    - $ref: '#/definitions/ErrorModel'
    - type: object
      required:
      - rootCause
      properties:
        rootCause:
          type: string
多态模型支持
 
xxxxxxxxxx
{
  "definitions": {
    "Pet": {
      "type": "object",
      "discriminator": "petType",
      "properties": {
        "name": {
          "type": "string"
        },
        "petType": {
          "type": "string"
        }
      },
      "required": [
        "name",
        "petType"
      ]
    },
    "Cat": {
      "description": "A representation of a cat",
      "allOf": [
        {
          "$ref": "#/definitions/Pet"
        },
        {
          "type": "object",
          "properties": {
            "huntingSkill": {
              "type": "string",
              "description": "The measured skill for hunting",
              "default": "lazy",
              "enum": [
                "clueless",
                "lazy",
                "adventurous",
                "aggressive"
              ]
            }
          },
          "required": [
            "huntingSkill"
          ]
        }
      ]
    },
    "Dog": {
      "description": "A representation of a dog",
      "allOf": [
        {
          "$ref": "#/definitions/Pet"
        },
        {
          "type": "object",
          "properties": {
            "packSize": {
              "type": "integer",
              "format": "int32",
              "description": "the size of the pack the dog is from",
              "default": 0,
              "minimum": 0
            }
          },
          "required": [
            "packSize"
          ]
        }
      ]
    }
  }
}
 
xxxxxxxxxx
definitions:
  Pet:
    type: object
    discriminator: petType
    properties:
      name:
        type: string
      petType:
        type: string
    required:
    - name
    - petType
  Cat:
    description: A representation of a cat
    allOf:
    - $ref: '#/definitions/Pet'
    - type: object
      properties:
        huntingSkill:
          type: string
          description: The measured skill for hunting
          default: lazy
          enum:
          - clueless
          - lazy
          - adventurous
          - aggressive
      required:
      - huntingSkill
  Dog:
    description: A representation of a dog
    allOf:
    - $ref: '#/definitions/Pet'
    - type: object
      properties:
        packSize:
          type: integer
          format: int32
          description: the size of the pack the dog is from
          default: 0
          minimum: 0
      required:
      - packSize

4.4.19. XML 对象

允许更精细的XML模型定义的元数据对象。

使用数组时,XML元素名称不会被推断(用于单数/复数形式),而'name`属性应用于添加该信息。查看预期行为的例子。

固定字段
字段名称类型描述
namestring替换用于描述的schema属性的元素/属性的名称。在Items对象(items)中定义时,它将影响列表中各个XML元素的名称。当type定义为array(items之外)时,它会影响包装元素,只有当wrappedtrue时。如果wrappedfalse,那将被忽略。
命名空间string命名空间定义的URL。值应以URL的形式。
前缀string用于name的前缀。
属性boolean声明属性定义是否转换为属性而不是元素。默认值为"false"。
wrapboolean可以仅用于数组定义。表示数组是否被包装(例如,<books> <book/> <book/> </books>)或展开(<book/> <book/>)。默认值为"false"。定义只有当type定义为array(items之外)时才会生效。
模式对象
模式字段类型描述
^ x-Any允许扩展到Swagger架构。字段名必须以x-开头,例如x-internal-id。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展
XML对象示例

XML对象定义的示例包含在Schema Object的属性定义中,并具有XML表示形式的示例。

非XML元素

基本字符串属性:

 
xxxxxxxxxx
{
    "animals": {
        "type": "string"
    }
}
 
xxxxxxxxxx
animals:
  type: string
 
xxxxxxxxxx
<animals>...</animals>

基本字符串数组属性(wrapped默认为false):

 
xxxxxxxxxx
{
    "animals": {
        "type": "array",
        "items": {
            "type": "string"
        }
    }
}
 
xxxxxxxxxx
animals:
  type: array
  items:
    type: string
 
xxxxxxxxxx
<animals>...</animals>
<animals>...</animals>
<animals>...</animals>
XML名称替换
 
xxxxxxxxxx
{
  "animals": {
    "type": "string",
    "xml": {
      "name": "animal"
    }
  }
}
 
xxxxxxxxxx
animals:
  type: string
  xml:
    name: animal
 
xxxxxxxxxx
<animal>...</animal>
XML属性、前缀和命名空间

在此示例中,显示了完整的模型定义。

 
xxxxxxxxxx
{
  "Person": {
    "type": "object",
    "properties": {
      "id": {
        "type": "integer",
        "format": "int32",
        "xml": {
          "attribute": true
        }
      },
      "name": {
        "type": "string",
        "xml": {
          "namespace": "http://swagger.io/schema/sample",
          "prefix": "sample"
        }
      }
    }
  }
}
 
xxxxxxxxxx
Person:
  type: object
  properties:
    id:
      type: integer
      format: int32
      xml:
        attribute: true
    name:
      type: string
      xml:
        namespace: http://swagger.io/schema/sample
        prefix: sample
 
xxxxxxxxxx
<Person id="123">
    <sample:name xmlns:sample="http://swagger.io/schema/sample">example</sample:name>
</Person>
XML 数组

更改元素名称:

 
xxxxxxxxxx
{
  "animals": {
    "type": "array",
    "items": {
      "type": "string",
      "xml": {
        "name": "animal"
      }
    }
  }
}
 
xxxxxxxxxx
animals:
  type: array
  items:
    type: string
    xml:
      name: animal
 
xxxxxxxxxx
<animal>value</animal>
<animal>value</animal>

外部的 name 属性对XML没有影响:

 
xxxxxxxxxx
{
  "animals": {
    "type": "array",
    "items": {
      "type": "string",
      "xml": {
        "name": "animal"
      }
    },
    "xml": {
      "name": "aliens"
    }
  }
}
 
xxxxxxxxxx
animals:
  type: array
  items:
    type: string
    xml:
      name: animal
  xml:
    name: aliens
 
xxxxxxxxxx
<animal>value</animal>
<animal>value</animal>

即使数组被包装,如果没有明确定义名称,内部和外部将使用相同的名称:

 
xxxxxxxxxx
{
  "animals": {
    "type": "array",
    "items": {
      "type": "string"
    },
    "xml": {
      "wrapped": true
    }
  }
}
 
xxxxxxxxxx
animals:
  type: array
  items:
    type: string
  xml:
    wrapped: true
 
xxxxxxxxxx
<animals>
  <animals>value</animals>
  <animals>value</animals>
</animals>

为了克服上述示例,可以使用以下定义:

 
xxxxxxxxxx
{
  "animals": {
    "type": "array",
    "items": {
      "type": "string",
      "xml": {
        "name": "animal"
      }
    },
    "xml": {
      "wrapped": true
    }
  }
}
 
xxxxxxxxxx
animals:
  type: array
  items:
    type: string
    xml:
      name: animal
  xml:
    wrapped: true
 
xxxxxxxxxx
<animals>
  <animal>value</animal>
  <animal>value</animal>
</animals>

影响内部和外部名称:

 
xxxxxxxxxx
{
  "animals": {
    "type": "array",
    "items": {
      "type": "string",
      "xml": {
        "name": "animal"
      }
    },
    "xml": {
      "name": "aliens",
      "wrapped": true
    }
  }
}
 
xxxxxxxxxx
animals:
  type: array
  items:
    type: string
    xml:
      name: animal
  xml:
    name: aliens
    wrapped: true
 
xxxxxxxxxx
<aliens>
  <animal>value</animal>
  <animal>value</animal>
</aliens>

如果我们改变外部元素而不是内部元素:

 
xxxxxxxxxx
{
  "animals": {
    "type": "array",
    "items": {
      "type": "string"
    },
    "xml": {
      "name": "aliens",
      "wrapped": true
    }
  }
}
 
xxxxxxxxxx
animals:
  type: array
  items:
    type: string
  xml:
    name: aliens
    wrapped: true
 
xxxxxxxxxx
<aliens>
  <aliens>value</aliens>
  <aliens>value</aliens>
</aliens>

4.4.20. Definitions对象

用于保存可由操作消耗和生成的数据类型。 这些数据类型可以是基本数据类型,数组或模型。

模式字段
模式字段类型描述
{name}Schema Object单一定义,将"名称"映射到其定义的模式。
Definitions对象示例
 
xxxxxxxxxx
{
  "Category": {
    "type": "object",
    "properties": {
      "id": {
        "type": "integer",
        "format": "int64"
      },
      "name": {
        "type": "string"
      }
    }
  },
  "Tag": {
    "type": "object",
    "properties": {
      "id": {
        "type": "integer",
        "format": "int64"
      },
      "name": {
        "type": "string"
      }
    }
  }
}
 
xxxxxxxxxx
Category:
  type: object
  properties:
    id:
      type: integer
      format: int64
    name:
      type: string
Tag:
  type: object
  properties:
    id:
      type: integer
      format: int64
    name:
      type: string

4.4.21. Parameters Definitions对象

用于保存跨操作重用的参数。 参数定义可以参考这里定义的参数。

定义全局操作参数。

模式字段
模式字段类型描述
{name}Parameter Object单个参数定义,将"名称"映射到其定义的参数。
Parameters Definitions义对象示例
 
xxxxxxxxxx
{
  "skipParam": {
    "name": "skip",
    "in": "query",
    "description": "number of items to skip",
    "required": true,
    "type": "integer",
    "format": "int32"
  },
  "limitParam": {
    "name": "limit",
    "in": "query",
    "description": "max records to return",
    "required": true,
    "type": "integer",
    "format": "int32"
  }
}
 
xxxxxxxxxx
skipParam:
  name: skip
  in: query
  description: number of items to skip
  required: true
  type: integer
  format: int32
limitParam:
  name: limit
  in: query
  description: max records to return
  required: true
  type: integer
  format: int32

4.4.22. Responses Definitions对象

一个对象要保持响应在操作之间重复使用。 响应定义可以参考这里定义的定义。

定义全局操作响应。

模式字段
模式字段类型描述
{name}Response Object单个响应定义,将"名称"映射到其定义的响应。
Responses Definitions对象示例
 
xxxxxxxxxx
{
  "NotFound": {
    "description": "Entity not found."
  },
  "IllegalInput": {
    "description": "Illegal input for operation."
  },
  "GeneralError": {
    "description": "General Error",
    "schema": {
        "$ref": "#/definitions/GeneralError"
    }
  }
}
 
xxxxxxxxxx
NotFound:
  description: Entity not found.
IllegalInput:
  description: Illegal input for operation.
GeneralError:
  description: General Error
  schema:
    $ref: '#/definitions/GeneralError'

4.4.23. SecurityDefinitions对象

说明书中可用的安全方案的声明。 这不执行操作的安全性方案,仅用于为每个方案提供相关详细信息。

模式字段
模式字段类型描述
{name}Security Scheme Object单个安全方案定义,将"名称"映射到其定义的方案。
SecurityDefinitions对象示例
 
xxxxxxxxxx
{
  "api_key": {
    "type": "apiKey",
    "name": "api_key",
    "in": "header"
  },
  "petstore_auth": {
    "type": "oauth2",
    "authorizationUrl": "http://swagger.io/api/oauth/dialog",
    "flow": "implicit",
    "scopes": {
      "write:pets": "modify pets in your account",
      "read:pets": "read your pets"
    }
  }
}
 
xxxxxxxxxx
api_key:
  type: apiKey
  name: api_key
  in: header
petstore_auth:
  type: oauth2
  authorizationUrl: http://swagger.io/api/oauth/dialog
  flow: implicit
  scopes:
    write:pets: modify pets in your account
    read:pets: read your pets

4.4.24. Security Scheme对象

允许定义可以由操作使用的安全性方案。支持的方案是基本认证,API密钥(作为头或查询参数)和OAuth2的公共流(隐式,密码,应用和访问代码)。

固定字段
字段名称类型有效性描述
typestringAny必需安全方案的类型。有效值为"basic","apiKey"或"oauth2"。
descriptionstringAny安全方案的简短描述。
namestringapiKey必需要使用的标题或查询参数的名称。
instringapiKey必需 API密钥的位置。有效值为"query""header"
flowstringoauth2必需 ** OAuth2安全方案使用的流程。有效值为 "implicit", "password", "application" or "accessCode"
authorizationUrlstringoauth2("implicit""accessCode")必需 **用于此流的授权URL。这应该是一个URL的形式。
tokenUrlstringoauth2("password""application""accessCode")必需 **用于此流的令牌URL。这应该是一个URL的形式。
scopesScopes Objectoauth2必需。 OAuth2安全方案的可用范围。
模式字段
字段名称类型描述
^ x-Any允许扩展到Swagger架构。字段名必须以x-开头,例如x-internal-id。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展
Security Scheme对象示例
基本认证示例
 
xxxxxxxxxx
{
  "type": "basic"
}
 
xxxxxxxxxx
type: basic
API Key 示例
 
xxxxxxxxxx
{
  "type": "apiKey",
  "name": "api_key",
  "in": "header"
}
 
xxxxxxxxxx
type: apiKey
name: api_key
in: header
隐含的 OAuth2 示例
 
xxxxxxxxxx
{
  "type": "oauth2",
  "authorizationUrl": "http://swagger.io/api/oauth/dialog",
  "flow": "implicit",
  "scopes": {
    "write:pets": "modify pets in your account",
    "read:pets": "read your pets"
  }
}
 
xxxxxxxxxx
type: oauth2
authorizationUrl: http://swagger.io/api/oauth/dialog
flow: implicit
scopes:
  write:pets: modify pets in your account
  read:pets: read your pets

4.4.25. Scopes 对象

列出OAuth2安全方案的可用范围。

模式字段
模式字段类型描述
{name}string将一个范围的名称映射到其简短描述(作为属性的值)。
模式对象
模式字段类型描述
^ x-Any允许扩展到Swagger架构。 字段名必须以x-开头,例如x-internal-id。 该值可以是"null",一个原始数据类型,一个数组或一个对象。 有关详细信息,请参阅供应商扩展
Scopes 对象示例
 
xxxxxxxxxx
{
  "write:pets": "modify pets in your account",
  "read:pets": "read your pets"
}
 
xxxxxxxxxx
write:pets: modify pets in your account
read:pets: read your pets

4.4.26. 安全需求对象

列出执行此操作所需的安全方案。 对象可以具有在其中声明的多个安全性方案,这些安全性方案都是必需的(即,方案之间存在逻辑和)。

用于每个属性的名称必须对应于安全定义中声明的安全性方案。

模式字段
模式字段类型描述
{name}[string]每个名称必须对应于在安全定义中声明的安全性方案。 如果安全方案的类型为"oauth2"`,那么该值就是执行所需的范围名称列表。 对于其他安全方案类型,数组必须为空。
Security Requirement对象示例
非OAuth2安全要求
 
xxxxxxxxxx
{
  "api_key": []
}
 
xxxxxxxxxx
api_key: []
OAuth2 安全需要
 
xxxxxxxxxx
{
  "petstore_auth": [
    "write:pets",
    "read:pets"
  ]
}
 
xxxxxxxxxx
petstore_auth:
- write:pets
- read:pets

4.5. 规范扩展

虽然Swagger规范尝试适应大多数用例,但可以添加附加数据以在某些点扩展规范。

扩展属性始终以"x-"为前缀,并且可以具有任何有效的JSON格式值。

可用或可能不支持扩展可用的工具,但也可以扩展扩展,以添加请求的支持(如果工具是内部的或开源的)。

4.6. 安全筛选

Swagger规范中的某些对象可能被声明并保持为空,或者完全被删除,即便它们本质上是API文档的核心。

背景原因是允许对文档本身进行额外的访问控制。虽然不是规范本身的一部分,但某些库可能会选择允许基于某种形式的身份验证/授权访问文档的部分内容。

两个例子:

  1. Paths Object可能为空。看起来违反直觉,但可以指引查阅者到达正确的位置。虽然查阅者可能无法看到一些文档,但他们仍然可以访问到InfoObject,其中可能包含有关身份验证的其他信息。
  2. Path Item Object可能为空。在这种情况下,查阅者会意识到该路径存在,但将无法看到其任何操作或参数。这不同于从Paths Object隐藏路径本身,所以用户不会意识到它的存在。这允许文档提供者更好地控制查阅者可以看到的内容。