Skip to content

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

Last checked and updated

apigit lint events.yaml checks an AsyncAPI 2.x or 3.x file on your machine, without an account. It runs Spectral's AsyncAPI rules (in the CLI's default set, oas-best-practice, which applies the rules for the file's format) plus a check of every $ref. Errors are a document that does not match the specification; warnings are mostly documentation advice (some, such as asyncapi-3-channel-no-query-nor-fragment, enforce a SHALL NOT of the specification); the exit code is 1 only when there is an error.

$ npm install -g @apigit/cli
$ apigit lint events.yaml

The findings on a minimal document

A valid 3.0 document with only a title, a version, one channel and one operation (it is on What is AsyncAPI) gets the documentation rules:

$ apigit lint asyncapi.yaml
asyncapi.yaml
  1:1    warn   asyncapi-servers                  AsyncAPI object must have non-empty "servers" object.
  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)
  2:6    warn   asyncapi-info-contact             Info object must have "contact" object.  (info)
  2:6    warn   asyncapi-info-description         Info "description" must be present and non-empty string.  (info)
  2:6    warn   asyncapi-info-license             Info object must have "license" object.  (info)
  18:23  warn   asyncapi-3-operation-description  Operation "description" must be present and non-empty string.  (operations.publishOrderCreated)

7 problems (0 errors, 6 warnings, 1 info, 0 hints)

With a contact that has only name and email, one more appears:

...
  6:11  warn   asyncapi-info-contact-properties  Contact object must have "name", "url" and "email".  (info.contact)
...

The findings on a broken document

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
  tags:
    - name: orders
    - name: orders
servers:
  production:
    host: broker.example.com/
    protocol: amqp
channels:
  orderCreated:
    address: orders.created
    messages:
      orderCreated:
        payload:
          $ref: '#/components/schemas/Order'
operations:
  publishOrderCreated:
    action: send
    description: The order service sends this event after an order is saved.
    channel:
      $ref: '#/channels/orderCreated'
    security:
      - $ref: '#/components/securitySchemes/apiKey'
components:
  schemas:
    Order:
      type: object
      properties:
        orderId:
          type: string
    Refund:
      type: object
