AsyncAPI channels, operations and messages: the 3.0 structure, step by step
Last checked and updated
An AsyncAPI 3.0 document has three parts that refer to each other. A channel is an address (a topic, a routing key, a path) together with the messages that may travel on it. An operation says what the application does with one channel: action: send or action: receive. Operations point to channels with $ref, and channels own their messages. The same holds in 3.1.
The document
This is the order service from What is AsyncAPI, complete. Each case below changes a few lines of it. The line numbers in the outputs refer to this file, except in the parameter case, where the three lines of address and parameters shown there take the place of line 21 and the findings are on lines 22 and 23 of that version:
asyncapi: 3.0.0
info:
title: Orders events
version: 1.0.0
description: Events the order service sends when an order changes.
contact:
name: Orders team
url: https://example.com/orders-team
email: orders@example.com
license:
name: Apache 2.0
url: https://www.apache.org/licenses/LICENSE-2.0
tags:
- name: orders
servers:
production:
host: broker.example.com:5672
protocol: amqp
channels:
orderCreated:
address: orders.created
messages:
orderCreated:
payload:
type: object
properties:
orderId:
type: string
total:
type: number
operations:
publishOrderCreated:
action: send
description: The order service sends this event after an order is saved.
channel:
$ref: '#/channels/orderCreated'
$ apigit lint asyncapi.yaml
asyncapi.yaml
1:11 info asyncapi-latest-version The latest version is not used. You should update to the "3.1.0" version. (asyncapi)
1 problem (0 errors, 0 warnings, 1 info, 0 hints)
The one finding says that 3.1.0 exists; nothing is wrong with the structure.
Step 1: the channel and its address
channels:
orderCreated: # the channel's id in this document
address: orders.created # the name on the broker
The specification calls address "An optional string representation of this channel's address. The address is typically the "topic name", "routing key", "event type", or "path". When null or absent, it MUST be interpreted as unknown." The key (orderCreated) is only an identifier. Removing the address line is valid, and the linter says nothing; the channel then has no known address.
Two address mistakes are reported, as warnings:
$ apigit lint address-query.yaml # address: orders/created?v=1
...
21:14 warn asyncapi-3-channel-no-query-nor-fragment Channel address must not include query ("?") or fragment ("#") delimiter. (channels.orderCreated.address)
...
$ apigit lint trailing.yaml # address: orders/created/
...
21:14 warn asyncapi-3-channel-no-trailing-slash Channel address must not end with slash. (channels.orderCreated.address)
...
The first follows the specification ("Query parameters and fragments SHALL NOT be used, instead use bindings to define them"); the second is Spectral's advice, because "Most messaging protocols will treat example/foo and example/foo/ as different things."
Parameters in the address
A part of the address in braces is a parameter, and the channel's parameters object describes it:
address: orders.{region}.created
parameters:
region:
description: The region of the order.
When the names do not match, the linter reports both sides:
$ apigit lint param-wrong.yaml # address has {region}, parameters has country
...
22:16 error asyncapi-channel-parameters Not all channel's parameters are described with "parameters" object. Missed: region. (channels.orderCreated.parameters)
23:15 error asyncapi-channel-parameters Channel's "parameters" object has redundant defined "country" parameter. (channels.orderCreated.parameters.country)
...
Step 2: the messages on the channel
messages:
orderCreated: # the message's id
payload:
type: object
messages is a map from an id to a Message Object (or a $ref to one). The specification: "Every message sent to this channel MUST be valid against one, and only one, of the message objects defined in this map." Written as a list, the way 2.x wrote oneOf, it is an error:
$ apigit lint messages-array.yaml # messages: [ {payload: ...} ]
...
22:14 error asyncapi-3-document-resolved "messages" property type must be object (channels.orderCreated.messages)
...
error: lint failed: findings at severity error or above
Step 3: the operation
operations:
publishOrderCreated: # the operation's id
action: send
channel:
$ref: '#/channels/orderCreated'
action and channel are both Required. "Use send when it's expected that the application will send a message to the given channel, and receive when the application should expect receiving messages from the given channel." Without action, or with any other word, the document is invalid:
$ apigit lint no-action.yaml
...
32:23 error asyncapi-3-document-resolved "publishOrderCreated" property must have required property "action" (operations.publishOrderCreated)
...
$ apigit lint action-publish.yaml # action: publish
...
33:13 error asyncapi-3-document-resolved "action" property must be equal to one of the allowed values: "send", "receive". Did you mean "receive"? (operations.publishOrderCreated.action)
...
Do not take the "Did you mean" at its word: here it is wrong. This application sends, so the fix is send. The 2.x words publish and subscribe meant the opposite of what they seem; the migration page has the mapping.
A $ref to a channel that does not exist is reported by the reference check:
$ apigit lint missing-channel.yaml # $ref: '#/channels/orderCreatd'
...
36:13 error invalid-ref '#/channels/orderCreatd' does not exist (operations.publishOrderCreated.channel.$ref)
...
A missing description on an operation is a warning, asyncapi-3-operation-description ("Operation objects should have a description").
Narrowing the messages of an operation
An operation can name which of the channel's messages it handles:
messages:
- $ref: '#/channels/orderCreated/messages/orderCreated'
The specification: the list "MUST contain a subset of the messages defined in the channel referenced in this operation".
What the linter does not catch
Four documents that break a MUST of the specification passed with only the asyncapi-latest-version info finding, exit code 0:
| The document | What the specification says |
|---|---|
An operation whose channel is the channel itself, written inline | "the channel property value MUST be a Reference Object and, therefore, MUST NOT contain a Channel Object" |
An operation whose messages point to #/components/messages/orderCreated, a message that is not in the channel | the list "MUST contain a subset of the messages defined in the channel", and "MUST NOT point to a subset of message definitions located in the Messages Object in the Components Object" |
address: orders.{region}.created with no parameters at all | the Parameters Object "MUST contain all the parameters used in the parent channel address" (the rule above checks this only when a parameters object exists) |
A parameters object on a channel whose address has no {...} | parameters "MUST be present only when the address contains Channel Address Expressions" |
Check these by reading, or in review: they are the parts that tie operations, channels and messages together.
Summary of the findings
| Mistake | Rule | Severity | Fix |
|---|---|---|---|
action missing | asyncapi-3-document-resolved | error | add action: send or action: receive |
action: publish or subscribe | asyncapi-3-document-resolved | error | send or receive, from the application's side |
channel $ref to a missing channel | invalid-ref | error | correct the id under channels |
messages written as a list | asyncapi-3-document-resolved | error | a map: messages: { orderCreated: {...} } |
| Address parameter not described, or described but absent | asyncapi-channel-parameters | error | names in parameters equal to the {...} in address |
? or # in the address | asyncapi-3-channel-no-query-nor-fragment | warn | move it into bindings |
Address ends with / | asyncapi-3-channel-no-trailing-slash | warn | remove the slash |
Operation without description | asyncapi-3-operation-description | warn | add one |
The rest of the rules, for info and servers, are on Lint an AsyncAPI file.