Generators

Generators are classes that are used to render models for a given language.

Generator's options

For Modelina, there exist 3 types of options for the generation process.

  1. Default options, are the default options the rest overwrite.
  2. Generator options, are used as the baseline that is by default used for each model render.
  3. Render options, are the last resort to specialize options for the individual rendering of a model, that overwrite the generator options.

Generator options are passed as the first argument to the generator's constructor. Check the example:

1const generator = new TypeScriptGenerator({ ...options });

Render options are passed as the first argument to the generator's render function. Check the example:

1const generator = ...
2const model = ...
3const inputModel = ...
4generator.render(model, inputModel, { ...options });

Default generator options (common to all generators) are as follows:

OptionTypeDescriptionDefault value
indentationObjectOptions for indentation (example).-
indentation.typeStringType of indentation. Its value can be either SPACES or TABS and are typed by IndentationTypesSPACES
indentation.sizeStringSize of indentation.2
defaultPresetObjectDefault preset for generator. For more information, read customization document.Implemented by generator
presetsArrayArray contains presets. For more information, read customization document.[]

In addition, generators take additional options when calling their renderCompleteModel(input, options) functions. This allows the caller to specify additional options when generating a multi-file model from the input with cross dependencies.

Below is a list of additional options available for a given generator.

TypeScript

Generator options

OptionTypeDescriptionDefault value
renderTypesBooleanRender signature for types.true
mapTypeStringIt indicates which mapping type should be rendered for the object type. Its value can be one of map, record or indexedObject.map
modelTypeStringIt indicates which model type should be rendered for the object type. Its value can be either interface or class.class
enumTypeStringIt indicates which type should be rendered for the enum type. Its value can be either union or enum.enum
namingConventionObjectOptions for naming conventions.-
namingConvention.typeFunctionA function that returns the format of the type.Returns pascal cased name, and ensures that reserved keywords are never rendered_
namingConvention.propertyFunctionA function that returns the format of the property.Returns camel cased name, and ensures that names of properties does not clash against reserved keywords for TS, as well as JS to ensure painless transpilation

Render complete model options

OptionTypeDescriptionDefault value
moduleSystem'ESM' | 'CJS'Which module system the generated files should use (import or require)'CJS'
exportType'default' | 'named'Whether the exports should be default or named exports'default'

Java

Generator options

OptionTypeDescriptionDefault value
collectionTypeStringIt indicates with which signature should be rendered the array type. Its value can be either List (List<{type}>) or Array ({type}[]).List
namingConventionObjectOptions for naming conventions.-
namingConvention.typeFunctionA function that returns the format of the type.Returns pascal cased name, and ensures that reserved keywords are never rendered_
namingConvention.propertyFunctionA function that returns the format of the property.Returns camel cased name, and ensures that names of properties does not clash against reserved keywords

Render complete model options

OptionTypeDescriptionDefault value
packageNamestringThe package name to generate the models under[required]

JavaScript

Generator options

OptionTypeDescriptionDefault value
namingConventionObjectOptions for naming conventions.-
namingConvention.typeFunctionA function that returns the format of the type.Returns pascal cased name, and ensures that reserved keywords are never rendered
namingConvention.propertyFunctionA function that returns the format of the property.Returns camel cased name, and ensures that names of properties does not clash against reserved keywords

Render complete model options

OptionTypeDescriptionDefault value
moduleSystem'ESM' | 'CJS'Which module system the generated files should use (import or require)'CJS'

Go

Generator options

OptionTypeDescriptionDefault value
namingConventionObjectOptions for naming conventions.-
namingConvention.typeFunctionA function that returns the format of the type.Returns pascal cased name
namingConvention.fieldFunctionA function that returns the format of the field.Returns pascal cased name

Render complete model options

OptionTypeDescriptionDefault value
packageNamestringThe package name to generate the models under[required]

C#

Generator options

OptionTypeDescriptionDefault value
namingConventionObjectOptions for naming conventions.-
namingConvention.typeFunctionA function that returns the format of the type.Returns pascal cased name, and ensures that reserved keywords are never rendered_
namingConvention.propertyFunctionA function that returns the format of the property.Returns camel cased name, and ensures that names of properties does not clash against reserved keywords

Render complete model options

OptionTypeDescriptionDefault value
namespacestringThe namespace to generate the models under[required]

Custom generator

The minimum set of required actions to create a new generator are:

  • Source code must be included in generators folder.
  • Must extend the abstract AbstractGenerator class,
  • Must define Preset's shape for the language,
  • Must define language options by passing an interface describing additional options to the first generic argument of AbstractGenerator. The interface must also be extended by CommonGeneratorOptions interface,
  • Must define default options as static class's field, which must be extended by defaultGeneratorOptions,
  • Default options must include defaultPreset property,
  • Must implement render function,
  • Must define Renderers classes for available model types in a given language. Renderer is an instance of the class with common helper functions to render appropriate model type and must be extended by AbstractRenderer class - example.

Check the generator implementation for TypeScript language to see how it should look like.

If you created a generator then you can contribute it to the AsyncAPI Model SDK and it will become the official supported generator.