Skip to content

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.63.0From the migration guide
channels: {orders/{region}/created: ...}: the key is the topicchannels: {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 channeloperations: {publishOrderCreated: {action: send, channel: {$ref: ...}}}"subscribe meant that others could subscribe because your application was the one publishing"
publish: inside the channelan operation with action: receive"When you specified publish, it implied that others could publish to this channel since your application subscribed to it"
operationId: publishOrderCreatedthe key under operations
message: with messageId, or oneOf for severalmessages: 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.urlhost (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 externalDocsinside info"In v2, two properties of tags and externalDocs were placed outside of the Info Object"
channels: {} requiredchannels 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

  1. Change asyncapi to 3.0.0 (or 3.1.0) and run the linter: the errors list the 2.x constructs.
  2. For each channel, pick an id, move the key into address, and turn message into messages: {<id>: ...}, dropping messageId.
  3. Turn each publish / subscribe into an entry under operations with action: receive / action: send and channel: {$ref: '#/channels/<id>'}; the operationId becomes the entry's key.
  4. Split server url into host and pathname; move root tags and externalDocs into info; reduce parameters to description, enum, default, examples, location.
  5. 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.

Sources

Related pages

  • AsyncAPI linter: every finding apigit lint reports, with its fix

    apigit lint checks AsyncAPI 2.x and 3.x files offline with Spectral's AsyncAPI rules. The findings it printed on real documents, what each rule asks for, how to fix it, how to change the rule set, and the version string that stops every rule.

  • AsyncAPI channels, operations and messages: the 3.0 structure, step by step

    In AsyncAPI 3.0 a channel is an address with the messages on it, an operation says whether the application sends to or receives from a channel, and messages live under the channel. Each part built up in a working document, with the linter's message when it is missing or wrong, and four mistakes the linter does not catch.

  • AsyncAPI documentation: render it locally, then publish it from Git

    An AsyncAPI file becomes a documentation page with its servers, channels, messages and operations. How to see it on your machine with apigit preview before committing, what the page shows, and how to publish a version from a commit in the browser or with apigit doc publish.

  • What is AsyncAPI? A minimal 3.0 document, linted and rendered

    AsyncAPI describes message-driven APIs: the channels an application sends to or receives from, and the messages on them. How it differs from OpenAPI, a 21-line AsyncAPI 3.0 document, what a linter says about it, and how it renders as documentation.

Try it with your own API

Create a free workspace, add an OpenAPI file, and publish its documentation and a mock server from the same repository.

Free for individuals. No credit card.

$ npm install -g @apigit/cli$ apigit auth login$ apigit doc publish