swagger icon indicating copy to clipboard operation
swagger copied to clipboard

Wrong swagger generation for @Query with array of objects

Open wilker7ribeiro opened this issue 1 year ago • 7 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Current behavior

I want to create a GET endpoint with a inner object inside query params:

export class QueryParamsSubObject {
  @ApiProperty()
  subObjectStringParam: string;
}

export class QueryParams {
  @ApiProperty()
  stringParam: string;

  @ApiProperty({ type: QueryParamsSubObject })
  subObject: QueryParamsSubObject;
}

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get('/bugged')
  getHelloBugged(@Query() queryParams: QueryParams): string {
    console.log(queryParams);
    return this.appService.getHello();
  }
}

The swagger definition is bugged, having all parameters from QueryParamsSubObject inside QueryParams instead of QueryParams.subObject:

image

The definition is generated correctly when using @ApiQuery instead of @Query:

@Get('/correct')
@ApiQuery({
  type: QueryParams,
})
getCorrectlyGenerated(): string {
  return this.appService.getHello();
}

image

Minimum reproduction code

Codesandbox https://github.com/wilker7ribeiro/nest-swagger-query-object-param-issue

Steps to reproduce

  1. npm install
  2. npm start
  3. open http://localhost:3000/api

Expected behavior

@Query should correctly generate the swagger definition as @ApiQuery does.

Package version

7.3.1

NestJS version

10.3.2

Node.js version

10.3.2

In which operating systems have you tested?

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

wilker7ribeiro avatar May 09 '24 15:05 wilker7ribeiro

Would you like to create a PR for this issue?

kamilmysliwiec avatar May 10 '24 08:05 kamilmysliwiec

I can confirm, same issue here. Actually the title might be misleading since the bug seems to be not limited to arrays but related with properties being in nested objects, including nested in arrays.

lajerca avatar May 29 '24 09:05 lajerca

Can confirm this issue too, experimenting a little I found out that basically the api generator is flattening too much: I forked OP repro to show it ( https://codesandbox.io/p/github/wonderbeel/nest-swagger-query-object-param-issue/ and https://github.com/wonderbeel/nest-swagger-query-object-param-issue ), as you can see we are basically missing one level of nesting.

wonderbeel avatar May 31 '24 18:05 wonderbeel

Have you resolved the issue yet? I am currently using Apidog to generate API documentation, and it's working well for me.

satokenta940 avatar Jun 03 '24 02:06 satokenta940

I have a FilterDto like so

class FilterDto {
  @IsString()
  column: string;

  @IsEnum({
    equals: 'equals',
    neq: 'not',
    gt: 'gt',
    lt: 'lt',
    gte: 'gte',
    lte: 'lte',
    in: 'in',
    nin: 'not',
  })
  operator: string;

  @IsString()
  value: string;
}

and an Query param like so

  @ApiProperty({ type: FilterDto, isArray: true })
  @IsOptional()
  @IsArray()
  @Type(() => FilterDto)
  @ValidateNested({ each: true })
  readonly filters: FilterDto[];

This doesn't generate it right unless i put the FilterDto in an array @ApiProperty({ type: [FilterDto], isArray: true }), and when i do it puts it as an array of arrays instead of array of objects

I tried @ApiProperty({ type: [FilterDto] }), doesn't work I tried to specify the type inside, doesn't work I tried to auto generate it using the plugin in nestcli, doesn't work

any news about this?

abdulkaderelrawas avatar Aug 15 '24 08:08 abdulkaderelrawas

I have same issue.

marefati110 avatar Aug 20 '24 06:08 marefati110

I am facing the same issue with array of objects

sixtusDev avatar Sep 26 '24 13:09 sixtusDev

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

kamilmysliwiec avatar Oct 24 '24 12:10 kamilmysliwiec