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.
Swagger™ 是用于描述和记录RESTful API的项目。
Swagger规范定义了一组描述这样的API所需的文件。 Swagger-UI项目可以使用这些文件来显示API和Swagger-Codegen来生成各种语言的客户端。 附加工具程序还可以使用生成的文件,如测试工具。
版本 | 日期 | 事件 |
---|---|---|
2.0 | 2014-09-08 | 发布Swagger 2.0 |
1.2 | 2014-03-14 | 发布正式文件 |
1.1 | 2012-08-22 | 发布 Swagger 1.1 |
1.0 | 2011-08-10 | 首次发布Swagger规范 |
Path Templating
路径模板是指使用花括号({})将URL路径的一部分标记为可替换的路径参数。
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
HTTP Status Codes HTTP状态码用于指示执行操作的状态。 可用状态代码由 RFC 7231 和 IANA Status Code Registry构成.
根据Swagger规范描述RESTful API的文件被表示为JSON对象,并符合JSON标准。 作为JSON的超集,YAML也可以被使用 代表Swagger规范文件。
例如,如果一个字段被称为具有数组值,则将使用JSON数组表示形式:
xxxxxxxxxx
{
"field" : [ ]
}
虽然API是使用JSON描述的,但它并没有将JSON输入/输出强加给API本身。
规范中的所有字段名称均为区分大小写。
Swagger的Schema暴露两种类型的字段。 具有声明名称的固定字段和模式字段。它们均声明字段名称的正则表达式模式。 只要每个都有唯一的名称,模式化的字段可以有多个出现。
API的Swagger表示由单个文件组成。 但是,部分定义可以分为单独的文件,由用户自行决定。 这适用于规范中的$ref
字段,如下所示JSON Schema .
按照惯例,Swagger规范文件名为swagger.json
。
Swagger规范中的原始数据类型基于所支持的类型 JSON-Schema Draft 4. 使用Schema Object描述模型,该模式是JSON模式草案4的一个子集。
Parameter Object和Response Object使用一个额外的原始数据类型"file"
将参数类型或响应设置为文件。
基本数据类型有一个可选的修饰符属性format
。 Swagger使用几种已知格式更精确地定义正在使用的数据类型。 "format"属性是一个开放的"string"值属性,可以有任何值来支持文档需求。 即使这些规范未定义,仍然可以使用"email","uuid"等来描述格式。 不包含"format"属性的类型遵循JSON模式的定义(除了上面定义的"file"类型)。 Swagger规范定义的格式如下:
通用名称 | type | format | 注释 |
---|---|---|---|
integer | integer | int32 | 有符号 32 bits |
long | integer | int64 | 有符号 64 bits |
float | number | float | |
double | number | double | |
string | string | ||
byte | string | byte | base64 编码字符 |
binary | string | binary | 任何八进制序列 |
boolean | boolean | ||
date | string | date | 参考规范的 full-date - RFC3339 |
dateTime | string | date-time | 参考规范的 date-time - RFC3339 |
password | string | password | 用于提示输入需要被遮罩 |
这里开始介绍API规范的根文档对象。 它将以前的资源列表和API声明(版本1.2及更早版本)组合在一起成为一个文档。
字段名 | 类型 | 描述 |
---|---|---|
swagger | string | 必须的. 指定正在使用的Swagger Specification版本。 Swagger UI和其他客户端可以使用它来解释API列表。 值必须为"2.0"`。 |
info | Info Object | 必须的. 提供有关API的元数据。 根据需要客户端可能会使用该元数据。 |
host | string | 主机(名称或ip)服务于API。 这必须是主机,不包括传输协议或子路径。 它可以包括一个端口。 如果不包含"主机",则将使用提供文档的主机(包括端口)。 host 不支持 path templating. |
basePath | string | 提供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. |
paths | Paths Object | 必须的. API的可用路径和操作。 |
definitions | Definitions Object | 定义对象,用于保存操作生成和使用的数据类型。 |
parameters | Parameters Definitions Object | 跨操作使用的参数的对象。 此属性不为所有操作定义全局参数。 |
responses | Responses Definitions Object | 用来保存可以跨操作使用的响应。 此属性不定义所有操作的全局响应。 |
securityDefinitions | Security Definitions Object | 可以在规范中使用的安全方案定义。 |
security | [Security Requirement Object] | 声明对整个API适用哪些安全方案。 值列表描述了可以使用的替代安全方案(即安全性要求之间是逻辑OR的关系)。 单个的操作可以覆盖此定义。 |
tags | [Tag Object] | 规范使用的附加元数据的标签列表。 标签的顺序可以用于通过解析工具反映其顺序。 并不是所有的Operation Object使用的标签都必须声明。 未声明的标签可以随机组织或基于工具的逻辑进行组织。 列表中的每个标签名称必须是唯一的。 |
externalDocs | External Documentation Object | 其他外部文档。 |
字段模式 | 类型 | 描述 |
---|---|---|
^x- | Any | 允许扩展到Swagger架构。 字段名必须以x- 开头,例如x-internal-id 。 该值可以是"null",一个原始数据类型,一个数组或一个对象。 有关详细信息,请参阅供应商扩展。 |
该对象提供有关API的元数据。 客户端可以根据需要使用元数据,例如便于在Swagger-UI中显示的元数据。
字段名 | 类型 | 描述 |
---|---|---|
title | string | 必须的. 应用程序的标题. |
description | string | 应用程序的短描述. GFM syntax 可用于富文本表示。 |
termsOfService | string | API的服务条款. |
contact | Contact Object | 暴露的API的联系信息。 |
license | License Object | 暴露的API的许可证信息。 |
version | string | 必须的. 提供应用程序API的版本(不要与规范版本混淆)。 |
模式字段 | 类型 | 描述 |
---|---|---|
^ x- | Any | 允许扩展到Swagger架构。 字段名必须以x- 开头,例如x-internal-id 。 该值可以是"null",一个原始数据类型,一个数组或一个对象。 有关详细信息,请参阅供应商扩展。 |
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
version1.0.1
暴露的API的联系信息。
字段名称 | 类型 | 描述 |
---|---|---|
name | string | 联系人/组织的识别名称。 |
url | string | 指向联系人信息的URL。 必须是URL的格式。 |
string | 联系人/组织的电子邮件地址。 必须采用电子邮件地址格式。 |
模式字段 | 类型 | 描述 |
---|---|---|
^ x- | Any | 允许扩展到Swagger架构。 字段名必须以x- 开头,例如x-internal-id 。 该值可以是"null",一个原始数据类型,一个数组或一个对象。 有关详细信息,请参阅供应商扩展。 |
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
暴露的API的许可信息。
字段名称 | 类型 | 描述 |
---|---|---|
name | string | 必需的. 用于API的许可证名称。 |
url | string | 用于API的许可证的URL。 必须是URL的格式。 |
字段模式 | 类型 | 描述 |
---|---|---|
^ x- | Any | 允许扩展到Swagger架构。 字段名必须以x- 开头,例如x-internal-id 。 该值可以是"null",一个原始数据类型,一个数组或一个对象。 有关详细信息,请参阅供应商扩展。 |
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
描述各个端点的相对路径。 该路径附加到basePath
,以构造完整的URL。
由于ACL约束,路径可能为空。
字段模式 | 类型 | 描述 |
---|---|---|
/{path} | Path Item Object | 单个端点的相对路径。 字段名必须以斜杠开头。 该路径附加到basePath ,以构造完整的URL。 路径模板是允许的。 |
^ x- | Any | 允许扩展到Swagger架构。 字段名必须以x- 开头,例如x-internal-id 。 该值可以是"null",一个原始数据类型,一个数组或一个对象。 有关详细信息,请参阅供应商扩展。 |
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'
描述单个路径上可用的操作。 由于ACL约束,路径项可能为空。 路径本身仍然暴露在文档查看器中,但是它们不知道哪些操作和参数可用。
字段名称 | 类型 | 描述 |
---|---|---|
$ref | string | 允许此路径项的外部定义。引用的结构必须是Path Item Object的格式。如果引用的定义与此Path Item的定义之间存在冲突,则行为为undefined。 |
get | Operation Object | 此路径上的GET操作的定义。 |
put | Operation Object | 此路径上的PUT操作的定义。 |
post | Operation Object | 这个路径上POST操作的定义。 |
delete | Operation Object | 这个路径上DELETE操作的定义。 |
options | Operation Object | 此路径上OPTIONS操作的定义。 |
head | Operation Object | 这个路径上的HEAD操作的定义。 |
patch | Operation Object | PATCH操作在此路径上的定义。 |
parameter | [Parameter Object| Reference Object] | 适用于此路径下描述的所有操作的参数列表。这些参数可以在操作级别覆盖,但不能在那里删除。列表不得包含重复的参数。唯一参数由name和location的组合定义。该列表可以使用Reference对象链接到Swagger对象的参数中定义的参数。最多可以有一个"body"参数。 |
模式字段 | 类型 | 描述 |
---|---|---|
^ x- | Any | 允许扩展到Swagger架构。字段名必须以x- 开头,例如x-internal-id 。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展。 |
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
requiredtrue
type array
items
type string
collectionFormat csv
描述路径上的单个API操作。
字段名称 | 类型 | 描述 |
---|---|---|
tags | [string ] | API文档控件的标签列表。标签可以用于资源或任何其他限定符的逻辑分组。 |
summary | string | 简要介绍。为了在swagger-ui中获得最大的可读性,该字段应该小于120个字符。 |
description | string | 操作行为的详细说明。 GFM语法可用于富文本表示。 |
externalDocs | External Documentation Object | 此操作的其他外部文档。 |
operationId | string | 用于识别操作的唯一字符串。在API中描述的所有操作中,ID必须是唯一的。工具和库可以使用operationId来唯一标识操作,因此,建议遵循常用的编程命名约定。 |
consumes | [string ] | 操作可以使用的MimeType列表。这覆盖了Swagger对象的consumes 定义。可以使用空值来清除全局定义。值必须按照MimeType中所述。 |
produces | [string ] | 操作可以生成的MimeType列表。这覆盖了Swagger对象的produces 定义。可以使用空值来清除全局定义。值必须按照MimeType中所述。 |
parameters | Parameter对象 或 Reference对象 | 适用于此操作的参数列表。如果已经在Path Item上定义了一个参数,新定义将覆盖它,但不能将其删除。列表不得包含重复的参数。唯一参数由name和location的组合定义。该列表可以使用Reference对象链接到Swagger对象的参数中定义的参数。最多可以有一个"body"参数。 |
responses | Response对象 | 必需从执行此操作返回可能的响应的列表。 |
scheme | [string ] | 操作的传输协议。值必须来自列表:"http" ,"https" ,"ws" ,"wss" 。该值覆盖Swagger对象schemes 定义。 |
deprecated | boolean | 声明此操作已被弃用。应该禁止使用声明的操作。默认值为"false"。 |
security | [SecurityRequirement对象] | 声明应用哪种安全方案进行此操作。值列表描述了可以使用的替代安全方案(即安全性要求之间存在逻辑OR)。此定义覆盖任何已声明的顶级security 。要删除顶级安全声明,可以使用空数组。 |
模式字段 | 类型 | 描述 |
---|---|---|
^x- | Any | 允许扩展到Swagger架构。 字段名必须以x- 开头,例如x-internal-id 。 该值可以是"null",一个原始数据类型,一个数组或一个对象。 有关详细信息,请参阅供应商扩展。 |
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
requiredtrue
type string
name name
in formData
description Updated name of the pet
requiredfalse
type string
name status
in formData
description Updated status of the pet
requiredfalse
type string
responses
'200'
description Pet updated.
'405'
description Invalid input
security
petstore_auth
write:pets
read:pets
允许引用扩展文档的外部资源。
字段名 | 类型 | 描述 |
---|---|---|
description | string | 目标文档的简短描述。 GFM语法可用于富文本表示。 |
url | string | 必需 目标文档的URL,值必须是URL的格式。 |
字段模式 | 类型 | 描述 |
---|---|---|
^x- | Any | 允许扩展到Swagger架构。 字段名必须以x- 开头,例如x-internal-id 。 该值可以是"null",一个原始数据类型,一个数组或一个对象。 有关详细信息,请参阅供应商扩展。 |
xxxxxxxxxx
{
"description": "Find more info here",
"url": "https://swagger.io"
}
xxxxxxxxxx
description Find more info here
url https //swagger.io
描述单个操作参数。
有五种可能的参数类型。
Path - 与路径模板一起使用,其中参数值实际上是操作URL的一部分。这不包括API的主机或基本路径。例如,在/items/{itemId}
中,path参数是itemId
。
Query - 附加到URL的参数。例如,在/items?id=###
中,查询参数为id
。
Head - 期望作为请求一部分的自定义标头。
Body - 附加到HTTP请求的有效内容。由于只能有一个有效载荷,只能有一个参数。 body参数的名称对参数本身没有影响,仅用于文档目的。由于Form参数也在有效载荷中,因此,body和form参数不能一起存在于相同的操作中。
Form - 用于描述HTTP请求的有效负载,当application/x-www-form-urlencoded
,multipart/form-data
或这两者用作请求的内容类型(在Swagger的定义中, "operation"属性)。这是可以用于发送文件的唯一参数类型,因此支持file
类型。由于表单参数是在有效载荷中发送的,所以它们不能与同一操作的body参数一起声明。表格参数根据所使用的内容类型具有不同的格式(有关详细信息,请参阅http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4):
application/x-www-form-urlencoded
- 与Query参数的格式相似,但作为一个有效载荷。例如,foo=1&bar=swagger
- foo
和bar
都是表单参数。这通常用于正在传输的简单参数。multipart/form-data
- 每个参数都在一个带有内部头部的负载中占用一个部分。例如,对于标题"Content-Disposition:form-data; name ="submit-name"
参数的名称是submit-name
。这种类型的表单参数更常用于文件传输。字段名 | 类型 | 描述 |
---|---|---|
name | string | 必需 参数的名称。参数名称区分大小写。
|
in | string | 必需参数的位置。可能的值为"query","header","path","formData"或"body"。 |
description | string | 参数的简要说明。这可能包含使用的例子。 GFM语法可用于富文本表示。 |
required | boolean | 确定此参数是否是必需的。如果参数是in : "path",则该属性为必需,其值必须为"true"。否则,可以包含属性,其默认值为"false"。 |
如果in
为"body"
:
字段名称 | 类型 | 描述 |
---|---|---|
schema | Schema Object | 必需定义用于body参数的类型的模式。 |
如果in
是除"body"
之外的任何值:
模式字段 | 类型 | 描述 |
---|---|---|
^ x- | Any | 允许扩展到Swagger架构。字段名必须以x- 开头,例如x-internal-id 。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展。 |
具有引用模式定义(通常用于模型定义)的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
requiredtrue
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
requiredtrue
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
requiredtrue
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
requiredtrue
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
requiredfalse
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
requiredtrue
type file
JSON-Schema项目对象的一个有限子集。它由不在in
"body"
的参数定义使用。
模式图案 | 类型 | 描述 |
---|---|---|
^ x- | Any | 允许扩展到Swagger架构。字段名必须以x- 开头,例如x-internal-id 。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展。 |
Item必须是字符串类型,最小长度为2个字符:
xxxxxxxxxx
{
"type": "string",
"minLength": 2
}
xxxxxxxxxx
type string
minLength2
一组数组,内部数组为整数,数字必须介于0到63之间(含):
xxxxxxxxxx
{
"type": "array",
"items": {
"type": "integer",
"minimum": 0,
"maximum": 63
}
}
xxxxxxxxxx
type array
items
type integer
minimum0
maximum63
用于操作的预期响应的容器。容器将HTTP响应代码映射到预期响应。文档中预计不会涵盖所有可能的HTTP响应代码,因为它们可能不是事先知道的。但是,从文档中可以看到成功的操作响应和任何已知的错误。
"default"可以用作所有HTTP规范的默认响应对象,这些代码不在规范中。
"Response对象"必须包含至少一个响应代码,它应该是成功操作调用的响应。
字段名称 | 类型 | 描述 |
---|---|---|
default | Response 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",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展。 |
对于成功的操作和对他人的默认响应的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'
描述来自API操作的单个响应。
字段名称 | 类型 | 描述 |
---|---|---|
description | string | 必填简短的回复说明。 GFM语法可用于富文本表示。 |
schema | Schema Object | 响应结构的定义。它可以是原始数组,数组或对象。如果此字段不存在,则表示不作为响应的一部分返回任何内容。作为Schema Object的扩展,它的根type 值也可以是"file"。这应该伴随着相关的"生产"MimeType。 |
eaders | Headers Object | 与响应一起发送的标题列表。 |
examples | Example Object | 响应消息的示例。 |
模式字段 | 类型 | 描述 |
---|---|---|
^ x- | Any | 允许扩展到Swagger架构。字段名必须以x- 开头,例如x-internal-id 。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展。 |
一个复杂类型的数组的响应:
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
列出可作为响应一部分发送的标题。
模式字段 | 类型 | 描述 |
---|---|---|
{name} | Header Object | 属性的名称对应于标题的名称。 该值描述标题的类型。 |
速率限制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
允许共享操作响应的示例。
模式字段 | 类型 | 描述 |
---|---|---|
{mime type} | Any | 属性的名称必须是操作之一产生值(隐式或继承)。 这个值应该是这样一个响应的样子。 |
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
模式字段 | 类型 | 描述 |
---|---|---|
^ x- | Any | 允许扩展到Swagger架构。字段名必须以x- 开头,例如x-internal-id 。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展。 |
一个整数类型的简单标题:
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
允许将元数据添加到操作对象使用的单个标签中。在这里使用标签对象不是必须的。
字段名称 | 类型 | 描述 |
---|---|---|
name | string | 必填标签的名称。 |
description | string | 标签的简短描述。 GFM语法可用于富文本表示。 |
externalDocs | 扩展文档对象 | 此标签的其他外部文档。 |
模式字段 | 类型 | 描述 |
---|---|---|
^ x- | Any | 允许扩展到Swagger架构。字段名必须以x- 开头,例如x-internal-id 。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展。 |
xxxxxxxxxx
{
"name": "pet",
"description": "Pets operations"
}
xxxxxxxxxx
name pet
description Pets operations
允许在规范中引用其他定义的简单对象。 它可用于引用在顶层定义的参数和响应以供重用。
引用对象是使用JSON Reference的JSON Pointer作为其值。 对于本规范,仅支持canonical dereferencing。
字段名称 | 类型 | 描述 |
---|---|---|
$ref | string | 必填参考字符串。 |
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'
模式对象允许定义输入和输出数据类型。 这些类型可以是对象,也可以是基本数据类型和数组。 此对象基于JSON模式规范草案4,并使用其预定义的子集。 在此子集之上,本规范提供了扩展,以允许更完整的文档。
有关属性的更多信息,请参见JSON Schema Core和JSON Schema Validation。 除非另有说明,属性定义遵循此处所引用的JSON模式规范。
以下属性直接从JSON模式定义中获取,并遵循相同的规范:
以下属性取自JSON模式定义,但其定义已调整为Swagger规范。 它们的定义与JSON Schema中的定义相同,只有原始定义引用了JSON Schema定义,才使用Schema Object定义。
除了JSON模式子集字段之外,以下字段可用于进一步的模式文档。
字段名称 | 类型 | 描述 |
---|---|---|
discriminator | string | 增加了对多态性的支持。鉴别器是用于区分继承此模式的其他模式之间的模式属性名称。必须在此模式中定义所使用的属性名称,并且必须在"必需"属性列表中。使用时,值必须是此模式或继承它的任何模式的名称。 |
readOnly | boolean | 仅与模式"属性"定义相关。将该属性声明为"只读"。这意味着它可以作为响应的一部分发送,但不能作为请求的一部分发送。标记为"readOnly"为"true"的属性不应该在定义的模式的"必需"列表中。默认值为"false"。 |
xml | XML Object | 这只能用于属性模式。它对根模式没有影响。添加额外的元数据来描述此属性的XML表示格式。 |
externalDocs | External Documentation Object | 此模式的其他外部文档。 |
example | Any | 一个自由格式属性,用于包括此模式的实例示例。 |
模式字段 | 类型 | 描述 |
---|---|---|
^ x- | Any | 允许扩展到Swagger架构。字段名必须以x- 开头,例如x-internal-id 。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展。 |
Swagger允许使用JSON Schema的"allOf"属性组合和扩展模型定义,实际上提供模型组合。 allOf
接受一个独立的验证的对象定义数组,一起构成一个对象。
虽然组合提供模型可扩展性,但并不意味着模型之间的层次结构。为了支持多态,Swagger增加了discriminator
字段的支持。使用时,discriminator
将是用于决定使用哪个模式定义验证模型结构的属性的名称。因此,discriminator
字段必须是必填字段。所选属性的值必须是在definitions
属性下赋予模型的友好名称。因此,不具有给定id的内联模式定义不能用于多态。
当将XML定义转换为XML时,xml属性允许额外的定义。 XML对象包含有关可用选项的其他信息。
与以前版本的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
minimum0
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'
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
id1
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
minimum100
maximum600
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
default0
minimum0
required
packSize
允许更精细的XML模型定义的元数据对象。
使用数组时,XML元素名称不会被推断(用于单数/复数形式),而'name`属性应用于添加该信息。查看预期行为的例子。
字段名称 | 类型 | 描述 |
---|---|---|
name | string | 替换用于描述的schema属性的元素/属性的名称。在Items对象(items )中定义时,它将影响列表中各个XML元素的名称。当type 定义为array (items 之外)时,它会影响包装元素,只有当wrapped 为true 时。如果wrapped 是false ,那将被忽略。 |
命名空间 | string | 命名空间定义的URL。值应以URL的形式。 |
前缀 | string | 用于name的前缀。 |
属性 | boolean | 声明属性定义是否转换为属性而不是元素。默认值为"false"。 |
wrap | boolean | 可以仅用于数组定义。表示数组是否被包装(例如,<books> <book/> <book/> </books> )或展开(<book/> <book/> )。默认值为"false"。定义只有当type 定义为array (items 之外)时才会生效。 |
模式字段 | 类型 | 描述 |
---|---|---|
^ x- | Any | 允许扩展到Swagger架构。字段名必须以x- 开头,例如x-internal-id 。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展。 |
XML对象定义的示例包含在Schema Object的属性定义中,并具有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>
xxxxxxxxxx
{
"animals": {
"type": "string",
"xml": {
"name": "animal"
}
}
}
xxxxxxxxxx
animals
type string
xml
name animal
xxxxxxxxxx
<animal>...</animal>
在此示例中,显示了完整的模型定义。
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
attributetrue
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>
更改元素名称:
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
wrappedtrue
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
wrappedtrue
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
wrappedtrue
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
wrappedtrue
xxxxxxxxxx
<aliens>
<aliens>value</aliens>
<aliens>value</aliens>
</aliens>
用于保存可由操作消耗和生成的数据类型。 这些数据类型可以是基本数据类型,数组或模型。
模式字段 | 类型 | 描述 |
---|---|---|
{name} | Schema Object | 单一定义,将"名称"映射到其定义的模式。 |
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
用于保存跨操作重用的参数。 参数定义可以参考这里定义的参数。
这不定义全局操作参数。
模式字段 | 类型 | 描述 |
---|---|---|
{name} | Parameter Object | 单个参数定义,将"名称"映射到其定义的参数。 |
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
requiredtrue
type integer
format int32
limitParam
name limit
in query
description max records to return
requiredtrue
type integer
format int32
一个对象要保持响应在操作之间重复使用。 响应定义可以参考这里定义的定义。
这不定义全局操作响应。
模式字段 | 类型 | 描述 |
---|---|---|
{name} | Response Object | 单个响应定义,将"名称"映射到其定义的响应。 |
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'
说明书中可用的安全方案的声明。 这不执行操作的安全性方案,仅用于为每个方案提供相关详细信息。
模式字段 | 类型 | 描述 |
---|---|---|
{name} | Security Scheme Object | 单个安全方案定义,将"名称"映射到其定义的方案。 |
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
允许定义可以由操作使用的安全性方案。支持的方案是基本认证,API密钥(作为头或查询参数)和OAuth2的公共流(隐式,密码,应用和访问代码)。
字段名称 | 类型 | 有效性 | 描述 |
---|---|---|---|
type | string | Any | 必需安全方案的类型。有效值为"basic","apiKey"或"oauth2"。 |
description | string | Any | 安全方案的简短描述。 |
name | string | apiKey | 必需要使用的标题或查询参数的名称。 |
in | string | apiKey | 必需 API密钥的位置。有效值为"query" 或"header" 。 |
flow | string | oauth2 | 必需 ** OAuth2安全方案使用的流程。有效值为 "implicit" , "password" , "application" or "accessCode" 。 |
authorizationUrl | string | oauth2 ("implicit" ,"accessCode" ) | 必需 **用于此流的授权URL。这应该是一个URL的形式。 |
tokenUrl | string | oauth2 ("password" ,"application" ,"accessCode" ) | 必需 **用于此流的令牌URL。这应该是一个URL的形式。 |
scopes | Scopes Object | oauth2 | 必需。 OAuth2安全方案的可用范围。 |
字段名称 | 类型 | 描述 |
---|---|---|
^ x- | Any | 允许扩展到Swagger架构。字段名必须以x- 开头,例如x-internal-id 。该值可以是"null",一个原始数据类型,一个数组或一个对象。有关详细信息,请参阅供应商扩展。 |
xxxxxxxxxx
{
"type": "basic"
}
xxxxxxxxxx
type basic
xxxxxxxxxx
{
"type": "apiKey",
"name": "api_key",
"in": "header"
}
xxxxxxxxxx
type apiKey
name api_key
in header
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
列出OAuth2安全方案的可用范围。
模式字段 | 类型 | 描述 |
---|---|---|
{name} | string | 将一个范围的名称映射到其简短描述(作为属性的值)。 |
模式字段 | 类型 | 描述 |
---|---|---|
^ x- | Any | 允许扩展到Swagger架构。 字段名必须以x- 开头,例如x-internal-id 。 该值可以是"null",一个原始数据类型,一个数组或一个对象。 有关详细信息,请参阅供应商扩展。 |
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
列出执行此操作所需的安全方案。 对象可以具有在其中声明的多个安全性方案,这些安全性方案都是必需的(即,方案之间存在逻辑和)。
用于每个属性的名称必须对应于安全定义中声明的安全性方案。
模式字段 | 类型 | 描述 |
---|---|---|
{name} | [string ] | 每个名称必须对应于在安全定义中声明的安全性方案。 如果安全方案的类型为"oauth2"`,那么该值就是执行所需的范围名称列表。 对于其他安全方案类型,数组必须为空。 |
xxxxxxxxxx
{
"api_key": []
}
xxxxxxxxxx
api_key
xxxxxxxxxx
{
"petstore_auth": [
"write:pets",
"read:pets"
]
}
xxxxxxxxxx
petstore_auth
write:pets
read:pets
虽然Swagger规范尝试适应大多数用例,但可以添加附加数据以在某些点扩展规范。
扩展属性始终以"x-"
为前缀,并且可以具有任何有效的JSON格式值。
可用或可能不支持扩展可用的工具,但也可以扩展扩展,以添加请求的支持(如果工具是内部的或开源的)。
Swagger规范中的某些对象可能被声明并保持为空,或者完全被删除,即便它们本质上是API文档的核心。
背景原因是允许对文档本身进行额外的访问控制。虽然不是规范本身的一部分,但某些库可能会选择允许基于某种形式的身份验证/授权访问文档的部分内容。
两个例子: