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: add complex payload schema in AsyncAPI Document #1889

Closed
Closed
Show file tree
Hide file tree
Changes from 7 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
kakabisht marked this conversation as resolved.
Show resolved Hide resolved
weight: 0
---
110 changes: 110 additions & 0 deletions pages/docs/concepts/asyncapi-document/payload-schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
---
title: Payload Schema
weight: 239
kakabisht marked this conversation as resolved.
Show resolved Hide resolved
---

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

In the AsyncAPI specification, you can use schema languages such as JSON or Avro Schema to create the payload schema. Enabling consumers to gain insights into the structure and data types of the payload.
kakabisht marked this conversation as resolved.
Show resolved Hide resolved

You should use Avro schemas to handle complex messages and structured data models. Avro schemas support complex types like arrays and records. You can create Avro schemas by writing the JSON representation or using tools like Avro4s UI to generate the schema from valid JSON.
kakabisht marked this conversation as resolved.
Show resolved Hide resolved
kakabisht marked this conversation as resolved.
Show resolved Hide resolved

Use npm or yarn to install the `@asyncapi/avro-schema-parser` package to enable Avro schema parsing in AsyncAPI.

## Define Avro schema

Define the Avro schema for the message payload. You can choose one of the following methods,
kakabisht marked this conversation as resolved.
Show resolved Hide resolved

- Embedded notation: Define the Avro 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 reference: Specify the schema using a relative reference, such as `$ref: './user-signedup.avsc#/User'`.
kakabisht marked this conversation as resolved.
Show resolved Hide resolved

The diagram below defines the AsyncAPI specification file using the local reference method.

```mermaid
graph LR
style A fill:#47BCEE, stroke:#333, stroke-width:2px;
style C fill:#47BCEE, stroke:#333, stroke-width:2px;
style D fill:#47BCEE, stroke:#333, stroke-width:2px;
style B fill:#47BCEE, stroke:#333, stroke-width:2px;
style E fill:#47BCEE, stroke:#333, stroke-width:2px;

A[Define Avro Schema]
B[Local Reference]
C[Avro Schema File]
D[Message Payload]
E[AsyncAPI Specification File]

A -->|One of the following methods:| B
B -->|Specify schema using relative reference| D
E -->|Uses local reference method| C
C-->|Defines structure of message payload| D
```
kakabisht marked this conversation as resolved.
Show resolved Hide resolved

Here is an example of an AsyncAPI specification file that uses the local reference method,
kakabisht marked this conversation as resolved.
Show resolved Hide resolved

```yaml
messageId: userSignup
name: UserSignup
title: User signup
summary: Action to sign a user up.
description: A longer description
tags:
- name: user
- name: signup
payload:
schemaFormat: application/vnd.apache.avro+json;version=1.9.0
schema:
$ref: path/to/user-create.avsc#/UserCreate
kakabisht marked this conversation as resolved.
Show resolved Hide resolved
```

Create a separate Avro schema file with a .avsc extension. The file should define the structure of the message payload. Here is an example of an Avro schema file for the User record type:
kakabisht marked this conversation as resolved.
Show resolved Hide resolved

```yaml
namespace: UserCreate.avro
type: record
name: user
fields:
- name: fullName
type: string
- name: email
type: string
```
kakabisht marked this conversation as resolved.
Show resolved Hide resolved

## 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, like Avro. 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:
fullName: Demo
email: [email protected]
```

You can use a Schema Registry to separate the Avro schema from the message payload, making it easier to manage schema compatibility.

## 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.
kakabisht marked this conversation as resolved.
Show resolved Hide resolved

```yaml
channels:
user/signedup:
subscribe:
message:
payload:
$ref: '#/components/schemas/User'
components:
schemas:
User:
type: object
properties:
fullName:
type: string
email:
type: string
```