typescript - 为什么这个Schema为空.....如何在nestjs中设置@ApiProperty
问题描述
我像这样在 Dto 中创建 @ApiProperty()
export class UpdateMeassageDto {
@ApiProperty()
message: {
hash: string;
created_at: Date;
updated_at: Date;
}
}
然后它得到一个像这样的空结果
解决方案
您可以通过以下方式进行
import { ApiProperty, getSchemaPath } from '@nestjs/swagger';
export class MessageDto {
@ApiProperty({
type: 'string',
example: 'hx1231',
})
hash: string;
@ApiProperty({
type: 'date',
example: '2021-08-04T19:39:00.829Z',
})
created_at: Date;
@ApiProperty({
type: 'string',
format: 'date-time',
example: '2021-08-04T19:39:00.829Z',
})
updated_at: Date;
}
export class UpdateMessageDto {
@ApiProperty({
type: 'array',
items: { $ref: getSchemaPath(MessageDto) },
})
message: MessageDto;
}
每个属性都需要 ApiProperty。您可以使用 $ref 在另一个 DTO 中使用一个 DTO。在 DTO 中也使用类验证器会很好。
推荐阅读
- applescript - AppleScript Photos 文件夹引用在子例程中不起作用 - 为什么?
- asp.net-core - .NET Core Razor 页面下拉菜单不返回值
- javascript - 在两个 MySql 表之间共享 user_id 值
- java - AWSToolkit for Eclipse --- 部署无服务器项目挂起 10%
- tsql - 为什么 ANTLR 语法文件中的类似规则会产生完全不同的树?
- amazon-web-services - AWS 组织账户管理
- cypher - neo4j n-levels的父子关系
- reactjs - 反应:我的方法没有根据其名称过滤我的数据
- qt - 为什么按钮不适合翻译?
- android - Expo Go 应用程序在 Android 上崩溃 | 数字文字后不允许直接使用标识符