Async API 规范(中文版)
    Async API 规范(中文版)
    • Async API 规范

    Async API 规范

    异步 API 规范#

    免责声明#

    此内容的一部分取自OpenAPI Initiative 的人们所做的伟大工作。主要是因为它是一项伟大的工作,我们希望与 OpenAPI 规范 保持尽可能多的兼容性。

    版本 2.0.0#

    本文档中的关键词“必须”、“不得”、“必需”、“应”、“不应”、“应该”、“不应”、“推荐”、“可以”和“可选”如 RFC 2119 中所述进行解释。
    AsyncAPI 规范根据 Apache 许可证 2.0 版获得许可。

    介绍#

    AsyncAPI 规范是一个用于以机器可读格式描述和记录消息驱动 API 的项目。它与协议无关,因此您可以将它用于在任何协议(例如,AMQP、MQTT、WebSockets、Kafka、STOMP、HTTP 等)上工作的 API。
    AsyncAPI 规范定义了一组描述此类 API 所需的文件。这些文件随后可用于创建实用程序,例如文档、集成和/或测试工具。
    文件必须描述应用程序接受的操作。例如,考虑以下 AsyncAPI 定义片段:
    
    1 user/signedup:
    2  subscribe:
    3    $ref: "#/components/messages/userSignUp"
    这意味着应用程序允许消费者订阅接收 userSignUp 消息的user/signedup 频道。
    AsyncAPI 规范不假定任何类型的软件拓扑、架构或模式。 因此,服务器可以是消息代理、网络服务器或任何其他类型的能够发送和/或接收数据的计算机程序。但是,AsyncAPI 提供了一种称为“绑定”的机制,旨在帮助获得有关协议和/或拓扑的更具体信息。

    定义#

    应用#

    应用程序是任何一种计算机程序或其中的一组。它必须是生产者、消费者或两者。应用程序可以是微服务、物联网设备(传感器)、大型机进程等。应用程序可以用任意数量的不同编程语言编写,只要它们支持所选协议。应用程序还必须使用服务器支持的协议来连接和交换消息。

    制作人#

    生产者是一种连接到服务器的应用程序,它正在创建消息并将它们寻址到通道。生产者可以根据服务器、协议和用例模式发布到多个渠道。

    消费者#

    消费者是一种应用程序,通过支持的协议连接到服务器,它正在使用来自通道的消息。消费者可以根据服务器、协议和用例模式从多个渠道消费。

    信息#

    消息是通过服务器和应用程序之间的通道交换信息的机制。消息必须包含有效载荷,也可以包含标头。标头可以细分为协议定义的标头和应用程序定义的标头属性,它们可以充当支持元数据。有效负载包含应用程序定义的数据,必须将其序列化为某种格式(JSON、XML、Avro、二进制等)。由于消息是一种通用机制,它可以支持多种交互模式,例如事件、命令、请求或响应。

    渠道#

    通道是可寻址的组件,由服务器提供,用于消息的组织。生产者应用程序将消息发送到通道,消费者应用程序使用来自通道的消息。服务器可以支持多个通道实例,允许将具有不同内容的消息发送到不同的通道。根据服务器实现,通道可以通过协议定义的标头包含在消息中。

    协议#

    协议是在应用程序和通道之间交换消息的机制(有线协议或 API)。示例协议包括但不限于 AMQP、HTTP、JMS、Kafka、MQTT、STOMP、WebSocket。

    规格#

    格式#

    描述符合 AsyncAPI 规范的消息驱动 API 的文件被表示为 JSON 对象并符合 JSON 标准。YAML 作为 JSON 的超集,也可用于表示 A2S(AsyncAPI 规范)文件。
    例如,如果说某个字段具有数组值,则将使用 JSON 数组表示形式:
    1 {
    2   "field" : [...]
    3 }
    虽然 API 是使用 JSON 描述的,但它不会将 JSON 输入/输出强加给 API 本身。
    规范中的所有字段名称都区分大小写。
    该模式公开了两种类型的字段。具有声明名称的固定字段和为字段名称声明正则表达式模式的模式化字段。模式字段可以多次出现,只要每个字段都有唯一的名称。
    为了保留在 YAML 和 JSON 格式之间往返的能力,建议使用 YAML 1.2 版以及一些额外的限制:
    标签必须限制在 JSON 模式规则集允许的范围内
    YAML 映射中使用的键必须限制为标量字符串,如 YAML Failsafe 模式规则集所定义

    文件结构#

    API 的 A2S 表示由单个文件组成。但是,根据用户的判断,可以将部分定义拆分为单独的文件。这适用于JSON 模式定义$ref中的规范中的字段。
    按照惯例,AsyncAPI 规范 (A2S) 文件命名为asyncapi.json或asyncapi.yaml.

    图式#

    异步 API 对象#

    这是 API 规范的根文档对象。它将资源列表和 API 声明合并到一个文档中。
    固定字段#
    字段名称类型描述
    asyncapiAsyncAPI Version String必需的。 指定正在使用的 AsyncAPI 规范版本。工具规范和客户端可以使用它来解释版本。结构应为major. minor. patch,其中patch版本必须与现有的 兼容major。minor工装。通常会引入补丁版本来解决文档中的错误,并且工具通常应该与相应的major. minor(1.0.*)。补丁版本将对应本文档的补丁。
    idIdentifierAsyncAPI 文档正在定义的应用程序的标识符。
    infoInfo Object必需的。提供有关 API 的元数据。如果需要,客户端可以使用元数据。
    serversServers Object提供服务器的连接详细信息。
    channelsChannels Object必需 API 的可用通道和消息。
    componentsComponents Object用于保存规范的各种模式的元素。
    tagsTags Object规范使用的带有附加元数据的标签列表。列表中的每个标签名称必须是唯一的。
    externalDocsExternal Documentation Object额外的外部文档。
    这个对象可以用规范扩展来扩展。

    AsyncAPI 版本字符串#

    版本字符串表示文档符合的 AsyncAPI 规范的版本。此字符串的格式必须是major. minor. patch. patch 可以用连字符和额外的字母数字字符作为后缀。
    一个major。minor应用于指定 AsyncAPI 规范版本,并将被视为与该 指定的 AsyncAPI 规范兼容major。minor版本。补丁版本不会被工具考虑,不区分1.0.0和1.0.1。
    在 AsyncAPI 规范的后续版本中,将注意版本的增加minor不应干扰为较低次要版本开发的工具的操作。因此,假设的1.1.0规范应该可用于为 . 设计的工具1.0.0。

    标识符#

    该字段表示 AsyncAPI 文档正在定义的应用程序的唯一通用标识符。根据RFC3986 ,它必须符合 URI 格式。
    建议使用 URN 在很长一段时间内全局唯一地标识应用程序,即使在它变得不可用或不再存在之后也是如此。
    例子#
    
    1 {
    2  "id": "urn:com:smartylighting:streetlights:server"
    3 }
    id: 'urn:com:smartylighting:streetlights:server'
    
    1 {
    2  "id": "https://github.com/smartylighting/streetlights-server"
    3 }
    id: 'https://github.com/smartylighting/streetlights-server'

    信息对象#

    该对象提供有关 API 的元数据。如果需要,客户端可以使用元数据。
    固定字段#
    字段名称类型描述
    titlestring必须的。应用程序的标题。
    versionstring必需提供应用程序 API 的版本(不要与规范版本混淆)。
    descriptionstring应用程序的简短描述。CommonMark 语法可用于富文本表示。
    termsOfServicestringAPI 服务条款的 URL。必须采用 URL 格式。
    contactContact Object公开的 API 的联系信息。
    licenseLicense Object公开的 API 的许可证信息。
    这个对象可以用规范扩展来扩展。
    信息对象示例:#
    
    1 {
    2  "title": "AsyncAPI Sample App",
    3  "description": "This is a sample server.",
    4  "termsOfService": "http://asyncapi.org/terms/",
    5  "contact": {
    6    "name": "API Support",
    7    "url": "http://www.asyncapi.org/support",
    8    "email": "support@asyncapi.org"
    9  },
    10  "license": {
    11    "name": "Apache 2.0",
    12    "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
    13  },
    14  "version": "1.0.1"
    15 }
    
    1 title: AsyncAPI Sample App
    2 description: This is a sample server.
    3 termsOfService: http://asyncapi.org/terms/
    4 contact:
    5  name: API Support
    6  url: http://www.asyncapi.org/support
    7  email: support@asyncapi.org
    8 license:
    9  name: Apache 2.0
    10  url: http://www.apache.org/licenses/LICENSE-2.0.html
    11 version: 1.0.1

    联系对象#

    公开的 API 的联系信息。
    固定字段#
    字段名称类型描述
    namestring联系人/组织的识别名称。
    urlstring指向联系信息的 URL。必须采用 URL 格式。
    emailstring联系人/组织的电子邮件地址。必须采用电子邮件地址的格式。
    这个对象可以用规范扩展来扩展。
    联系人对象示例:#
    
    1 {
    2  "name": "API Support",
    3  "url": "http://www.example.com/support",
    4  "email": "support@example.com"
    5 }
    
    1 name: API Support
    2 url: http://www.example.com/support
    3 email: support@example.com

    许可证对象#

    公开的 API 的许可信息。
    固定字段#
    字段名称类型描述
    namestring必需的。用于 API 的许可证名称。
    urlstring用于 API 的许可证的 URL。必须采用 URL 格式。
    这个对象可以用规范扩展来扩展。
    许可证对象示例:#
    
    1 {
    2  "name": "Apache 2.0",
    3  "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
    4 }
    
    1 name: Apache 2.0
    2 url: http://www.apache.org/licenses/LICENSE-2.0.html

    服务器对象#

    服务器对象是服务器对象的映射。
    图案字段#
    字段模式类型描述
    ^[A-Za-z0-9_-]+$Server Object这个应用程序可以连接到的服务器的定义。
    服务器对象示例#
    
    1 {
    2  "production": {
    3    "url": "development.gigantic-server.com",
    4    "description": "Development server",
    5    "protocol": "kafka",
    6    "protocolVersion": "1.0.0"
    7  }
    8 }
    
    1 production:
    2  url: development.gigantic-server.com
    3  description: Development server
    4  protocol: kafka
    5  protocolVersion: '1.0.0'

    服务器对象#

    表示消息代理、服务器或任何其他类型的能够发送和/或接收数据的计算机程序的对象。此对象用于捕获 URI、协议和安全配置等详细信息。可以使用变量替换,以便代码生成工具可以注入一些细节,例如用户名和密码。
    固定字段#
    字段名称类型描述
    urlstring必需的。目标主机的 URL。此 URL 支持服务器变量并且可以是相对的,以指示主机位置是相对于提供 AsyncAPI 文档的位置。{当在括号中命名变量时,将进行变量替换}。
    protocolstring必需的。此 URL 支持的连接协议。支持的协议包括但不限于:amqp amqps http https jms kafka kafka-secure mqtt secure-mqtt stomp stomps ws wss
    protocolVersionstring用于连接的协议版本。例如: AMQP 0.9.1、 HTTP 2.0、 Kafka1.0.0等。
    descriptionstring可选字符串,描述 URL 指定的主机。CommonMark 语法可以用于富文本表示。
    variablesMap[string, Server Variable Object]变量名称与其值之间的映射。该值用于替换服务器的 URL 模板。
    security[Security Requirement Object]该服务器可以使用哪些安全机制的声明。值列表包括可以使用的替代安全要求对象。只需满足其中一个安全要求对象即可授权连接或操作。
    bindingsServer Bindings Object一个自由格式的映射,其中键描述协议的名称,值描述服务器的协议特定定义。
    这个对象可以用 Specification Extensions 来扩展。
    服务器对象示例#
    单个服务器将被描述为:
    
    1 {
    2  "url": "development.gigantic-server.com",
    3  "description": "Development server",
    4  "protocol": "kafka",
    5  "protocolVersion": "1.0.0"
    6 }
    
    1 url: development.gigantic-server.com
    2 description: Development server
    3 protocol: kafka
    4 protocolVersion: '1.0.0'
    下面显示了如何描述多个服务器,例如,在 AsyncAPI 对象中servers:
    
    1 {
    2  "servers": {
    3    "development": {
    4      "url": "development.gigantic-server.com",
    5      "description": "Development server",
    6      "protocol": "amqp",
    7      "protocolVersion": "0.9.1"
    8    },
    9    "staging": {
    10      "url": "staging.gigantic-server.com",
    11      "description": "Staging server",
    12      "protocol": "amqp",
    13      "protocolVersion": "0.9.1"
    14    },
    15    "production": {
    16      "url": "api.gigantic-server.com",
    17      "description": "Production server",
    18      "protocol": "amqp",
    19      "protocolVersion": "0.9.1"
    20    }
    21  }
    22 }
    
    1 servers:
    2  development:
    3    url: development.gigantic-server.com
    4    description: Development server
    5    protocol: amqp
    6    protocolVersion: 0.9.1
    7  staging:
    8    url: staging.gigantic-server.com
    9    description: Staging server
    10    protocol: amqp
    11    protocolVersion: 0.9.1
    12  production:
    13    url: api.gigantic-server.com
    14    description: Production server
    15    protocol: amqp
    16    protocolVersion: 0.9.1
    下面显示了如何将变量用于服务器配置:
    
    1 {
    2  "servers": [
    3    {
    4      "url": "{username}.gigantic-server.com:{port}/{basePath}",
    5      "description": "The production API server",
    6      "protocol": "secure-mqtt",
    7      "variables": {
    8        "username": {
    9          "default": "demo",
    10          "description": "This value is assigned by the service provider, in  this example `gigantic-server.com`"
    11        },
    12        "port": {
    13          "enum": [
    14            "8883",
    15            "8884"
    16          ],
    17          "default": "8883"
    18        },
    19        "basePath": {
    20          "default": "v2"
    21        }
    22      }
    23    }
    24   ]
    25 }
    
    1 servers:
    2 - url: '{username}.gigantic-server.com:{port}/{basePath}'
    3  description: The production API server
    4  protocol: secure-mqtt
    5  variables:
    6    username:
    7      # note! no enum here means it is an open value
    8      default: demo
    9      description: This value is assigned by the service provider, in this  example `gigantic-server.com`
    10    port:
    11      enum:
    12        - '8883'
    13        - '8884'
    14      default: '8883'
    15    basePath:
    16      # open meaning there is the opportunity to use special base paths as assigned by the provider, default is `v2`
    17      default: v2

    服务器变量对象#

    表示用于服务器 URL 模板替换的服务器变量的对象。
    固定字段#
    字段名称类型描述
    enum[string]如果替换选项来自有限集合,则要使用的字符串值的枚举。
    defaultstring如果未提供替代值,则用于替换和发送的默认值。
    descriptionstring服务器变量的可选描述。CommonMark 语法可以用于富文本表示。
    examples[string]服务器变量的示例数组。
    这个对象可以用 Specification Extensions 来扩展。

    默认内容类型#

    表示编码/解码消息负载时使用的默认内容类型的字符串。该值必须是特定的媒体类型(例如application/json)。当省略 contentType 属性时,模式解析器必须使用此值。
    如果无法使用此值对消息进行编码/解码,则模式解析器必须使用其默认内容类型。
    默认内容类型示例#
    
    1 {
    2  "defaultContentType": "application/json"
    3 }
    defaultContentType: application/json

    通道对象#

    保存单个通道及其操作的相对路径。通道路径是相对于服务器的。
    通道也称为“主题”、“路由键”、“事件类型”或“路径”。
    图案字段#
    字段模式类型描述
    {channel}Channel Item Object单个通道的相对路径。字段名称必须采用RFC 6570 URI 模板的形式。不得使用查询参数和片段,而是使用绑定来定义它们。
    通道对象示例#
    
    1 {
    2  "user/signedup": {
    3    "subscribe": {
    4      "$ref": "#/components/messages/userSignedUp"
    5    }
    6  }
    7 }
    
    1 user/signedup:
    2  subscribe:
    3    $ref: "#/components/messages/userSignedUp"

    渠道项目对象#

    描述单个通道上可用的操作。
    固定字段#
    字段名称类型描述
    $refstring允许对此频道项进行外部定义。引用的结构必须采用 Channel Item Object 的格式。如果引用的定义与此频道项的定义之间存在冲突,则行为为undefined。
    descriptionstring此频道项目的可选描述。CommonMark 语法 可用于富文本表示。
    subscribeOperation ObjectSUBSCRIBE 操作的定义。
    publishOperation Object发布操作的定义。
    parametersParameters Object通道名称中包含的参数映射。它应该仅在使用带有表达式的通道时出现(如 RFC 6570 第 2.2 节 所定义)。
    bindingsChannel Bindings Object一个自由格式的映射,其中键描述协议的名称,值描述通道的协议特定定义。
    这个对象可以用规范扩展来扩展。
    渠道项目对象示例#
    
    1 {
    2  "description": "This channel is used to exchange messages about users signing up",
    3  "subscribe": {
    4    "summary": "A user signed up.",
    5    "message": {
    6      "description": "A longer description of the message",
    7      "payload": {
    8        "type": "object",
    9        "properties": {
    10          "user": {
    11            "$ref": "#/components/schemas/user"
    12          },
    13          "signup": {
    14            "$ref": "#/components/schemas/signup"
    15          }
    16        }
    17      }
    18    }
    19  },
    20  "bindings": {
    21    "amqp": {
    22      "is": "queue",
    23      "queue": {
    24        "exclusive": true
    25      }
    26    }
    27  }
    28 }
    
    1 description: This channel is used to exchange messages about users signing up
    2 subscribe:
    3  summary: A user signed up.
    4  message:
    5    description: A longer description of the message
    6    payload:
    7      type: object
    8      properties:
    9        user:
    10          $ref: "#/components/schemas/user"
    11        signup:
    12 bindings:
    13  amqp:
    14    is: queue
    15    queue:
    16      exclusive: true
    用于oneOf为每个操作指定多条消息:
    
    1 {
    2  "subscribe": {
    3    "message": {
    4      "oneOf": [
    5        { "$ref": "#/components/messages/signup" },
    6        { "$ref": "#/components/messages/login" }
    7      ]
    8    }
    9  }
    10 }
    
    1 subscribe:
    2  message:
    3    oneOf:
    4      - $ref: '#/components/messages/signup'
    5      - $ref: '#/components/messages/login'

    操作对象#

    描述发布或订阅操作。这提供了一个记录如何以及为什么发送和接收消息的地方。例如,操作可能描述了一个聊天应用程序用例,其中用户向群组发送文本消息。
    固定字段#
    字段名称类型描述
    operationIdstring用于标识操作的唯一字符串。在 API 中描述的所有操作中,id 必须是唯一的。operationId 值区分大小写。工具和库可以使用 operationId 来唯一标识一个操作,因此,建议遵循通用的编程命名约定。
    summarystring操作内容的简短摘要。
    descriptionstring对操作的详细解释。CommonMark 语法可用于富文本表示。
    tags[Tag Object]API 文档控制的标签列表。标签可用于操作的逻辑分组。
    externalDocsExternal Documentation Object此操作的附加外部文档。
    bindingsMap[string, Operation Bindings Object]一个自由格式的映射,其中键描述协议的名称,值描述操作的协议特定定义。
    traits[Operation Trait Object]应用于操作对象的特征列表。特征必须按照此处定义的相同顺序使用JSON 合并补丁算法合并到操作对象中。
    messageMessage Object将在此频道上发布或接收的消息的定义。oneOf此处允许指定多个消息,但是,消息必须仅对引用的消息对象之一有效。
    这个对象可以用规范扩展来扩展。
    操作对象示例#
    
    1 {
    2  "operationId": "registerUser",
    3  "summary": "Action to sign a user up.",
    4  "description": "A longer description",
    5  "tags": [
    6    { "name": "user" },
    7    { "name": "signup" },
    8    { "name": "register" }
    9  ],
    10  "message": {
    11    "headers": {
    12      "type": "object",
    13      "properties": {
    14        "applicationInstanceId": {
    15          "description": "Unique identifier for a given instance of the publishing application",
    16          "type": "string"
    17        }
    18      }
    19    },
    20    "payload": {
    21      "type": "object",
    22      "properties": {
    23        "user": {
    24          "$ref": "#/components/schemas/userCreate"
    25        },
    26        "signup": {
    27          "$ref": "#/components/schemas/signup"
    28        }
    29      }
    30    }
    31  },
    32  "bindings": {
    33    "amqp": {
    34      "ack": false
    35    },
    36  },
    37  "traits": [
    38    { "$ref": "#/components/operationTraits/kafka" }
    39  ]
    40 }
    
    1 operationId: registerUser
    2 summary: Action to sign a user up.
    3 description: A longer description
    4 tags:
    5  - name: user
    6  - name: signup
    7  - name: register
    8 message:
    9  headers:
    10    type: object
    11    properties:
    12      applicationInstanceId:
    13        description: Unique identifier for a given instance of the publishing application
    14        type: string
    15  payload:
    16    type: object
    17    properties:
    18      user:
    19        $ref: "#/components/schemas/userCreate"
    20      signup:
    21        $ref: "#/components/schemas/signup"
    22 bindings:
    23  amqp:
    24    ack: false
    25 traits:
    26  - $ref: "#/components/operationTraits/kafka"

    操作特征对象#

    描述可以应用于操作对象的特征。这个对象可以包含来自 Operation Object 的任何属性,除了message和traits。
    如果您希望将特征应用于消息,请参阅消息特征对象。
    固定字段#
    字段名称类型描述
    operationIdstring用于标识操作的唯一字符串。在 API 中描述的所有操作中,id 必须是唯一的。operationId 值区分大小写。工具和库可以使用 operationId 来唯一标识一个操作,因此,建议遵循通用的编程命名约定。
    summarystring操作内容的简短摘要。
    descriptionstring对操作的详细解释。CommonMark 语法可用于富文本表示。
    tagsTags ObjectAPI 文档控制的标签列表。标签可用于操作的逻辑分组。
    externalDocsExternal Documentation Object此操作的附加外部文档。
    bindingsOperation Bindings Object一个自由格式的映射,其中键描述协议的名称,值描述操作的协议特定定义。
    这个对象可以用规范扩展来扩展。
    操作特征对象示例#
    
    1 {
    2  "bindings": {
    3    "amqp": {
    4      "ack": false
    5    }
    6  }
    7 }
    
    1 bindings:
    2  amqp:
    3    ack: false

    参数对象#

    描述通道名称中包含的参数映射。
    该映射必须包含父通道名称中使用的所有参数。
    图案字段#
    字段模式类型描述
    ^[A-Za-z0-9_-]+$参数对象 / 参考对象键代表参数的名称。它必须与父通道名称中使用的参数名称相匹配。
    参数对象示例#
    
    1 {
    2  "user/{userId}/signup": {
    3    "parameters": {
    4      "userId": {
    5        "description": "Id of the user.",
    6        "schema": {
    7          "type": "string"
    8        }
    9      }
    10    },
    11    "subscribe": {
    12      "$ref": "#/components/messages/userSignedUp"
    13    }
    14  }
    15 }
    
    1 user/{userId}/signup:
    2  parameters:
    3    userId:
    4      description: Id of the user.
    5      schema:
    6        type: string
    7  subscribe:
    8    $ref: "#/components/messages/userSignedUp"

    参数对象#

    描述包含在通道名称中的参数。
    固定字段#
    字段名称类型描述
    descriptionstring参数的详细解释。CommonMark 语法可用于富文本表示。
    schemaSchema Object参数的定义。
    locationstring指定参数值位置的运行时表达式。即使存在目标字段的定义,也不得使用它来验证此参数,而schema必须使用属性。
    这个对象可以用规范扩展来扩展。
    参数对象示例#
    
    1 {
    2  "user/{userId}/signup": {
    3    "parameters": {
    4      "userId": {
    5        "description": "Id of the user.",
    6        "schema": {
    7          "type": "string"
    8        },
    9        "location": "$message.payload#/user/id"
    10      }
    11    },
    12    "subscribe": {
    13      "$ref": "#/components/messages/userSignedUp"
    14    }
    15  }
    16 }
    
    1 user/{userId}/signup:
    2  parameters:
    3    userId:
    4      description: Id of the user.
    5      schema:
    6        type: string
    7      location: $message.payload#/user/id
    8  subscribe:
    9    $ref: "#/components/messages/userSignedUp"

    服务器绑定对象#

    描述服务器特定协议定义的映射。
    固定字段#
    字段名称类型描述
    httpHTTP 服务器绑定HTTP 服务器的特定于协议的信息。
    wsWebSockets 服务器绑定WebSockets 服务器的特定于协议的信息。
    kafkaKafka 服务器绑定Kafka 服务器的特定于协议的信息。
    amqpAMQP 服务器绑定AMQP 0-9-1 服务器的协议特定信息。
    amqp1AMQP 1.0 服务器绑定AMQP 1.0 服务器的特定于协议的信息。
    mqttMQTT 服务器绑定MQTT 服务器的特定于协议的信息。
    mqtt5MQTT 5 服务器绑定MQTT 5 服务器的特定于协议的信息。
    natsNATS 服务器绑定NATS 服务器的协议特定信息。
    jmsJMS 服务器绑定JMS 服务器的特定于协议的信息。
    snsSNS 服务器绑定SNS 服务器的特定于协议的信息。
    sqsSQS 服务器绑定SQS 服务器的特定于协议的信息。
    stompSTOMP 服务器绑定STOMP 服务器的特定于协议的信息。
    redisRedis 服务器绑定Redis 服务器的特定于协议的信息。

    通道绑定对象#

    映射描述通道的协议特定定义。
    固定字段#
    字段名称类型描述
    httpHTTP 通道绑定HTTP 通道的特定于协议的信息。
    wsWebSockets 通道绑定WebSockets 通道的特定于协议的信息。
    kafkaKafka 通道绑定Kafka 通道的特定于协议的信息。
    amqpAMQP 通道绑定AMQP 0-9-1 通道的特定于协议的信息。
    amqp1AMQP 1.0 通道绑定AMQP 1.0 通道的特定于协议的信息。
    mqttMQTT 通道绑定MQTT 通道的特定于协议的信息。
    mqtt5MQTT 5 通道绑定MQTT 5 通道的特定于协议的信息。
    natsNATS 通道绑定NATS 通道的特定于协议的信息。
    jmsJMS 通道绑定JMS 通道的特定于协议的信息。
    snsSNS 频道绑定SNS 频道的特定于协议的信息。
    sqsSQS 通道绑定SQS 通道的特定于协议的信息。
    stompSTOMP 通道绑定STOMP 通道的特定于协议的信息。
    redisRedis 通道绑定Redis 通道的特定于协议的信息。

    操作绑定对象#

    描述操作的特定于协议的定义的映射。
    固定字段#
    字段名称类型描述
    httpHTTP 操作绑定HTTP 操作的特定于协议的信息。
    wsWebSockets 操作绑定WebSockets 操作的特定于协议的信息。
    kafkaKafka 操作绑定Kafka 操作的特定于协议的信息。
    amqpAMQP 操作绑定AMQP 0-9-1 操作的特定于协议的信息。
    amqp1AMQP 1.0 操作绑定AMQP 1.0 操作的特定于协议的信息。
    mqttMQTT 操作绑定MQTT 操作的特定于协议的信息。
    mqtt5MQTT 5 操作绑定MQTT 5 操作的特定于协议的信息。
    natsNATS 操作绑定NATS 操作的协议特定信息。
    jmsJMS 操作绑定JMS 操作的特定于协议的信息。
    snsSNS运营绑定SNS 操作的特定于协议的信息。
    sqsSQS操作绑定SQS 操作的特定于协议的信息。
    stompSTOMP操作绑定STOMP 操作的特定于协议的信息。
    redisRedis操作绑定Redis 操作的特定于协议的信息。

    消息绑定对象#

    描述消息的协议特定定义的映射。
    固定字段#
    字段名称类型描述
    httpHTTP 消息绑定HTTP 消息(即请求或响应)的协议特定信息。
    wsWebSockets 消息绑定WebSockets 消息的特定于协议的信息。
    kafkaKafka 消息绑定Kafka 消息的特定于协议的信息。
    amqpAMQP 消息绑定AMQP 0-9-1 消息的特定于协议的信息。
    amqp1AMQP 1.0 消息绑定AMQP 1.0 消息的特定于协议的信息。
    mqttMQTT 消息绑定MQTT 消息的特定于协议的信息。
    mqtt5MQTT 5 消息绑定MQTT 5 消息的特定于协议的信息。
    natsNATS 消息绑定NATS 消息的协议特定信息。
    jmsJMS 消息绑定JMS 消息的特定于协议的信息。
    snsSNS消息绑定SNS 消息的特定于协议的信息。
    sqsSQS 消息绑定SQS 消息的特定于协议的信息。
    stompSTOMP 消息绑定STOMP 消息的特定于协议的信息。
    redisRedis 消息绑定Redis 消息的特定于协议的信息。

    消息对象#

    描述在给定通道和操作上收到的消息。
    固定字段#
    字段名称类型描述
    headersSchema Object / Reference Object应用程序标头的架构定义。模式必须是“对象”类型。它不得定义协议标头。
    payloadany消息负载的定义。它可以是任何类型,但默认为Schema object。
    correlationIdCorrelation ID Object / Reference Object用于消息跟踪或匹配的关联 ID 的定义。
    schemaFormatstring一个字符串,包含用于定义消息负载的架构格式的名称。如果省略,实现应将有效负载解析为Schema object。查看支持的架构格式表以获取更多信息。允许自定义值,但它们的实现是可选的。自定义值不得引用表中列出的模式格式之一。
    contentTypestring编码/解码消息负载时使用的内容类型。该值必须是特定的媒体类型(例如application/json)。省略时,该值必须是在defaultContentType字段上指定的值。
    namestring消息的机器友好名称。
    titlestring消息的人性化标题。
    summarystring消息内容的简短摘要。
    descriptionstring消息的详细解释。CommonMark 语法可用于富文本表示。
    tagsTags ObjectAPI 文档控制的标签列表。标签可用于消息的逻辑分组。
    externalDocsExternal Documentation Object此消息的其他外部文档。
    bindingsMessage Bindings Object一个自由格式的映射,其中键描述协议的名称,值描述消息的协议特定定义。
    examples[Map[string, any]]包含有效消息对象示例的数组。
    特质[Message Trait Object]应用于消息对象的特征列表。特征必须按照此处定义的相同顺序使用JSON 合并补丁算法合并到消息对象中。结果对象必须是一个有效的消息对象。
    这个对象可以用规范扩展来扩展。
    架构格式表#
    下表包含每个实现都必须支持的一组值。
    姓名允许值笔记
    AsyncAPI 2.0.0 架构对象application/vnd.aai.asyncapi;version=2.0.0, application/vnd.aai.asyncapi+json;version=2.0.0,application/vnd.aai.asyncapi+yaml;version=2.0.0schemaFormat当没有提供a 时,这是默认值。
    OpenAPI 3.0.0 架构对象application/vnd.oai.openapi;version=3.0.0, application/vnd.oai.openapi+json;version=3.0.0,application/vnd.oai.openapi+yaml;version=3.0.0
    JSON 架构草案 07application/schema+json;version=draft-07,application/schema+yaml;version=draft-07
    Avro 1.9.0 架构application/vnd.apache.avro;version=1.9.0, application/vnd.apache.avro+json;version=1.9.0,application/vnd.apache.avro+yaml;version=1.9.0
    消息对象示例#
    
    1 {
    2  "name": "UserSignup",
    3  "title": "User signup",
    4  "summary": "Action to sign a user up.",
    5  "description": "A longer description",
    6  "contentType": "application/json",
    7  "tags": [
    8    { "name": "user" },
    9    { "name": "signup" },
    10    { "name": "register" }
    11  ],
    12  "headers": {
    13    "type": "object",
    14    "properties": {
    15      "correlationId": {
    16        "description": "Correlation ID set by application",
    17        "type": "string"
    18      },
    19      "applicationInstanceId": {
    20        "description": "Unique identifier for a given instance of the publishing application",
    21        "type": "string"
    22      }
    23    }
    24  },
    25  "payload": {
    26    "type": "object",
    27    "properties": {
    28      "user": {
    29        "$ref": "#/components/schemas/userCreate"
    30      },
    31      "signup": {
    32        "$ref": "#/components/schemas/signup"
    33      }
    34    }
    35  },
    36  "correlationId": {
    37    "description": "Default Correlation ID",
    38    "location": "$message.header#/correlationId"
    39  },
    40  "traits": [
    41    { "$ref": "#/components/messageTraits/commonHeaders" }
    42  ]
    43 }
    
    1 name: UserSignup
    2 title: User signup
    3 summary: Action to sign a user up.
    4 description: A longer description
    5 contentType: application/json
    6 tags:
    7  - name: user
    8  - name: signup
    9  - name: register
    10 headers:
    11  type: object
    12  properties:
    13    correlationId:
    14      description: Correlation ID set by application
    15      type: string
    16    applicationInstanceId:
    17      description: Unique identifier for a given instance of the publishing application
    18      type: string
    19 payload:
    20  type: object
    21  properties:
    22    user:
    23      $ref: "#/components/schemas/userCreate"
    24    signup:
    25      $ref: "#/components/schemas/signup"
    26 correlationId:
    27  description: Default Correlation ID
    28  location: $message.header#/correlationId
    29 traits:
    30  - $ref: "#/components/messageTraits/commonHeaders"
    使用 Avro 定义负载的示例:
    
    1 {
    2  "name": "UserSignup",
    3  "title": "User signup",
    4  "summary": "Action to sign a user up.",
    5  "description": "A longer description",
    6  "tags": [
    7    { "name": "user" },
    8    { "name": "signup" },
    9    { "name": "register" }
    10  ],
    11  "schemaFormat": "application/vnd.apache.avro+json;version=1.9.0",
    12  "payload": {
    13    "$ref": "path/to/user-create.avsc#/UserCreate"
    14  }
    15 }
    
    1 name: UserSignup
    2 title: User signup
    3 summary: Action to sign a user up.
    4 description: A longer description
    5 tags:
    6  - name: user
    7  - name: signup
    8  - name: register
    9 schemaFormat: 'application/vnd.apache.avro+yaml;version=1.9.0'
    10 payload:
    11  $ref: 'path/to/user-create.avsc/#UserCreate'

    消息特征对象#

    描述可以应用于Message Object的特征。这个对象可以包含消息对象的任何属性,除了payload和traits。
    如果您希望将特征应用于操作,请参阅操作特征对象。
    固定字段#
    字段名称类型描述
    headersSchema Object / Reference Object应用程序标头的架构定义。模式必须是“对象”类型。它不得定义协议标头。
    correlationIdCorrelation ID Object / Reference Object用于消息跟踪或匹配的关联 ID 的定义。
    schemaFormatstring包含用于定义消息负载的模式格式/语言名称的字符串。如果省略,实现应将有效负载解析为Schema object。
    contentTypestring编码/解码消息负载时使用的内容类型。该值必须是特定的媒体类型(例如application/json)。省略时,该值必须是在defaultContentType字段上指定的值。
    namestring消息的机器友好名称。
    titlestring消息的人性化标题。
    summarystring消息内容的简短摘要。
    descriptionstring消息的详细解释。CommonMark 语法可用于富文本表示。
    tagsTags ObjectAPI 文档控制的标签列表。标签可用于消息的逻辑分组。
    externalDocsExternal Documentation Object此消息的其他外部文档。
    bindingsMap[ string,消息绑定对象]一个自由格式的映射,其中键描述协议的名称,值描述消息的协议特定定义。
    examples[Map[ string, any] ]包含有效消息对象示例的数组。
    这个对象可以用规范扩展来扩展。
    消息特征对象示例#
    
    1 {
    2  "schemaFormat": "application/vnd.apache.avro+json;version=1.9.0",
    3  "contentType": "application/json"
    4 }
    
    1 schemaFormat: 'application/vnd.apache.avro+yaml;version=1.9.0'
    2 contentType: application/json

    标签对象#

    标签对象是标签对象的列表。

    标签对象#

    允许将元数据添加到单个标签。
    固定字段#
    字段名称类型描述
    namestring必需的。 标记的名称。
    descriptionstring标签的简短描述。CommonMark 语法可用于富文本表示。
    externalDocsExternal Documentation Object此标签的附加外部文档。
    这个对象可以用规范扩展来扩展。
    标记对象示例#
    
    1 {
    2    "name": "user",
    3    "description": "User-related messages"
    4 }
    
    1 name: user
    2 description: User-related messages

    外部文档对象#

    允许引用外部资源以获取扩展文档。
    固定字段#
    字段名称类型描述
    descriptionstring目标文档的简短描述。CommonMark 语法可用于富文本表示。
    urlstring必需的。 目标文档的 URL。值必须采用 URL 格式。
    这个对象可以用规范扩展来扩展。
    外部文档对象示例#
    1 {
    2  "description": "Find more info here",
    3  "url": "https://example.com"
    4 }
    
    1 description: Find more info here
    2 url: https://example.com

    参考对象#

    一个简单的对象,允许在内部和外部引用规范中的其他组件。
    Reference Object 由JSON Reference定义,并遵循相同的结构、行为和规则。JSON 引用应仅用于引用以 JSON 或 YAML 格式化的模式。对于 YAML 格式的架构,JSON 参考应应用于该架构的 JSON 表示。应通过应用此处描述的转换来生成 JSON 表示。
    对于本规范,参考解析是按照 JSON 参考规范而不是 JSON 模式规范定义的。
    固定字段#
    字段名称类型描述
    $refstring必需的。 参考字符串。
    该对象不能用附加属性扩展,任何添加的属性都应被忽略。
    参考对象示例#
    
    1 {
    2  "$ref": "#/components/schemas/Pet"
    3 }
      $ref: '#/components/schemas/Pet'

    组件对象#

    为 AsyncAPI 规范的不同方面保存一组可重用对象。components 对象中定义的所有对象对 API 都没有影响,除非从 components 对象外部的属性中明确引用它们。
    固定字段#
    字段名称类型描述
    schemasMap[string, Schema Object / Reference Object]一个对象来保存可重用的Schema Objects。
    messagesMap[string, Message Object / Reference Object]一个对象来保存可重用的消息对象。.
    securitySchemesMap[string, Security Scheme Object / Reference Object]一个对象来保存可重用的安全方案对象。
    parametersMap[string, Parameter Object / Reference Object]一个对象来保存可重用的参数对象。
    correlationIdsMap[string, Correlation ID Object]一个对象来保存可重用的相关 ID 对象。
    operationTraitsMap[string, Operation Trait Object]一个对象来保存可重用的Operation Trait Objects。
    messageTraitsMap[string, Message Trait Object]一个对象来保存可重用的Message Trait Objects。
    serverBindingsMap[string, Server Binding Object]一个对象来保存可重用的服务器绑定对象。
    channelBindingsMap[string, Channel Binding Object]一个对象来保存可重用的Channel Binding Objects。
    operationBindingsMap[string, Operation Binding Object]一个对象来保存可重用的操作绑定对象。
    messageBindingsMap[string, Message Binding Object]一个对象来保存可重用的消息绑定对象。
    这个对象可以用规范扩展来扩展。
    上面声明的所有固定字段都是必须使用与正则表达式匹配的键的对象:^[a-zA-Z0-9.-_]+$。
    字段名称示例:
    
    1 User
    2 User_1
    3 User_Name
    4 user-name
    5 my.org.User
    组件对象示例#
    
    1 "components": {
    2  "schemas": {
    3    "Category": {
    4      "type": "object",
    5      "properties": {
    6        "id": {
    7          "type": "integer",
    8          "format": "int64"
    9        },
    10        "name": {
    11          "type": "string"
    12        }
    13      }
    14    },
    15    "Tag": {
    16      "type": "object",
    17      "properties": {
    18        "id": {
    19          "type": "integer",
    20          "format": "int64"
    21        },
    22        "name": {
    23          "type": "string"
    24        }
    25      }
    26    }
    27  },
    28  "messages": {
    29    "userSignUp": {
    30      "summary": "Action to sign a user up.",
    31      "description": "Multiline description of what this action does.\nHere you have another line.\n",
    32      "tags": [
    33        {
    34          "name": "user"
    35        },
    36        {
    37          "name": "signup"
    38        }
    39      ],
    40      "headers": {
    41        "type": "object",
    42        "properties": {
    43          "applicationInstanceId": {
    44            "description": "Unique identifier for a given instance of the publishing application",
    45            "type": "string"
    46          }
    47        }
    48      },
    49      "payload": {
    50        "type": "object",
    51        "properties": {
    52          "user": {
    53            "$ref": "#/components/schemas/userCreate"
    54          },
    55          "signup": {
    56            "$ref": "#/components/schemas/signup"
    57          }
    58        }
    59      }
    60    }
    61  },
    62  "parameters": {
    63    "userId": {
    64      "name": "userId",
    65      "description": "Id of the user.",
    66      "schema": {
    67        "type": "string"
    68      }
    69    }
    70  },
    71  "correlationIds": {
    72    "default": {
    73      "description": "Default Correlation ID",
    74      "location": "$message.header#/correlationId"
    75    }
    76  },
    77  "messageTraits": {
    78    "commonHeaders": {
    79      "headers": {
    80        "type": "object",
    81        "properties": {
    82          "my-app-header": {
    83            "type": "integer",
    84            "minimum": 0,
    85            "maximum": 100
    86          }
    87        }
    88      }
    89    }
    90  }
    91 }
    
    1 components:
    2  schemas:
    3    Category:
    4      type: object
    5      properties:
    6        id:
    7          type: integer
    8          format: int64
    9        name:
    10          type: string
    11    Tag:
    12      type: object
    13      properties:
    14        id:
    15          type: integer
    16          format: int64
    17        name:
    18          type: string
    19  messages:
    20    userSignUp:
    21      summary: Action to sign a user up.
    22      description: |
    23        Multiline description of what this action does.
    24        Here you have another line.
    25      tags:
    26        - name: user
    27        - name: signup
    28      headers:
    29        type: object
    30        properties:
    31          applicationInstanceId:
    32            description: Unique identifier for a given instance of the publishing application
    33            type: string
    34      payload:
    35        type: object
    36        properties:
    37          user:
    38            $ref: "#/components/schemas/userCreate"
    39          signup:
    40            $ref: "#/components/schemas/signup"
    41  parameters:
    42    userId:
    43    - name: userId
    44      description: Id of the user.
    45      schema:
    46        type: string
    47  correlationIds:
    48    default:
    49      description: Default Correlation ID
    50      location: $message.header#/correlationId
    51  messageTraits:
    52    commonHeaders:
    53      headers:
    54        type: object
    55        properties:
    56          my-app-header:
    57            type: integer
    58            minimum: 0
    59            maximum: 100

    架构对象#

    模式对象允许定义输入和输出数据类型。这些类型可以是对象,也可以是基元和数组。该对象是JSON Schema Specification Draft 07的超集。
    有关属性的更多信息,请参阅JSON Schema Core和JSON Schema Validation。除非另有说明,否则属性定义遵循此处引用的 JSON 架构规范。
    特性#
    AsyncAPI 模式对象是一个 JSON 模式词汇表,它扩展了 JSON 模式核心和验证词汇表。因此,根据定义,可用于这些词汇表的任何关键字在 AsyncAPI 中可用,并且将以完全相同的方式工作,包括但不限于:
    标题
    类型
    必需的
    倍数
    最大限度
    独家最大
    最低限度
    独家最低
    最长长度
    最小长度
    模式(根据ECMA 262 正则表达式方言,此字符串应该是有效的正则表达式)
    最大项目
    最小项目
    独特的物品
    最大属性
    最小属性
    枚举
    常数
    例子
    如果/那么/否则
    只读
    只写
    特性
    图案属性
    附加属性
    附加项目
    项目
    属性名称
    包含
    所有的
    一个
    任何
    不是
    以下属性取自 JSON 架构定义,但它们的定义已根据 AsyncAPI 规范进行了调整。
    description - CommonMark 语法可用于富文本表示。
    format - 有关更多详细信息,请参阅数据类型格式。在依赖 JSON Schema 定义的格式的同时,AsyncAPI 规范提供了一些额外的预定义格式。
    default - 如果未提供默认值,则默认值表示输入的使用者假定为架构值的值。与 JSON Schema 不同,该值必须符合在同一级别定义的 Schema 对象的定义类型。例如 of typeis string, then defaultcan be "foo"but cannot be 1。
    或者,只要可以使用模式对象,就可以使用引用对象代替它。这允许引用定义而不是内联定义它们。
    除了 JSON Schema 字段之外,以下 AsyncAPI 词汇字段可以用于进一步的 schema 文档:
    固定字段#
    字段名称类型描述
    discriminatorstring添加对多态性的支持。鉴别器是用于区分继承此模式的其他模式的模式属性名称。使用的属性名称必须在此模式中定义,并且必须在required属性列表中。使用时,该值必须是该模式或继承它的任何模式的名称。有关详细信息,请参阅组合和继承。
    externalDocsExternal Documentation Object此架构的其他外部文档。
    deprecatedboolean指定模式已弃用并且应该停止使用。默认值为false。
    这个对象可以用规范扩展来扩展。
    组合与继承(多态)#
    AsyncAPI 规范允许使用allOfJSON Schema 的属性组合和扩展模型定义,实际上提供了模型组合。 allOf接收一组对象定义,这些定义独立验证但一起组成一个对象。
    虽然组合提供了模型可扩展性,但并不意味着模型之间存在层次结构。为了支持多态性,AsyncAPI Specification 添加了discriminator字段的支持。使用时,discriminator将是属性的名称,用于决定使用哪个模式定义来验证模型的结构。因此,该discriminator字段必须是必填字段。有两种方法可以为继承实例定义鉴别器的值。
    使用模式的名称。
    通过用新值覆盖属性来覆盖模式的名称。如果存在,则优先于模式的名称。因此,没有给定 id 的内联模式定义不能用于多态性。
    架构对象示例#
    原始样本#
    
    1 {
    2  "type": "string",
    3  "format": "email"
    4 }
    
    1 type: string
    2 format: email
    简单模型#
    
    1 {
    2  "type": "object",
    3  "required": [
    4    "name"
    5  ],
    6  "properties": {
    7    "name": {
    8      "type": "string"
    9    },
    10    "address": {
    11      "$ref": "#/components/schemas/Address"
    12    },
    13    "age": {
    14      "type": "integer",
    15      "format": "int32",
    16      "minimum": 0
    17    }
    18  }
    19 }
    
    1 type: object
    2 required:
    3 - name
    4 properties:
    5  name:
    6    type: string
    7  address:
    8    $ref: '#/components/schemas/Address'
    9  age:
    10    type: integer
    11    format: int32
    12    minimum: 0
    具有地图/字典属性的模型#
    对于简单的字符串到字符串映射:
    
    1 {
    2  "type": "object",
    3  "additionalProperties": {
    4    "type": "string"
    5  }
    6 }
    
    1 type: object
    2 additionalProperties:
    3   type: string
    对于字符串到模型的映射:
    
    1 {
    2  "type": "object",
    3  "additionalProperties": {
    4    "$ref": "#/components/schemas/ComplexModel"
    5  }
    6 }
    
    1 type: object
    2 additionalProperties:
    3  $ref: '#/components/schemas/ComplexModel'
    示例模型#
    
    1 {
    2  "type": "object",
    3  "properties": {
    4    "id": {
    5      "type": "integer",
    6      "format": "int64"
    7    },
    8    "name": {
    9      "type": "string"
    10    }
    11  },
    12  "required": [
    13    "name"
    14  ],
    15  "example": {
    16    "name": "Puma",
    17    "id": 1
    18  }
    19 }
    
    1 type: object
    2 properties:
    3  id:
    4    type: integer
    5    format: int64
    6  name:
    7    type: string
    8 required:
    9 - name
    10 example:
    11  name: Puma
    12  id: 1
    具有组合的模型#
    
    1 {
    2  "schemas": {
    3    "ErrorModel": {
    4      "type": "object",
    5      "required": [
    6        "message",
    7        "code"
    8      ],
    9      "properties": {
    10        "message": {
    11          "type": "string"
    12        },
    13        "code": {
    14          "type": "integer",
    15          "minimum": 100,
    16          "maximum": 600
    17        }
    18      }
    19    },
    20    "ExtendedErrorModel": {
    21      "allOf": [
    22        {
    23          "$ref": "#/components/schemas/ErrorModel"
    24        },
    25        {
    26          "type": "object",
    27          "required": [
    28            "rootCause"
    29          ],
    30          "properties": {
    31            "rootCause": {
    32              "type": "string"
    33            }
    34          }
    35        }
    36      ]
    37    }
    38  }
    39 }
    
    1 schemas:
    2  ErrorModel:
    3    type: object
    4    required:
    5    - message
    6    - code
    7    properties:
    8      message:
    9        type: string
    10      code:
    11        type: integer
    12        minimum: 100
    13        maximum: 600
    14  ExtendedErrorModel:
    15    allOf:
    16    - $ref: '#/components/schemas/ErrorModel'
    17    - type: object
    18      required:
    19      - rootCause
    20      properties:
    21        rootCause:
    22          type: string
    支持多态性的模型#
    
    1 {
    2  "schemas": {
    3    "Pet": {
    4      "type": "object",
    5      "discriminator": "petType",
    6      "properties": {
    7        "name": {
    8          "type": "string"
    9        },
    10        "petType": {
    11          "type": "string"
    12        }
    13      },
    14      "required": [
    15        "name",
    16        "petType"
    17      ]
    18    },
    19    "Cat": {
    20      "description": "A representation of a cat. Note that `Cat` will be used as the discriminator value.",
    21      "allOf": [
    22        {
    23          "$ref": "#/components/schemas/Pet"
    24        },
    25        {
    26          "type": "object",
    27          "properties": {
    28            "huntingSkill": {
    29              "type": "string",
    30              "description": "The measured skill for hunting",
    31              "enum": [
    32                "clueless",
    33                "lazy",
    34                "adventurous",
    35                "aggressive"
    36              ]
    37            }
    38          },
    39          "required": [
    40            "huntingSkill"
    41          ]
    42        }
    43      ]
    44    },
    45    "Dog": {
    46      "description": "A representation of a dog. Note that `Dog` will be used as the discriminator value.",
    47      "allOf": [
    48        {
    49          "$ref": "#/components/schemas/Pet"
    50        },
    51        {
    52          "type": "object",
    53          "properties": {
    54            "packSize": {
    55              "type": "integer",
    56              "format": "int32",
    57              "description": "the size of the pack the dog is from",
    58              "minimum": 0
    59            }
    60          },
    61          "required": [
    62            "packSize"
    63          ]
    64        }
    65      ]
    66    }
    67  }
    68 }
    
    1 schemas:
    2  Pet:
    3    type: object
    4    discriminator: petType
    5    properties:
    6      name:
    7        type: string
    8      petType:
    9        type: string
    10    required:
    11    - name
    12    - petType
    13  Cat:  ## "Cat" will be used as the discriminator value
    14    description: A representation of a cat
    15    allOf:
    16    - $ref: '#/components/schemas/Pet'
    17    - type: object
    18      properties:
    19        huntingSkill:
    20          type: string
    21          description: The measured skill for hunting
    22          enum:
    23          - clueless
    24          - lazy
    25          - adventurous
    26          - aggressive
    27      required:
    28      - huntingSkill
    29  Dog:  ## "Dog" will be used as the discriminator value
    30    description: A representation of a dog
    31    allOf:
    32    - $ref: '#/components/schemas/Pet'
    33    - type: object
    34      properties:
    35        packSize:
    36          type: integer
    37          format: int32
    38          description: the size of the pack the dog is from
    39          minimum: 0
    40      required:
    41      - packSize

    安全方案对象#

    定义操作可以使用的安全方案。支持的方案有:
    用户密码。
    API 密钥(作为用户或密码)。
    X.509 证书。
    端到端加密(对称或非对称)。
    HTTP 身份验证。
    HTTP API 密钥。
    OAuth2 的公共流程(隐式、资源所有者保护的凭证、客户端凭证和授权代码)如RFC6749中所定义。
    OpenID 连接发现。
    固定字段#
    字段名称类型适用于描述
    typestring任何必需的。安全方案的类型。有效值为"userPassword", "apiKey", "X509", "symmetricEncryption", "asymmetricEncryption", "httpApiKey", "http",oauth2和openIdConnect。
    descriptionstring任何安全方案的简短描述。CommonMark 语法可以用于富文本表示。
    namestringhttpApiKey必需的。要使用的标头、查询或 cookie 参数的名称。
    instringapiKey / httpApiKey必需的。API 密钥的位置。有效值为"user"and "password"for apiKeyand"query"或for 。"header"``"cookie"``httpApiKey
    schemestringhttp必需的。在 RFC7235 中定义的授权标头中使用的 HTTP 授权方案的名称。
    bearerFormatstringhttp( "bearer")提示客户端确定不记名令牌的格式。不记名令牌通常由授权服务器生成,因此此信息主要用于文档目的。
    flowsOAuth 流对象oauth2必需的。包含支持的流类型的配置信息的对象。
    openIdConnectUrlstringopenIdConnect必需的。用于发现 OAuth2 配置值的 OpenId Connect URL。这必须是 URL 的形式。
    这个对象可以用Specification Extensions来扩展。
    安全方案对象示例#
    用户/密码验证示例#
    
    1 {
    2  "type": "userPassword"
    3 }
    type: userPassword
    API 密钥认证示例#
    
    1 {
    2  "type": "apiKey",
    3  "in": "user"
    4 }
    
    1 type: apiKey,
    2 in: user
    X.509 认证样本#
    
    1 {
    2   "type": "X509"
    3 }
    type: X509
    端到端加密认证示例#
    
    1 {
    2  "type": "symmetricEncryption"
    3 }
    type: symmetricEncryption
    基本身份验证示例#
    
    1 {
    2  "type": "http",
    3  "scheme": "basic"
    4 }
    
    1 type: http
    2 scheme: basic
    API 密钥样本#
    
    1 {
    2  "type": "httpApiKey",
    3  "name": "api_key",
    4  "in": "header"
    5 }
    
    1 type: httpApiKey
    2 name: api_key
    3 in: header
    JWT 不记名示例#
    
    1 {
    2  "type": "http",
    3  "scheme": "bearer",
    4  "bearerFormat": "JWT",
    5 }
    
    1 type: http
    2 scheme: bearer
    3 bearerFormat: JWT
    隐式 OAUTH2 示例#
    
    1 {
    2  "type": "oauth2",
    3  "flows": {
    4    "implicit": {
    5      "authorizationUrl": "https://example.com/api/oauth/dialog",
    6      "scopes": {
    7        "write:pets": "modify pets in your account",
    8        "read:pets": "read your pets"
    9      }
    10    }
    11  }
    12 }
    
    1 type: oauth2
    2 flows:
    3  implicit:
    4    authorizationUrl: https://example.com/api/oauth/dialog
    5    scopes:
    6      write:pets: modify pets in your account
    7      read:pets: read your pets

    OAuth 流对象#

    允许配置支持的 OAuth 流程。
    固定字段#
    字段名称类型描述
    implicitOAuth 流对象OAuth 隐式流程的配置
    passwordOAuth 流对象OAuth 资源所有者受保护凭证流程的配置
    clientCredentialsOAuth 流对象OAuth 客户端凭证流的配置。
    authorizationCodeOAuth 流对象OAuth 授权代码流的配置。
    这个对象可以用Specification Extensions来扩展。

    OAuth 流对象#

    受支持的 OAuth 流程的配置详细信息
    固定字段#
    字段名称类型适用于描述
    authorizationUrlstringoauth2( "implicit", "authorizationCode")必需的。用于此流的授权 URL。这必须是 URL 的形式。
    tokenUrlstringoauth2( "password", "clientCredentials", "authorizationCode")必需的。用于此流的令牌 URL。这必须是 URL 的形式。
    refreshUrlstringoauth2用于获取刷新令牌的 URL。这必须是 URL 的形式。
    scopesMap[ string, string]oauth2必需的。OAuth2 安全方案的可用范围。作用域名称和它的简短描述之间的映射。
    这个对象可以用Specification Extensions来扩展。
    OAuth 流对象示例#
    
    1 {
    2  "type": "oauth2",
    3  "flows": {
    4    "implicit": {
    5      "authorizationUrl": "https://example.com/api/oauth/dialog",
    6      "scopes": {
    7        "write:pets": "modify pets in your account",
    8        "read:pets": "read your pets"
    9      }
    10    },
    11    "authorizationCode": {
    12      "authorizationUrl": "https://example.com/api/oauth/dialog",
    13      "tokenUrl": "https://example.com/api/oauth/token",
    14      "scopes": {
    15        "write:pets": "modify pets in your account",
    16        "read:pets": "read your pets"
    17      }
    18    }
    19  }
    20 }
    
    1 type: oauth2
    2 flows:
    3  implicit:
    4    authorizationUrl: https://example.com/api/oauth/dialog
    5    scopes:
    6      write:pets: modify pets in your account
    7      read:pets: read your pets
    8  authorizationCode:
    9    authorizationUrl: https://example.com/api/oauth/dialog
    10    tokenUrl: https://example.com/api/oauth/token
    11    scopes:
    12      write:pets: modify pets in your account
    13      read:pets: read your pets

    安全需求对象#

    列出执行此操作所需的安全方案。用于每个属性的名称必须对应于组件对象下的安全方案中声明的安全方案。
    当在服务器对象上定义了安全要求对象列表时,只需满足列表中的安全要求对象之一即可授权连接。
    图案字段#
    字段模式类型描述
    name[ string]每个名称必须对应于在组件对象下的安全方案中声明的安全方案。如果安全方案的类型为或,则该值是执行所需范围名称的列表。对于其他安全方案类型,该数组必须为空。"oauth2"``"openIdConnect"
    安全要求对象示例#
    用户/密码安全要求#
    1
    2
    3
    {
      "user_pass": []
    }
    user_pass: []
    API 密钥安全要求#
    1
    2
    3
    {
      "api_key": []
    }
    api_key: []
    OAUTH2 安全要求#
    
    1 {
    2  "petstore_auth": [
    3    "write:pets",
    4    "read:pets"
    5  ]
    6 }
    
    1 petstore_auth:
    2 - write:pets
    3 - read:pets

    关联 ID 对象#

    在设计时指定可用于消息跟踪和关联的标识符的对象。
    为了指定和计算关联 ID 的位置,使用了运行时表达式。
    固定字段#
    字段名称类型描述
    descriptionstring标识符的可选描述。CommonMark 语法可用于富文本表示。
    locationstring必需的。 指定相关 ID 位置的运行时表达式。
    这个对象可以用规范扩展来扩展。
    例子#
    
    1 {
    2  "description": "Default Correlation ID",
    3  "location": "$message.header#/correlationId"
    4 }
    
    1 description: Default Correlation ID
    2 location: $message.header#/correlationId

    运行时表达式#

    运行时表达式允许根据消息中可用的信息来定义值。此机制由Correlation ID Object使用。
    运行时表达式由以下ABNF语法定义:
    
    1      expression = ( "$message" "." source )
    2      source = ( header-reference | payload-reference )
    3      header-reference = "header." ["#" fragment]
    4      payload-reference = "payload." ["#" fragment]
    5      fragment = a JSON Pointer [RFC 6901](https://tools.ietf.org/html/rfc6901)
    下表提供了运行时表达式的示例以及它们在值中的使用示例:
    例子#
    源位置示例表达式笔记
    Message Header Property$message.header#/MQMD/CorrelIdCorrelId使用标头中的值设置关联 ID MQMD。
    Message Payload Property$message.payload#/messageId关联 ID 是使用messageId消息有效负载中的值设置的。
    运行时表达式保留引用值的类型。

    规范扩展#

    虽然 AsyncAPI 规范试图适应大多数用例,但可以添加额外的数据以在某些点扩展规范。
    扩展属性被实现为总是以 . 为前缀的模式化字段"x-"。
    字段模式类型描述
    ^x-[\w\d-_]+$任何允许扩展 AsyncAPI 模式。字段名必须以 开头x-,例如x-internal-id. 该值可以是null、基元、数组或对象。可以具有任何有效的 JSON 格式值。
    可用工具可能支持也可能不支持扩展,但也可以扩展这些扩展以添加请求的支持(如果工具是内部的或开源的)。

    数据类型格式#

    基元有一个可选的修饰符属性:format. AsyncAPI 规范使用多种已知格式来更精细地定义所使用的数据类型。但是,该format属性是一个开放string值属性,可以具有支持文档需求的任何值。可以使用 、 等格式"email","uuid"即使它们未由本规范定义。不附带format属性的类型遵循它们在 JSON 架构中的定义。不识别特定的工具format可以默认返回type单独的,就好像format没有指定一样。
    AsyncAPI 规范定义的格式是:
    通用名称TYPEFORMAT评论
    integerintegerint32带符号的 32 位
    longintegerint64有符号的 64 位
    floatnumberfloat
    doublenumberdouble
    stringstring
    bytestringbytebase64编码的字符
    binarystringbinary任何八位字节序列
    booleanboolean
    datestringdate由full-date- RFC3339定义
    dateTimestringdate-time由date-time- RFC3339定义
    passwordstringpassword用于提示 UI 需要隐藏输入。
    Built with