AsyncAPI 2 to 3 migration: the same API in 2.6 and 3.0, side by side
Last checked and updated
Moving an AsyncAPI document from 2.x to 3.0 is a change of structure, not of the version line. Operations leave the channels and get their own section, with action: send or action: receive in place of publish and subscribe; the channel key becomes an id and the topic moves to address; messages become a map under the channel; and a server's url is split into host and pathname. Below, one order service written both ways, and what a linter says about each.
The same API in 2.6
asyncapi: 2.6.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:
url: broker.example.com:5672
protocol: amqp
channels:
orders/{region}/created:
parameters:
region:
description: The region the order was placed in.
schema:
type: string
subscribe:
operationId: publishOrderCreated
description: The order service sends this event after an order is saved.
message:
messageId: orderCreated
payload:
type: object
properties:
orderId:
type: string
total:
type: number
$ apigit lint asyncapi-2.yaml
asyncapi-2.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)
And in 3.0
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/{region}/created
parameters:
region:
description: The region the order was placed in.
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'
messages:
- $ref: '#/channels/orderCreated/messages/orderCreated'
$ apigit lint asyncapi-3.yaml
asyncapi-3.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)
Both versions are valid; the info line in both says that 3.1.0 exists.
What moved
| 2.6 | 3.0 | From the migration guide |
|---|---|---|
channels: {orders/{region}/created: ...}: the key is the topic | channels: {orderCreated: {address: orders/{region}/created}} | "the channel key no longer represents the channel path. Instead, it's now an arbitrary unique ID" |
subscribe: inside the channel | operations: {publishOrderCreated: {action: send, channel: {$ref: ...}}} | "subscribe meant that others could subscribe because your application was the one publishing" |
publish: inside the channel | an operation with action: receive | "When you specified publish, it implied that others could publish to this channel since your application subscribed to it" |
operationId: publishOrderCreated | the key under operations | |
message: with messageId, or oneOf for several | messages: under the channel, a map whose keys are the ids | "We have updated the structure of the Message Object by eliminating the messageId property" |
servers.production.url | host (and pathname) | "the url property has been divided into host, pathname, and protocol" |
A parameter with schema: {type: string} | description, enum, default, examples, location only | "Now, you can only access enum, default, description, examples, and location" |
Root tags and externalDocs | inside info | "In v2, two properties of tags and externalDocs were placed outside of the Info Object" |
channels: {} required | channels optional | "In v3, defining channels has become entirely optional" |
The guide also covers changes this example does not use: references that must be explicit (a server's security becomes a list of $ref), traits that no longer override the object they apply to, and schemaFormat moving into a Multi Format Schema Object next to the schema.
A 2.6 file with only the version changed
What happens when the first line says 3.0.0 and nothing else changed:
$ apigit lint bumped-only.yaml
bumped-only.yaml
1:11 info asyncapi-latest-version The latest version is not used. You should update to the "3.1.0" version. (asyncapi)
2:6 warn asyncapi-3-tags AsyncAPI document must have non-empty "tags" array. (info)
13:6 error asyncapi-3-document-resolved Property "tags" is not expected to be here (tags)
16:14 error asyncapi-3-document-resolved "production" property must have required property "host" (servers.production)
17:10 error asyncapi-3-document-resolved Property "url" is not expected to be here (servers.production.url)
24:16 error asyncapi-3-document-resolved Property "schema" is not expected to be here (channels.orders/{region}/created.parameters.region.schema)
26:15 error asyncapi-3-document-resolved Property "subscribe" is not expected to be here (channels.orders/{region}/created.subscribe)
7 problems (5 errors, 1 warning, 1 info, 0 hints)
error: lint failed: findings at severity error or above
The line numbers are those of the 2.6 file above. The list is a good migration checklist, but not a complete one: the channel key orders/{region}/created is not reported, because in 3.0 it is a legal id; the channel then has no address, which the specification reads as unknown. And the messageId inside subscribe is not reported only because subscribe itself is rejected. Left in a 3.0 message, it is (line 27 is a messageId: added under orderCreated: in the 3.0 file above):
$ apigit lint messageid.yaml
...
27:20 error asyncapi-3-document-resolved Property "messageId" is not expected to be here (channels.orderCreated.messages.orderCreated.messageId)
...
action: subscribe is not a 3.0 word
Moving subscribe into an operation as action: subscribe (line 36 of the 3.0 file) gets this answer:
$ apigit lint subscribe-op.yaml
...
36:13 error asyncapi-3-document-resolved "action" property must be equal to one of the allowed values: "send", "receive". A 2.x "subscribe" operation (the application sends) becomes "send". (operations.publishOrderCreated.action)
...
A 2.x subscribe operation becomes action: send, and a 2.x publish becomes action: receive. For action: publish the linter still prints Spectral's own suggestion, Did you mean "receive"?, which is right only for a 2.x publish; decide by what the application does. (CLI versions before 0.9.1 printed Did you mean "receive"? for subscribe too, which is wrong.)
Steps
- Change
asyncapito3.0.0(or3.1.0) and run the linter: the errors list the 2.x constructs. - For each channel, pick an id, move the key into
address, and turnmessageintomessages: {<id>: ...}, droppingmessageId. - Turn each
publish/subscribeinto an entry underoperationswithaction: receive/action: sendandchannel: {$ref: '#/channels/<id>'}; theoperationIdbecomes the entry's key. - Split server
urlintohostandpathname; move roottagsandexternalDocsintoinfo; reduce parameters todescription,enum,default,examples,location. - Lint again until no errors are left, then read the operations once more: the linter checks shape, not direction.
The migration guide also names a converter, asyncapi convert asyncapi.json --output=asyncapi_v3.json --target-version=3.0.0, from the AsyncAPI CLI. It was not run for this page; whatever does the conversion, step 5 still applies.
Related: channels, operations and messages explains the 3.0 structure part by part, and Lint an AsyncAPI file lists the rules.