$ apigit lint events.yaml
events.yaml
  1:11   info   asyncapi-latest-version              The latest version is not used. You should update to the "3.1.0" version.  (asyncapi)
  12:8   error  asyncapi-3-document-resolved         "tags" property must not have duplicate items (items ## 0 and 1 are identical)  (info.tags)
  14:13  error  asyncapi-3-tags-uniqueness           "tags" object contains duplicate tag name "orders".  (info.tags.1.name)
  17:11  warn   asyncapi-3-server-no-trailing-slash  Server host must not end with slash.  (servers.production.host)
  33:15  error  invalid-ref                          '#/components/securitySchemes/apiKey' does not exist  (operations.publishOrderCreated.security.0.$ref)
  41:12  warn   asyncapi-unused-components-schema    Potentially unused components schema has been detected.  (components.schemas.Refund)

6 problems (3 errors, 2 warnings, 1 info, 0 hints)
error: lint failed: findings at severity error or above

A license without url is not reported: asyncapi-info-license-url exists, but Spectral lists it as not recommended, so it is off by default.

Every rule seen, with its fix

Severity is what apigit lint printed; the meaning is quoted from Spectral's rule documentation or the specification.

RuleSeverityWhat it asks forFix
asyncapi-3-document-resolvederror"Validate structure of AsyncAPI specification when references have been resolved": the document against the 3.x schemaRead the message: a missing required property (action, host), a property not allowed here (subscribe, url, messageId), a wrong type
invalid-referrorevery $ref points to somethingcorrect the path; see finding a broken reference
asyncapi-3-tags-uniquenesserror"Tags must not have duplicate names"remove or rename the duplicate
asyncapi-channel-parameterserrorparameters in the address are described, and no othersnames under parameters equal to the {...} in address
asyncapi-operation-operationId (2.x)erroran operationId on every publish and subscribeadd one
asyncapi-serverswarn"A non-empty servers object is expected to be located at the root of the document."add servers with host and protocol (3.x) or url and protocol (2.x)
asyncapi-info-contactwarn"Info object should contain contact object."add info.contact
asyncapi-info-contact-propertieswarna contact "full of the most useful properties: name, url, and email"add the missing ones
asyncapi-info-descriptionwarn"AsyncAPI object info description must be present and non-empty string."add info.description (Markdown allowed)
asyncapi-info-licensewarn"The info object should have a license key."add info.license.name
asyncapi-3-tagswarn"AsyncAPI object should have non-empty tags array."add info.tags (in 3.x the tags are inside info)
asyncapi-3-operation-description, asyncapi-operation-description (2.x)warn"Operation objects should have a description."add description to the operation
asyncapi-3-server-no-trailing-slashwarn"Server host should not have a trailing slash."remove the slash
asyncapi-3-channel-no-trailing-slash, asyncapi-3-channel-no-query-nor-fragment, asyncapi-3-channel-no-empty-parameterwarna clean channel addresssee channels, operations and messages
asyncapi-unused-components-schemawarn"Potential unused reusable schema entry has been detected." Spectral warns that it "may identify false positives" for a file used as a libraryuse it, delete it, or turn the rule off for library files
asyncapi-latest-versioninfo"Checking if the AsyncAPI document is using the latest version."change asyncapi to 3.1.0; the 3.1.0 release notes call it "a minor release" without breaking changes. A 2.x file needs the migration first

A version string the rules do not know

The version string decides which rules run. When it does not match a known AsyncAPI version, none of them can run, and apigit lint (since CLI 0.9.1) reports that as an error:

$ apigit lint short-version.yaml        # asyncapi: '3.0'
short-version.yaml
  1:1  error  unrecognized-format  "asyncapi": "3.0" is not a version the AsyncAPI rules know, so none of them ran. Write major.minor.patch, such as "3.0.0" or "2.6.0"

1 problem (1 error, 0 warnings, 0 info, 0 hints)
error: lint failed: findings at severity error or above

The exit code was 1. The file was the clean document of the previous pages with asyncapi: '3.0'; the specification says the version string "must be major.minor.patch". Older CLI versions printed Spectral's own unrecognized-format warning here and passed with exit code 0; if you pin one of those in CI, add --fail-severity warn or upgrade.

A version that matches the pattern but does not exist yet, such as 3.2.0, is linted with the 3.x rules and gets only the asyncapi-latest-version note.

Failing a build on warnings, and annotations

With --fail-severity warn, the minimal document above fails (exit code 1):

$ apigit lint --fail-severity warn asyncapi.yaml
...
7 problems (0 errors, 6 warnings, 1 info, 0 hints)
error: lint failed: findings at severity warn or above

--format github-actions prints one workflow command per finding:

$ apigit lint --format github-actions events.yaml
::notice file=events.yaml,line=1,col=11,title=asyncapi-latest-version::The latest version is not used. You should update to the "3.1.0" version.
::error file=events.yaml,line=12,col=8,title=asyncapi-3-document-resolved::"tags" property must not have duplicate items (items ## 0 and 1 are identical)
::error file=events.yaml,line=14,col=13,title=asyncapi-3-tags-uniqueness::"tags" object contains duplicate tag name "orders".
::warning file=events.yaml,line=17,col=11,title=asyncapi-3-server-no-trailing-slash::Server host must not end with slash.
::error file=events.yaml,line=33,col=15,title=invalid-ref::'#/components/securitySchemes/apiKey' does not exist
::warning file=events.yaml,line=41,col=12,title=asyncapi-unused-components-schema::Potentially unused components schema has been detected.
error: lint failed: findings at severity error or above

GitHub Actions turns each ::error, ::warning and ::notice line into an annotation on that line of the file. A complete workflow, run on GitHub with an OpenAPI file, is in Lint an OpenAPI file in GitHub Actions; the lint step takes an AsyncAPI file the same way.

Your own rule set

A .spectral.yaml in the working directory replaces the built-in set. Spectral's documentation: "In your ruleset file you can add extends: "spectral:asyncapi" and you'll get all of the following rules applied." This one turns the version note off and switches on a rule that is off by default:

extends: ["spectral:asyncapi"]
rules:
  asyncapi-latest-version: off
  asyncapi-3-server-not-example-com: warn
$ apigit lint asyncapi.yaml             # the document of the channels page, with host: example.com
asyncapi.yaml
  17:11  warn   asyncapi-3-server-not-example-com  Server host must not point at example.com.  (servers.production.host)

1 problem (0 errors, 1 warning, 0 info, 0 hints)

The CLI's other built-in sets (owasp-top10, url-guideline, document) are for OpenAPI and refuse an AsyncAPI file:

$ apigit lint --builtin owasp-top10 asyncapi.yaml
asyncapi.yaml
  1:1  error  not-openapi          the rule set "owasp-top10" is for OpenAPI documents; this is an AsyncAPI document
  1:1  warn   unrecognized-format  The provided document does not match any of the registered formats [OpenAPI 2.0 (Swagger), OpenAPI 3.x, OpenAPI 3.0.x, OpenAPI 3.1.x]

2 problems (1 error, 1 warning, 0 info, 0 hints)
error: lint failed: findings at severity error or above

What the other commands do with AsyncAPI

apigit score refuses an AsyncAPI file (error: asyncapi-3.yaml is not an OpenAPI document: no "openapi" or "swagger" field, exit code 2). apigit diff and apigit breaking "Compare an OpenAPI document" (their help text) and refuse AsyncAPI too:

$ apigit breaking asyncapi-3.yaml renamed.yaml
error: asyncapi-3.yaml is an AsyncAPI document: apigit diff and breaking compare OpenAPI documents only

The exit code is 2. Before CLI 0.9.1 they printed No breaking change. with exit code 0 for two AsyncAPI files whose channel address differed. For an AsyncAPI change, review the diff of the file.

Sources

Related pages

  • AsyncAPI 2 to 3 migration: the same API in 2.6 and 3.0, side by side

    One small event API written in AsyncAPI 2.6 and in 3.0: publish and subscribe become operations with action send or receive, the channel key becomes an id with an address, messages move under the channel, servers split url into host and pathname. With the linter's output for each version and for a 2.6 file that only had its version changed.

  • 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