swagger icon indicating copy to clipboard operation
swagger copied to clipboard

ApiProperty containing a key as an array of objects

Open abdulkaderelrawas opened this issue 1 year ago • 3 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Current behavior

This is related to the following issues: 2945, 2418, 1112, 1113, 2418, 735, 90.

I have these 2 classes:

class FilterParamDto {
  @IsString()
  filterBy: string;

  @IsString()
  operator: string;

  @IsString()
  value: string;
}

export class SortAndFilterDto extends SortParamsDto {
  @ApiProperty({ type: FilterDto, isArray: true })
  readonly filters: FilterParamDto[];
}

I am getting this

image

I tried the following:

  • plugin in nest cli and added all the necessary decorators to filters and not use the @ApiProperty: doesn't work.
  • @ApiProperty({ type: [FilterDto]}) instead of @ApiProperty({ type: FilterDto, isArray: true }): doesn't work.
  • changed the type of FilterDto to get an array of strings: works

So I figured the problem is somewhere in reading array of objects only.

I read some workarounds using $ref and @ApiQuery over the controller directly, but it doesn't make sense to do workarounds that will disorganize the code when there might be a bug that needs fixing.

Minimum reproduction code

Reproduction repo

Steps to reproduce

No response

Expected behavior

the expected behaviour should be like the image

image

Package version

7.4.0

NestJS version

9.4.3

Node.js version

20.11.0

In which operating systems have you tested?

  • [ ] macOS
  • [X] Windows
  • [ ] Linux

Other

This has been reported in more than 5 other issues since 2018, is there anything we can do to fix? there is even an open PR

abdulkaderelrawas avatar Aug 15 '24 08:08 abdulkaderelrawas

Please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project).

kamilmysliwiec avatar Aug 15 '24 09:08 kamilmysliwiec

@kamilmysliwiec

here's a minimal repro

Reproduction Repo

how to setup

  1. clone
  2. npm/yarn/pnpm i
  3. pnpm start:dev
  4. go to http://localhost:3000/docs
  5. go to app -> bug endpoint
  6. try it out
  7. there is no filters array of objects

can we return it to (bug)(needs triage) instead of (needs clarification)? thank you :)

Things to play with:

  • I added class-validator, class-transformer and @nestjs/swagger
  • I added the plugin in the nestjs cli
  • I added @ApiProperty to all api properties
  • I added validations in the query dto

abdulkaderelrawas avatar Aug 15 '24 10:08 abdulkaderelrawas

Ping

abdulkaderelrawas avatar Sep 26 '24 13:09 abdulkaderelrawas

https://github.com/nestjs/swagger/pull/1113

kamilmysliwiec avatar Oct 24 '24 12:10 kamilmysliwiec