【问题标题】:Why this Schema Empty ..... How to setting @ApiProperty in nestjs为什么这个Schema为空.....如何在nestjs中设置@ApiProperty
【发布时间】:2021-10-09 05:21:01
【问题描述】:

我像这样在 Dto 中创建 @ApiProperty()

export class UpdateMeassageDto {

@ApiProperty()

message: {

    hash: string;


    created_at: Date;


    updated_at: Date;
}

}

然后它得到一个像这样的空结果

https://i.stack.imgur.com/kYGP5.jpg

【问题讨论】:

    标签: typescript swagger nestjs nestjs-swagger


    【解决方案1】:

    您可以通过以下方式进行操作

    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 中也可以使用 class-validator

    【讨论】:

      【解决方案2】:

      尝试将新类型定义为:

      export class Message{
      
          @ApiProperty()
          hash: string;
      
          @ApiProperty()
          created_at: Date;
      
          @ApiProperty()
          updated_at: Date;
      }
      

      并按如下方式更新您的 DTO:

      export class UpdateMessageDto {
      
          @ApiProperty()
          message: Message
      }
      
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-19
        • 2022-09-24
        • 2018-01-10
        • 1970-01-01
        • 1970-01-01
        • 2022-12-06
        • 1970-01-01
        • 2021-01-16
        相关资源
        最近更新 更多