我试图在我的新nestjs项目中使用swagger。
我使用DTO来序列化响应,但我也想使用它来进行swagger,几乎一切都可以。swagger中的Lessons值只是"lessons":["string"]
,它应该是具有三个键:值对的Lessons对象。
我不想使用example param来手动定义课程对象,因为我认为这在较大的应用程序中可能会很复杂。Swagger Example Response
{
"totalDocs": 0,
"limit": 0,
"totalPages": 0,
"page": 0,
"pagingCounter": 0,
"hasPrevPage": true,
"hasNextPage": true,
"docs": {
"title": "string",
"description": "string",
"lessons": [
"string"
],
"price": 0,
"isFree": false,
"published": false,
"slug": "string",
"createdAt": "2022-11-14T22:33:42.608Z",
"updatedAt": "2022-11-14T22:33:42.608Z"
}
}
UserCouse.response.dto.ts
class Lesson {
@ApiProperty({ description: 'Lesson ID' })
@Expose()
_id: string;
@ApiProperty({ description: 'Lesson name' })
@Expose()
name: string;
@ApiProperty({ description: 'Lesson type' })
@Expose()
type: string;
}
class Course {
@ApiProperty({ description: 'Course ID' })
@Expose()
_id: string;
@ApiProperty({ description: 'Course title' })
@Expose()
title: string;
@ApiProperty({ description: 'Course description' })
@Expose()
description: string;
@ApiProperty({ description: 'Course slug' })
@Expose()
slug: string;
@ApiProperty({ description: 'Course lessons', type: Lesson, isArray: true })
@Expose()
@Type(() => Lesson)
lessons: Lesson[];
@Expose()
@ApiProperty({
description: 'Course lesson count',
example: 8,
})
lessonsCount: number;
}
1条答案
按热度按时间hts6caw31#
修好了!
Dto类名必须在名称中包含DTO。因此在我的示例中它是
LessonDTO
。