Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d0ed8eb
initial draft for payload schema
kakabisht Jul 3, 2023
df0a8ab
peer feedback
kakabisht Jul 11, 2023
deb4487
fix formatting
kakabisht Jul 11, 2023
bca219c
Introduce the image
kakabisht Jul 20, 2023
d97f998
Fix code for version
kakabisht Jul 22, 2023
c3c2e1f
fix mermaid diagram color
kakabisht Jul 31, 2023
d3c183c
Yaml code update
kakabisht Aug 29, 2023
900be20
Update pages/docs/concepts/asyncapi-document/payload-schema.md
kakabisht Sep 4, 2023
bd03c28
Update pages/docs/concepts/asyncapi-document/payload-schema.md
kakabisht Sep 4, 2023
47f7c50
Update pages/docs/concepts/asyncapi-document/payload-schema.md
kakabisht Sep 4, 2023
167644c
Update pages/docs/concepts/asyncapi-document/payload-schema.md
kakabisht Sep 4, 2023
583c51b
Update pages/docs/concepts/asyncapi-document/payload-schema.md
kakabisht Sep 4, 2023
42fbf4d
Update pages/docs/concepts/asyncapi-document/_section.md
kakabisht Sep 4, 2023
7a6b76d
Update pages/docs/concepts/asyncapi-document/payload-schema.md
kakabisht Sep 4, 2023
893d3c0
Update pages/docs/concepts/asyncapi-document/payload-schema.md
kakabisht Sep 4, 2023
a4ed37d
partial SME feedback
kakabisht Sep 4, 2023
5d9b0c2
Merge branch 'docs-payload-schema' of https://github.com/kakabisht/we…
kakabisht Sep 4, 2023
c499ac7
fix weights
kakabisht Sep 14, 2023
4b55f6a
Partial SME feedback
kakabisht Sep 15, 2023
668a36d
Complete SME feedback
kakabisht Oct 10, 2023
fa9eb79
fixing the image
kakabisht Nov 16, 2023
c337d2a
Fix for JSON Schema
kakabisht Dec 2, 2023
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
---
118 changes: 118 additions & 0 deletions pages/docs/concepts/asyncapi document/payload-schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
title: Payload Schema
weight: 270
---

The payload schema defines a message's format, data types, and properties to ensure that the payload follows a specific structure and data format.

You should use AsyncAPI Schema to handle complex messages and structured data models. Enabling consumers to gain insights into the structure and data types of the payload.

## Define JSON schema

Define the JSON schema for the message payload. You can choose one of the following methods:

- Inline reference: Define the JSON schema within the message payload property.
- Remote reference: Specify the schema using an absolute remote endpoint, such as `$ref: 'https://schemas.example.com/user'`.
- Local file reference: Specify the schema using a relative reference, such as `$ref: './user-signedup.json'`.

The diagram below describes how to use complex payloads.

```mermaid
graph TD
A[Message]
B[Payload]
C[JSON Schema]

style A fill:#47BCEE,stroke:#47BCEE;
style B fill:#47BCEE,stroke:#47BCEE;
style C fill:#47BCEE,stroke:#47BCEE;

A -->|references| B
B -->|local file reference| C
B -->|remote reference| C
B -->|Inline reference| C
```

Here is an example of an AsyncAPI specification file that uses the local reference method:

```yaml
channels:
exampleChannel:
address: exampleChannel
messages:
publish.message:
payload:
$ref: path/to/user-create.json
operations:
exampleChannel.publish:
action: receive
channel:
$ref: '#/channels/exampleChannel'
messages:
- $ref: '#/channels/exampleChannel/messages/publish.message'
```

Create a separate JSON schema file with a `.json` extension. The file should define the structure of the message payload. Here is an example of a JSON schema file for the user record type:

```json
{
"type": "object",
"properties": {
"username": {
"type": "string",
"description": "The username of the sender"
},
"email": {
"type": "string",
"format": "email",
"description": "The email of the sender"
},
"message": {
"type": "string",
"description": "The content of the message"
}
}
}
```

## Attach examples

Although optional, it is highly recommended to attach examples to the AsyncAPI specification. You can use JSON or YAML format for binary encodings. Attach the examples to the examples property within the message payload definition. Here is an example,

```yaml
examples:
- name: SimpleSignup
summary: A simple UserSignup example message
- payload:
user:
name: Demo
email: demo@demo.io
```

## Reuse schema

To reuse a schema in your AsyncAPI specification, define it in the `components/schemas` section and reference it using the `$ref` keyword. Using `$ref` helps to avoid duplication and ensures consistency. Here's an example of reusing a schema from components in AsyncAPI.

```yaml
components:
messages:
SimpleSignup:
name: SimpleSignup
contentType: application/json
payload:
$ref: '#/components/schemas/SimpleSignup'
examples:
name: example
payload:
SimpleSignup:
name: Demo
email: demo@demo.io
schemas:
SimpleSignup:
type: object
properties:
name:
type: string
email:
type: string
```