为什么带有 Swagger 的 NestJS 会按要求报告我的所有 DTO 属性?

Tho*_*mas 4 swagger nestjs

我定义了这个 DTO 类。

import { ApiProperty } from "@nestjs/swagger"

export class FormDTO {
    @ApiProperty()
    id: string

    @ApiProperty()
    type: string

    @ApiProperty()
    fieldValues?: Record<string, unknown>

    @ApiProperty()
    parentFormId?: string
}
Run Code Online (Sandbox Code Playgroud)

我预计生成的 OpenAPI 规范会表明这一点fieldValues并且parentFormId是可选的,但它们是必需的。

在此输入图像描述

根据此处文档中的示例,它们应该是可选的。我缺少什么?

使用该 DTO 的唯一方法如下所示,但我认为这并不重要:

@Post(":id")
createForm(@Body() createFormDto: FormDTO) {
    if (this.formService.hasForm(createFormDto.id)) {
        throw new ConflictException(
            undefined,
            `A form with the id ${createFormDto.id} already exists.`
        )
    }
    return this.formService.createOrUpdateForm(createFormDto)
}
Run Code Online (Sandbox Code Playgroud)

如果重要的话,这里是代码DocumentBuilder

const config = new DocumentBuilder()
    .setTitle("API")
    .setDescription(
        "description."
    )
    .setVersion("1.0")
    .addBearerAuth(
        {
            type: "http",
            scheme: "bearer",
            bearerFormat: "JWT",
            description: "Paste a valid access token here."
        },
        JWTGuard.name
    )
    .build()
Run Code Online (Sandbox Code Playgroud)

Mic*_*evi 6

因为那@ApiProperty()就是

相反,用于@ApiPropertyOptional()可选字段。