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

Fix generator template broken link #1118

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
542889e
add asyncapiDocument and asyncapiString documentation
Florence-Njeri Jul 20, 2022
006f2d3
merge from origin master
Florence-Njeri Nov 15, 2022
395db74
delete stale async-api file
Florence-Njeri Nov 15, 2022
d377166
Merge branch 'asyncapi:master' into master
Florence-Njeri Nov 22, 2022
cbe4fd0
Merge branch 'asyncapi:master' into master
Florence-Njeri Nov 24, 2022
b2aa2c7
Merge branch 'asyncapi:master' into master
Florence-Njeri Dec 20, 2022
f0816de
Merge branch 'asyncapi:master' into master
Florence-Njeri Dec 20, 2022
25d69b8
Merge branch 'asyncapi:master' into master
Florence-Njeri Dec 20, 2022
fefb8f9
Merge branch 'asyncapi:master' into master
Florence-Njeri Jan 22, 2023
36ecdfc
Merge branch 'asyncapi:master' into master
Florence-Njeri Feb 6, 2023
75610d5
Merge branch 'asyncapi:master' into master
Florence-Njeri May 16, 2023
a0afe6d
Merge branch 'asyncapi:master' into master
Florence-Njeri Jun 1, 2023
f40eee8
Merge branch 'asyncapi:master' into master
Florence-Njeri Jun 27, 2023
f714a20
Merge branch 'asyncapi:master' into master
Florence-Njeri Aug 1, 2023
e345a15
Merge branch 'asyncapi:master' into master
Florence-Njeri Nov 30, 2023
badaf1b
Merge branch 'asyncapi:master' into master
Florence-Njeri Dec 8, 2023
3fdf810
draft 1
Florence-Njeri Dec 14, 2023
3690a80
add tutorial to generate models using modelina
Florence-Njeri Dec 23, 2023
bd6d6ee
editorial
Florence-Njeri Dec 23, 2023
a80b468
Merge branch 'master' into model-generation-docs
Florence-Njeri Jan 4, 2024
ce963b7
Apply suggestions from code review
Florence-Njeri Jan 4, 2024
46e6722
PR feedback from Lukasz
Florence-Njeri Jan 9, 2024
d8525dc
Merge branch 'master' into model-generation-docs
Florence-Njeri Jan 9, 2024
0678e07
merge
Florence-Njeri Jan 9, 2024
bf4ed52
more editorial from Lukasz feedback
Florence-Njeri Jan 9, 2024
27e5c31
Update docs/model-generation.md
Florence-Njeri Jan 23, 2024
48c2e9f
Merge branch 'master' into model-generation-docs
Florence-Njeri Jan 23, 2024
31efc78
editorial from Lukasz
Florence-Njeri Jan 24, 2024
838282f
fix breaking link in the generator template tutorial
Florence-Njeri Feb 15, 2024
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
1 change: 1 addition & 0 deletions .github/workflows/update-docs-in-website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
- name: Copy generator folder from Current Repo to Another
working-directory: ./website
run: |
rm -r ./pages/docs/tools/generator
mkdir -p ./pages/docs/tools/generator
rm ../generator/docs/README.md
rm -r ../generator/docs/jsdoc2md-handlebars
Expand Down
2 changes: 1 addition & 1 deletion docs/generator-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -572,4 +572,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.
3 changes: 3 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ weight: 10

