Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmohona committed Nov 7, 2023
1 parent 7b6e5ee commit 77224b5
Showing 1 changed file with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
title: Reusing Traits
title: Messages and Operations reusability with 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.
Traits allows to define properties that can be reused across multiple message and operations within the specification. Reusing traits promotes code maintainability, reduces duplication, and makes your AsyncAPI documents cleaner and easier to manage.

## Defining traits

Expand All @@ -23,6 +23,26 @@ graph TD
style D fill:#47BCEE,stroke:#47BCEE;
```

Here is an AsyncAPI document wehre an object like the following:

```yaml
description: Example description.
traits:
- name: UserSignup
description: Trait description.
- tags:
- name: user
```
The document will look like the following after applying traits:
```yaml
name: UserSignup
description: Example description.
tags:
- name: user
```
## 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.
Expand Down Expand Up @@ -95,7 +115,7 @@ In this document, the `commonHeaders` trait, which includes a `Content-Type` hea

## Trait merging

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.
Traits in AsyncAPI are merged into the message object in the same order they are defined and traits are merged into the operation or message object.

```mermaid
graph TD
Expand Down

0 comments on commit 77224b5

Please sign in to comment.