Convert JHipster JDL to AsyncAPI v2/v3 with ZenWave SDK

Ivan Garcia Sainz-Aja

Ivan Garcia Sainz-Aja

·3 min read

Generating AsyncAPI definition files from JDL with ZenWaveSDK

Writing YAML by hand is no fun, but you can simplify the process of writing AsyncAPI definition files by using a Domain Specific Language (DSL).

JHipster Domain Language (JDL) is a Domain Specific Language (DSL) used to define the domain model of a web application. With JDL, you can describe the entities, relationships, and constraints of your system in a concise and readable way.

Zenwave SDK is a set of tools to generate (and reverse engineering) code from JDL and API-First models like AsyncAPI and OpenAPI.

Thanks to ZenWave SDK, you can convert JDL models into AsyncAPI definition files. This can save time and effort in the development process while ensuring that your APIs follow best practices and standards.

JDL Example

1@aggregate
2entity Customer {
3    username String required minlength(3) maxlength(250)
4    password String required minlength(3) maxlength(250)
5    email String required minlength(3) maxlength(250)
6    firstName String required minlength(3) maxlength(250)
7    lastName String required minlength(3) maxlength(250)
8}
9entity Address {
10    street String
11    city String
12    country String
13    zipCode String
14}
15
16relationship OneToOne {
17    Customer{address} to Address{customer}
18}

Generating AsyncAPI definition files from JDL with ZenWaveSDK

See JDL To AsyncAPI Generator for a complete list of options and GitHub repository for install instructions.

Because JDL can only describe static aspects of your models and doesn't cover dynamic behavior, ZenWave SDK can only infer CRUD operations from your entities, generating:

  • One channel for each entity for both publishing Domain Events and subscribing to Commands/Requests.
  • Messages and payloads for each entity Create/Update/Delete events (AVRO and AsyncAPI schema)
1jbang zw -p io.zenwave360.sdk.plugins.JDLToAsyncAPIPlugin \
2    includeCommands=false \
3    specFile=src/main/resources/model/entities-model.jdl \
4    idType=integer \
5    idTypeFormat=int64 \
6    annotations=aggregate \
7    payloadStyle=event \
8    targetFile=src/main/resources/model/asyncapi.yml

You can choose to generate only Events or Commands using includeEvents (default: true) and includeCommands (default: false) to filter which channels you want to include in your AsyncAPI definition file.

You can also filter which entities you want to include Messages for in your AsyncAPI definition file using: entities, skipEntities, annotations, and skipForAnnotations.

UPDATE:

ZenWave SDK version 1.0.6 now supports generating AsyncAPI v3 format. Use asyncapiVersion=v3 as:

1jbang zw -p io.zenwave360.sdk.plugins.JDLToAsyncAPIPlugin \
2    includeCommands=false \
3    specFile=src/main/resources/model/entities-model.jdl \
4    idType=integer \
5    idTypeFormat=int64 \
6    annotations=aggregate \
7    payloadStyle=event \
8    asyncapiVersion=v3 \
9    targetFile=src/main/resources/model/asyncapi.yml

Supported Schema Formats and Message Styles

You can generate AsyncAPI definition files with the following options:

  • Supported Schema Formats: AVRO and AsyncAPI schema
  • Supported Payload Styles: "Entity State Transfer" and "Domain Event" (for Create/Update/Delete events):
    • State Transfer message contains the entire state of the aggregate, so a consumer does not need to make additional calls.
    • Domain Event Messages contain information about the event and interesting portions of the underlying aggregate, but not the entire state of the aggregate.

By using JDL to define your domain model and ZenWave SDK to convert it into an AsyncAPI definition file, you can simplify the process of designing and documenting your APIs. This can improve your APIs' overall quality and consistency while reducing errors and inconsistencies.


Originally published at https://zenwave360.github.io.