CLI Plugin
TypeScript's metadata reflection system has several limitations which make it impossible to, for instance, determine what properties a class consists of or recognize whether a given property is optional or required. However, some of these constraints can be addressed at compilation time. Nest provides a plugin that enhances the TypeScript compilation process to reduce the amount of boilerplate code required.
info Hint This plugin is opt-in. If you prefer, you can declare all decorators manually, or only specific decorators where you need them.
Overview
The Swagger plugin will automatically:
- annotate all DTO properties with
@ApiPropertyunless@ApiHidePropertyis used - set the
requiredproperty depending on the question mark (e.g.name?: stringwill setrequired: false) - set the
typeorenumproperty depending on the type (supports arrays as well) - set the
defaultproperty based on the assigned default value - set several validation rules based on
class-validatordecorators (ifclassValidatorShimset totrue) - add a response decorator to every endpoint with a proper status and
type(response model) - generate descriptions for properties and endpoints based on comments (if
introspectCommentsset totrue) - generate example values for properties based on comments (if
introspectCommentsset totrue)
Please, note that your filenames must have one of the following suffixes: ['.dto.ts', '.entity.ts'] (e.g., create-user.dto.ts) in order to be analysed by the plugin.
If you are using a different suffix, you can adjust the plugin's behavior by specifying the dtoFileNameSuffix option (see below).
Previously, if you wanted to provide an interactive experience with the Swagger UI,
you had to duplicate a lot of code to let the package know how your models/components should be declared in the specification. For example, you could define a simple CreateUserDto class as follows:
While not a significant issue with medium-sized projects, it becomes verbose & hard to maintain once you have a large set of classes.
By enabling the Swagger plugin, the above class definition can be declared simply:
info Note The Swagger plugin will derive the @ApiProperty() annotations from the TypeScript types and class-validator decorators. This helps in clearly describing your API for the generated Swagger UI documentation. However, the validation at runtime would still be handled by class-validator decorators. So, it is required to continue using validators like
IsEmail(),IsNumber(), etc.
Hence, if you intend to rely on automatic annotations for generating documentations and still wish for runtime validations, then the class-validator decorators are still necessary.
info Hint When using mapped types utilities (like
PartialType) in DTOs import them from@nestjs/swaggerinstead of@nestjs/mapped-typesfor the plugin to pick up the schema.
The plugin adds appropriate decorators on the fly based on the Abstract Syntax Tree. Thus you won't have to struggle with @ApiProperty decorators scattered throughout the code.
info Hint The plugin will automatically generate any missing swagger properties, but if you need to override them, you simply set them explicitly via
@ApiProperty().
Comments introspection
With the comments introspection feature enabled, CLI plugin will generate descriptions and example values for properties based on comments.
For example, given an example roles property:
You must duplicate both description and example values. With introspectComments enabled, the CLI plugin can extract these comments and automatically provide descriptions (and examples, if defined) for properties. Now, the above property can be declared simply as follows:
There are dtoKeyOfComment and controllerKeyOfComment plugin options available for customizing how the plugin assigns values to the ApiProperty and ApiOperation decorators, respectively. See the example below:
This is equivalent to the following instruction:
info Hint For models, the same logic applies but is used with the
ApiPropertydecorator instead.
For controllers, you can provide not only a summary but also a description (remarks), tags (such as @deprecated), and response examples, like this:
Using the CLI plugin
To enable the plugin, open nest-cli.json (if you use Nest CLI) and add the following plugins configuration:
You can use the options property to customize the behavior of the plugin.
The options property has to fulfill the following interface:
Make sure to delete the /dist folder and rebuild your application whenever plugin options are updated.
If you don't use the CLI but instead have a custom webpack configuration, you can use this plugin in combination with ts-loader:
SWC builder
For standard setups (non-monorepo), to use CLI Plugins with the SWC builder, you need to enable type checking, as described here.
For monorepo setups, follow the instructions here.
Now, the serialized metadata file must be loaded by the SwaggerModule#loadPluginMetadata method, as shown below:
Integration with ts-jest (e2e tests)
To run e2e tests, ts-jest compiles your source code files on the fly, in memory. This means, it doesn't use Nest CLI compiler and does not apply any plugins or perform AST transformations.
To enable the plugin, create the following file in your e2e tests directory:
With this in place, import AST transformer within your jest configuration file. By default (in the starter application), e2e tests configuration file is located under the test folder and is named jest-e2e.json.
If you use jest@<29, then use the snippet below.
If you use jest@^29, then use the snippet below, as the previous approach got deprecated.
Troubleshooting jest (e2e tests)
In case jest does not seem to pick up your configuration changes, it's possible that Jest has already cached the build result. To apply the new configuration, you need to clear Jest's cache directory.
To clear the cache directory, run the following command in your NestJS project folder:
In case the automatic cache clearance fails, you can still manually remove the cache folder with the following commands:

