在 Nestjs Swagger 中设置查询嵌套对象的问题

问题描述 投票:0回答:1

我正在开发一个 NestJS 应用程序,其中有一个控制器方法,该方法接受复杂的查询对象来获取类别。查询对象包括过滤器和分页,我使用 Swagger 来记录 API。但是,我在 Swagger 中为查询参数设置嵌套对象结构时遇到问题。

DTO定义:

export class GetAllQueryDto {
  @IsOptional()
  @ApiProperty({
    type: FilterDto,
    required: false,
    isArray: true,
    description: 'Array of filters for query'
  })
  @ValidateNested({ each: true })
  filters?: FilterDto[];

  @IsOptional()
  @ApiProperty({
    type: PaginationDto,
    required: false,
    example: PaginationDto,
    description: 'Pagination for query'
  })
  @ValidateNested()
  pagination?: PaginationDto;
}

控制器方法:

@Get('')
async getAllCategories(@Query() query: GetAllQueryDto): Promise<CommonResponseDto<CategoryResponseDto[]>> {
  // Method implementation
}

预期的 Swagger 输出:

Name: filters
Type: Array
Location: (query)
Description: Array of filters for query

Name: pagination
Type: Object
Location: (query)
Description: Pagination for query

实际 Swagger 输出:

Name: range
Type: *
Location: (query)
Description: Range value for filter

Name: operator
Type: string
Location: (query)
Description: Query operator
Example: >

Name: filterValue
Type: *
Location: (query)
Description: Value to filter
Example: value

... (other parameters)

我在 DTO 和控制器中尝试了不同的配置,但 Swagger 没有为查询参数生成预期的嵌套对象结构。如何配置我的 DTO 和控制器方法以实现所需的 Swagger 输出?

任何帮助或建议将不胜感激。谢谢!

nestjs swagger
1个回答
0
投票

@ApiProperty()
添加到
FilterDto
的每个属性中。将其添加到所有内部类属性中。它会起作用的。

例如=>

  @ApiProperty()
  startGeoCoordinate: Coordinate;

坐标类

export class Coordinate {
 @ApiProperty()
 latitude: number;
 @ApiProperty()
 longitude: number; 
}
© www.soinside.com 2019 - 2024. All rights reserved.