Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: adding document for Reuse with traits page #2036

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pages/docs/concepts/asyncapi-document/_section.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
title: 'AsyncAPI Document'
weight: 50
---
105 changes: 105 additions & 0 deletions pages/docs/concepts/asyncapi-document/reusing-traits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
title: Reusing Traits
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not about reusing traits

maybe better Messages and Operations reusability with Traits?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the name dont you think Messages and Operations reusability with Traits is gonna be too long? How about only Traits??

weight: 200
---

Traits are a powerful way to define properties that can be reused across multiple message objects within the specification. Reusing traits promotes code maintainability, reduces duplication, and makes your AsyncAPI documents cleaner and easier to manage.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not only for for messages


## Defining Traits
mhmohona marked this conversation as resolved.
Show resolved Hide resolved

Traits are defined under the `components` section of your AsyncAPI document, within `operationTraits` or `messageTraits`, depending on whether you want to apply them to operations or messages, respectively. Each trait is given a unique name and contains the properties that will be applied.

```mermaid
graph TD
A[AsyncAPI Document] --> B{Components}
B --> C[operationTraits]
B --> D[messageTraits]
C --> E{Trait 1}
D --> G{Trait 2}
E --> I[Properties]
G --> K[Properties]
```

## Applying Traits to Operations

Once a trait is defined, you can apply it to an operation using the `$ref` keyword in the `traits` section of the operation. The `$ref` value should point to the path of the trait within the `components` section.

```mermaid
graph LR
A((User Signup Operation))
B[traits]
C[$ref of kafka trait]
A-->B
B-->C
```

Here's applying the `kafka` trait to an operation:

```yml
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a valid document? almost all on root, and kinda confusing that $ref is there with other props 🤔

also if you show how to use trait, also show how the trait looks like

and most important, show how resulting document will look like after traits are really applied to document

channel:
$ref: '#/channels/userSignup'
action: send
tags:
- name: user
- name: signup
- name: register
traits:
- $ref: '#/components/operationTraits/kafka'
```

In this case, the `userSignup` operation in the `userSignup` channel applies the `kafka` trait.

## Applying Traits to Messages

Traits can also be applied to `message` objects in the messages section of the AsyncAPI document. This is done using the `$ref` keyword within the `traits` section of the message object.

```mermaid
graph TD
subgraph AsyncAPI Document
traitsSection[traits]
messagesSection[messages]
end

traitsSection --> messageObject["$ref"]
messageObject --> messagesSection
```

For example, let's say we have a trait named `commonHeaders` defined in messageTraits:
mhmohona marked this conversation as resolved.
Show resolved Hide resolved

```yml
components:
messageTraits:
commonHeaders:
headers:
type: object
properties:
Content-Type:
type: integer
```

To apply this trait to a message object, you can do:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like in case of operation

I need to clearly see example trait, example how it is referenced to be used, and how resulting document will look like

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am struggling to come up with cde example. @derberg could you please help me here with the example?


```yml
traits:
- $ref: '#/components/messageTraits/commonHeaders'
```

In this example, the `commonHeaders` trait, which includes a `Content-Type` header, is applied to the `commonMessage`.

## Trait Merging
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is is different with 3.0
asyncapi/spec#907


Traits in AsyncAPI are merged into the message object using the [JSON Merge Patch](https://datatracker.ietf.org/doc/html/rfc7386) protocol, which means that traits are merged into the operation or message object.

```mermaid
graph TD
A[Traits]
B[Operation/Message]
C[Trait Merging]

subgraph JSON Merge Patch
C
end

A --> C
B --> C
```