# operation-channel-in-channels

Source: https://api-genie.dev/docs/linter/asyncapi/rules/operation-channel-in-channels

A rule of the AsyncAPI linter, which reads AsyncAPI 3 documents; the reader rejects AsyncAPI 2 documents. Since 0.12.0. In the rulesets: Recommended at error, Strict at error, All at error.

This rule checks that the channel of an operation under the root `operations` is a reference to a
channel under the root `channels`.

## Rule details

An operation names the channel it sends to or receives from with a reference. The specification
requires the operations under the root `operations` to point at a channel under the root
`channels`, and at no other place, so that every channel the application uses is listed there. A
reference to `components/channels`, or to another file, is reported. A root entry that is a
reference to an operation under `components/operations` is checked as that operation, once however
many entries name it; an operation under `components/operations` that no root entry names may refer
to any channel. A root entry that refers to another file is not checked.

The rule reports `Operation channel '<ref>' must be a reference to a channel of the root channels
('#/channels/...')` on the operation's `channel`.

The rule is the counterpart of the AsyncAPI parser's
`asyncapi3-required-operation-channel-unambiguity`, which accepts a reference to another file's
`channels` and skips root entries that are references.

Turn the rule off for a spec that keeps its channels in another file: the linter does not read
other files, and `asyncapi validate` accepts the reference.

## Examples

**Good**.

```yaml
asyncapi: 3.0.0
info:
  title: Pet Events
  version: 1.0.0
channels:
  petAdopted:
    address: pets.adopted
operations:
  onPetAdopted:
    action: receive
    channel:
      $ref: '#/channels/petAdopted'
```

**Bad**. The operation's channel is under `components`.

```yaml
asyncapi: 3.0.0
info:
  title: Pet Events
  version: 1.0.0
operations:
  onPetAdopted:
    action: receive
    channel:
      $ref: '#/components/channels/petAdopted'
components:
  channels:
    petAdopted:
      address: pets.adopted
```
