-
-
Notifications
You must be signed in to change notification settings - Fork 303
docs: update generator-template.md for AsyncAPI v3 parser usage #1551
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
fdef813
f62c7f7
6ba91cb
e2ff0a0
72403b1
6e84398
7bc3c93
5b18b6e
86931d5
742d915
820d777
ecaac8c
33cba12
cbeaac3
08b085b
656706b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -42,42 +42,64 @@ Before you begin, make sure you have the following set up: | |||||||||||||||||||||||||||||||||||
There is a list of [community maintained templates](https://www.asyncapi.com/docs/tools/generator/template#generator-templates-list), but what if you do not find what you need? In that case, you'll create a user-defined template that generates custom output from the generator. | ||||||||||||||||||||||||||||||||||||
Before you create the template, you'll need to have an [AsyncAPI document](https://www.asyncapi.com/docs/tools/generator/asyncapi-document) that defines the properties you want to use in your template to test against. In this tutorial, you'll use the following template saved in the **test/fixtures/asyncapi.yml** file in your template project directory. | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
``` yml | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
asyncapi: 2.6.0 | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
```yaml | ||||||||||||||||||||||||||||||||||||
asyncapi: 3.0.0 | ||||||||||||||||||||||||||||||||||||
info: | ||||||||||||||||||||||||||||||||||||
title: Temperature Service | ||||||||||||||||||||||||||||||||||||
version: 1.0.0 | ||||||||||||||||||||||||||||||||||||
description: This service is in charge of processing all the events related to temperature. | ||||||||||||||||||||||||||||||||||||
description: Service that emits temperature changes from a bedroom sensor. | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
servers: | ||||||||||||||||||||||||||||||||||||
production: | ||||||||||||||||||||||||||||||||||||
host: broker.example.com | ||||||||||||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||
dev: | ||||||||||||||||||||||||||||||||||||
url: test.mosquitto.org #in case you're using local mosquitto instance, change this value to localhost. | ||||||||||||||||||||||||||||||||||||
protocol: mqtt | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
channels: | ||||||||||||||||||||||||||||||||||||
temperature/changed: | ||||||||||||||||||||||||||||||||||||
description: Updates the bedroom temperature in the database when the temperatures drops or goes up. | ||||||||||||||||||||||||||||||||||||
publish: | ||||||||||||||||||||||||||||||||||||
operationId: temperatureChange | ||||||||||||||||||||||||||||||||||||
message: | ||||||||||||||||||||||||||||||||||||
description: Message that is being sent when the temperature in the bedroom changes. | ||||||||||||||||||||||||||||||||||||
temperatureChanged: | ||||||||||||||||||||||||||||||||||||
address: temperature/changed | ||||||||||||||||||||||||||||||||||||
messages: | ||||||||||||||||||||||||||||||||||||
temperatureChange: | ||||||||||||||||||||||||||||||||||||
description: Message sent when the temperature in the bedroom changes. | ||||||||||||||||||||||||||||||||||||
payload: | ||||||||||||||||||||||||||||||||||||
type: object | ||||||||||||||||||||||||||||||||||||
additionalProperties: false | ||||||||||||||||||||||||||||||||||||
properties: | ||||||||||||||||||||||||||||||||||||
temperatureId: | ||||||||||||||||||||||||||||||||||||
type: string | ||||||||||||||||||||||||||||||||||||
$ref: '#/components/schemas/Temperature' | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
operations: | ||||||||||||||||||||||||||||||||||||
sendTemperatureChanged: | ||||||||||||||||||||||||||||||||||||
action: send | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in AsyncAPI v2 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're absolutely right — with AsyncAPI v3, the semantics around publish and subscribe have flipped compared to v2. I’ve updated the document to reflect this change correctly by using the top-level operations with action: send, aligning with how v3 represents a message sent from the application. Let me know if you'd prefer a more explicit explanation about this change added in the tutorial! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just push the change, I think it is not visible yet There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah sure! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as I wrote, it should be |
||||||||||||||||||||||||||||||||||||
summary: Temperature changes are pushed to the broker | ||||||||||||||||||||||||||||||||||||
channel: | ||||||||||||||||||||||||||||||||||||
$ref: '#/channels/temperatureChanged' | ||||||||||||||||||||||||||||||||||||
derberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
components: | ||||||||||||||||||||||||||||||||||||
schemas: | ||||||||||||||||||||||||||||||||||||
temperatureId: | ||||||||||||||||||||||||||||||||||||
Temperature: | ||||||||||||||||||||||||||||||||||||
type: object | ||||||||||||||||||||||||||||||||||||
additionalProperties: false | ||||||||||||||||||||||||||||||||||||
properties: | ||||||||||||||||||||||||||||||||||||
temperatureId: | ||||||||||||||||||||||||||||||||||||
value: | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why payload had to change? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was updated to reflect how reusable components are recommended in AsyncAPI v3. By moving the payload to a reusable component (#/components/schemas/Temperature), we promote schema reusability and cleaner code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but I'm not asking about reusability, but why the payload structure changed entirely There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The actual data structure hasn't changed functionally, just refactored into the components/schemas section for cleaner organization. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, you changed it, it was |
||||||||||||||||||||||||||||||||||||
type: number | ||||||||||||||||||||||||||||||||||||
unit: | ||||||||||||||||||||||||||||||||||||
type: string | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove empty space |
||||||||||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||||||||||
> 🆕 This document uses the AsyncAPI 3.0.0 structure. Notable changes include `operations` now being top-level and the use of `address:` in `channels` instead of nested publish/subscribe. | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
## Handling Diagnostics (Warnings) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
When using the latest AsyncAPI parser, it's important to handle not just errors but also diagnostics (warnings). These help identify non-critical issues, such as missing recommended fields like `license`, `contact`, or outdated spec versions. | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
```ts | ||||||||||||||||||||||||||||||||||||
const { parseFromFile } = require('@asyncapi/parser'); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
const result = await parseFromFile('example-asyncapi.yaml'); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
if (result.diagnostics && result.diagnostics.length > 0) { | ||||||||||||||||||||||||||||||||||||
console.warn('⚠️ Found diagnostics:'); | ||||||||||||||||||||||||||||||||||||
console.dir(result.diagnostics, { depth: null }); | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Wrap the - const { parseFromFile } = require('@asyncapi/parser');
- const result = await parseFromFile('example-asyncapi.yaml');
- if (result.diagnostics && result.diagnostics.length > 0) {
- console.warn('⚠️ Found diagnostics:');
- console.dir(result.diagnostics, { depth: null });
- }
+ const { parseFromFile } = require('@asyncapi/parser');
+ (async () => {
+ const result = await parseFromFile('example-asyncapi.yaml');
+ if (result.diagnostics && result.diagnostics.length > 0) {
+ console.warn('⚠️ Found diagnostics:');
+ console.dir(result.diagnostics, { depth: null });
+ }
+ })(); 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
## Overview of steps | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
@@ -649,4 +671,4 @@ Great job completing this tutorial! You have learnt how to use an AsyncAPI file | |||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
If you want to tinker with a completed template and see what it would look like in production, check out the [Paho-MQTT template](https://github.com/derberg/python-mqtt-client-template/tree/v1.0.0). You can also check out the accompanying [article about creating MQTT client code](https://www.brainfart.dev/blog/asyncapi-codegen-python). | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
You can also check out the [MQTT beginners guide](https://medium.com/python-point/mqtt-basics-with-python-examples-7c758e605d4) tutorial to learn more about asynchronous messaging using MQTT. | ||||||||||||||||||||||||||||||||||||
You can also check out the [MQTT beginners guide](https://medium.com/python-point/mqtt-basics-with-python-examples-7c758e605d4) tutorial to learn more about asynchronous messaging using MQTT. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason why it was added?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm... Thanks for pointing it out! I added the production: server to better illustrate a more realistic scenario where both a development (dev) and production environment exist — which mirrors many actual use cases. It also supports the explanation later in the tutorial where we discuss dynamically switching brokers using the --param server=dev option. Let me know if you’d prefer I trim it down or clarify it further!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah trim it down, this tutorial do not teach how proper asyncapi document should look like but focus is on extracting data from it and defining a template
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay sure triming down!