The AsyncAPI generator is a tool that generates anything you want using the **[AsyncAPI Document](generator/asyncapi-document)** and **[Template](generator/template)** that are supplied as inputs to the AsyncAPI CLI. The generator was built with extensibility in mind; you can use the generator to generate anything you want, provided that it can be defined in a template, such as code, diagrams, markdown files, microservices, and applications. A number of [community-maintained templates](https://github.com/search?q=topic%3Aasyncapi+topic%3Agenerator+topic%3Atemplate) are now available for immediate usage.

> **Note:**
> If your primary objective is to generate models/classes for your event-driven architecture apps, use [AsyncAPI Modelina](/docs/tools/generator/model-generation), which is supported in the AsyncAPI CLI, instead of using the AsyncAPI Generator. Modelina is specifically designed for model generation and provides utilities for working with the AsyncAPI document.

### Generator use cases
- Generation of interactive and understandable API documentation
- Generation of APIs' client libraries
Expand Down
2 changes: 1 addition & 1 deletion docs/installation-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ You can install in Linux by using `dpkg`, a package manager for debian:
For further installation instructions for different operating systems, read the [AsyncAPI CLI documentation](https://github.com/asyncapi/cli#installation).

> **Remember:**
> Each [community-developed template](https://github.com/search?q=topic%3Aasyncapi+topic%3Agenerator+topic%3Atemplate) is dependent on a certain version of the generator for it to work correctly. Before you install the generator CLI, check the template's `package.json` for the version of the generator CLI your template is compatible with. Read the [versioning docs](versioning) to learn why it's important to use certain generator versions with your templates.
> Each [community-developed template](https://github.com/search?q=topic%3Aasyncapi+topic%3Agenerator+topic%3Atemplate) is dependent on a certain version of the generator for it to work correctly. Before you install the AsyncAPI CLI, check the template's `package.json` for the version of the AsyncAPI CLI your template is compatible with. Read the [versioning docs](versioning) to learn why it's important to use certain generator versions with your templates.

### Update AsyncAPI CLI
There are several reasons why you might want to update your generator version:
Expand Down
84 changes: 84 additions & 0 deletions docs/model-generation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
title: "Adding models generation in template"
weight: 200
---

This guide will walk you through the process of enabling models/types generation in a template by using [Modelina](https://www.asyncapi.com/tools/modelina).

Modelina is an AsyncAPI library designed for generating data models using inputs such as [AsyncAPI](generator/asyncapi-document), OpenAPI, or JSON schema inputs. Its functionality revolves around creating data models from the provided AsyncAPI document and the model template, which defines message payloads. It is better to use Modelina in your template to handle model generation rather than providing custom templates.

You can integrate the work shown in this guide into a template by following the [tutorial about creating a template](https://www.asyncapi.com/docs/tools/generator/generator-template).

In this guide, you'll learn how to use Modelina in a template code to enable support for Python data model generation.

## Add Modelina dependency

Install Modelina in your project using npm: `npm install --save @asyncapi/modelina`.

Ensure your template's `package.json` file now contains Modelina pointing to its latest version:

```json
"dependencies": {
// ...
"@asyncapi/modelina": "^2.0.5"
// ...
}
```

## Create a models.js file

Create a new directory in the **template** directory named **src/models** and create a **models.js** file within it. In the **models.js** file, add the following code:

```python
// 1
import { File } from '@asyncapi/generator-react-sdk';
// 2
import { PythonGenerator, FormatHelpers } from '@asyncapi/modelina';

/**
* @typedef RenderArgument
* @type {object}
* @property {AsyncAPIDocument} asyncapi document object received from the generator.
*/

/**
* Render all schema models
* @param {RenderArgument} param0
* @returns
*/
// 3
export default async function schemaRender({ asyncapi }) {
// 4
const pythonGenerator = new PythonGenerator();
// 5
const models = await pythonGenerator.generate(asyncapi);
// 6
const files = [];
// 7
for (const model of models) {
// 8
const modelFileName = `${FormatHelpers.toPascalCase(model.modelName)}.py`;
// 9
files.push(<File name={modelFileName}>{model.result}</File>);
}
return files;
}
```

Let's break it down. The code snippet above does the following:

1. The `File` component from the [generator react SDK](https://github.com/asyncapi/generator-react-sdk) is needed to handle the further rendering of generated models into files.
2. The `PythonGenerator` generator is the core needed for model generation. Additionally, you can import [FormatHelpers](https://github.com/asyncapi/modelina/blob/master/src/helpers/FormatHelpers.ts) that provides a set of helpers making it easier to modify model names to match your required case.
3. You can change the name `schemaRender` to anything else like `modelRenderer`. More importantly, this must be an `async` function and a default export. This function is invoked during generation process and should contain the logic behind models generation.
4. First, create an instance of the `PythonGenerator` model generator. If you decide to use present functionality from Modelina, you need to pass your presets here during instance creation.
5. The actual model generation is one line of code, and as a result you get an array of models that later you need to turn into files.
6. You need to define an array that must be returned from `schemaRender` function. The array must contain React components, and in this case, the `<File>` component.
7. Iterate over generated models and use their content to create proper definitions of `<File>` components.
8. Notice how using Modelina helpers, in this case the `toPascalCase` function, let's you make sure that the filename of your model follows specific case pattern.
9. Each component must be added into the `files` array that you later return from the default function. Notice the definition of the `<File>` component that enables you to provide the name of resulting file and the content of the model. Notice also `model.result` that shows that initially generated array with models did not contain raw models content but a set of output objects that contain not only `result` but also other info, like for example `modelName`.

With such a model template that uses Modelina, as a result of generation process you would receive a set of model files in `$OUTPUT_DIR/src/models` directory.

## Conclusion

Modelina provides a flexible and powerful way to generate data models from AsyncAPI, OpenAPI, or JSON Schema documents. By integrating Modelina you can much faster enable models generation in your template.
4 changes: 3 additions & 1 deletion docs/template-development.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
title: "Template development"
weight: 80
---
> **Note**
> It is advised against attempting to manually template types and models from scratch using the AsyncAPI templating engines such as Nunjucks and React render engines. Instead, it is recommended to use [AsyncAPI Modelina](/docs/tools/generator/model-generation) a dedicated library for model generation.

## Minimum template requirements

Expand Down Expand Up @@ -130,7 +132,7 @@ Newer:
<Text>Version is: **{params.version || asyncapi.info.version()}**</Text>
```

Now that you have added all the configuration options, you can start the generation process using the generator CLI. You can pass these parameters via the CLI: `--param name=value or -p name=value`.
Now that you have added all the configuration options, you can start the generation process using the AsyncAPI CLI. You can pass these parameters via the CLI: `--param name=value or -p name=value`.
The above configuration helps template users override the existing version with a new version on the command line. (Example: `-p version=2.0.0`)

## Hooks
Expand Down
2 changes: 1 addition & 1 deletion docs/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ You can store template projects on a local drive or as a `git` repository during

1. Template is provided as input to the **Generator**.
2. **asyncapi** is the original AsyncAPI document injected into your template file by default.
3. **params** are the parameters you pass to the generator CLI. Later, you can also pass these **params** further to other components.
3. **params** are the parameters you pass to the AsyncAPI CLI. Later, you can also pass these **params** further to other components.
4. The generator passes both the original **asyncapi**, the original AsyncAPI document, and the **params** to the **Template Context**.
5. Concurrently, the generator passes **Template files** to the **Render engine** as well. AsyncAPI uses two render engines — _react_ and _nunjucks_.
6. Once the Render Engine receives both the Template Files and the Template Context, it injects all the dynamic values into your react or nunjucks engine, based on the Template Files using the Template Context.
Expand Down
6 changes: 3 additions & 3 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ weight: 30
---

There are two ways to use the generator:
- [Generator CLI](#generator-cli)
- [AsyncAPI CLI](#generator-cli)
- [Generator library](#using-as-a-modulepackage)

## Generator CLI
## AsyncAPI CLI
```bash
Usage: asyncapi generate fromTemplate <asyncapi> <template> [<options>]

Expand Down Expand Up @@ -43,7 +43,7 @@ npm install <folder>

### Global templates installed with `yarn` or `npm`

You can preinstall templates globally before installing the generator CLI. The generator first tries to locate the template in local dependencies; if absent it checks where the global generator packages are installed.
You can preinstall templates globally before installing the [AsyncAPI CLI](https://www.asyncapi.com/docs/tools/cli). The generator first tries to locate the template in local dependencies; if absent it checks where the global generator packages are installed.

```bash
npm install -g @asyncapi/[email protected]
Expand Down
Loading