类型和参数¶
“SwaggerModule”在路由处理程序中搜索所有的“@Body()”、“@Query()”和“@Param()”装饰器来生成 API 文档。 它还通过利用反射创建相应的模型定义。考虑以下代码:
Hint
要显式设置 body 定义,请使用 @ApiBody()
装饰器(从 @nestjs/swagger
包导入)。
基于“CreateCatDto”,下面的模型定义将创建 Swagger UI:
正如您所看到的,虽然类有一些声明的属性,但定义是空的。 为了让类属性对“SwaggerModule”可见,我们必须要么用“@ApiProperty()”装饰器来注释它们,要么使用 CLI 插件(更多信息请阅读 插件 部分),它会自动完成:
Hint
与其手动注释每个属性,不如考虑使用 Swagger 插件(参见plugin部分),它会自动为你提供这些。
让我们打开浏览器并验证生成的“CreateCatDto”模型:
In addition, the @ApiProperty()
decorator allows setting various Schema Object properties:
Hint
Instead of explicitly typing the {{"@ApiProperty({ required: false })"}}
you can use the @ApiPropertyOptional()
short-hand decorator.
In order to explicitly set the type of the property, use the type
key:
Arrays¶
When the property is an array, we must manually indicate the array type as shown below:
Hint
Consider using the Swagger plugin (see Plugin section) which will automatically detect arrays.
Either include the type as the first element of an array (as shown above) or set the isArray
property to true
.
Circular dependencies¶
When you have circular dependencies between classes, use a lazy function to provide the SwaggerModule
with type information:
Hint
Consider using the Swagger plugin (see Plugin section) which will automatically detect circular dependencies.
Generics and interfaces¶
Since TypeScript does not store metadata about generics or interfaces, when you use them in your DTOs, SwaggerModule
may not be able to properly generate model definitions at runtime.
For instance, the following code won't be correctly inspected by the Swagger module:
In order to overcome this limitation, you can set the type explicitly:
Enums¶
To identify an enum
, we must manually set the enum
property on the @ApiProperty
with an array of values.
Alternatively, define an actual TypeScript enum as follows:
You can then use the enum directly with the @Query()
parameter decorator in combination with the @ApiQuery()
decorator.
With isArray
set to true, the enum
can be selected as a multi-select :
Enums schema¶
By default, the enum
property will add a raw definition of Enum on the parameter
.
The above specification works fine for most cases.
However, if you are utilizing a tool that takes the specification as input and generates client-side code, you might run into a problem with the generated code containing duplicated enums
.
Consider the following code snippet:
Hint
The above snippet is generated using a tool called NSwag.
You can see that now you have two enums
that are exactly the same.
To address this issue, you can pass an enumName
along with the enum
property in your decorator.
The enumName
property enables @nestjs/swagger
to turn CatBreed
into its own schema
which in turns makes CatBreed
enum reusable.
The specification will look like the following:
Hint
Any decorator that takes enum
as a property will also take enumName
.
Raw definitions¶
In some specific scenarios (e.g., deeply nested arrays, matrices), you may want to describe your type by hand.
Likewise, in order to define your input/output content manually in controller classes, use the schema
property:
Extra models¶
To define additional models that are not directly referenced in your controllers but should be inspected by the Swagger module, use the @ApiExtraModels()
decorator:
Hint
You only need to use @ApiExtraModels()
once for a specific model class.
Alternatively, you can pass an options object with the extraModels
property specified to the SwaggerModule#createDocument()
method, as follows:
To get a reference ($ref
) to your model, use the getSchemaPath(ExtraModel)
function:
oneOf, anyOf, allOf¶
To combine schemas, you can use the oneOf
, anyOf
or allOf
keywords (read more).
If you want to define a polymorphic array (i.e., an array whose members span multiple schemas), you should use a raw definition (see above) to define your type by hand.
Hint
The getSchemaPath()
function is imported from @nestjs/swagger
.
Both Cat
and Dog
must be defined as extra models using the @ApiExtraModels()
decorator (at the class-level).