swagger icon indicating copy to clipboard operation
swagger copied to clipboard

Doesn't work with @Query() dto: Dto

Open autaut03 opened this issue 8 years ago • 10 comments

I'm submitting a...


[ ] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Parsing @Query() with DTO object as a type doesn't work. Changed it to @Body() and it worked.

Expected behavior

Make it work.

Minimal reproduction of the problem with instructions


@Controller()
export class TestController {
    test(@Query() dto: TestDto) {}
}

class TestDto {
    @ApiModelProperty()
    readonly prop: string;
}

What is the motivation / use case for changing the behavior?

Example above.

Environment


Nest version: 5.1.0

autaut03 avatar Jul 15 '18 18:07 autaut03

It's not a bug. If you want to get data from request.body mapped to DTO you should use @Body decorator. If you want to get query you should use @Query. Also for swagger you should use @ApiImplicitQuery for documenting it.

Check example below:

  @Get()
  @ApiOperation({ title: 'Get All Tasks' })
  @ApiResponse({ status: 200, description: 'The list of tasks has been successfully retrieved.' })
  @ApiResponse({ status: 400, description: 'Validation failed' })
  @ApiResponse({ status: 403, description: 'Forbidden.' })
  @ApiImplicitQuery({ name: 'projectId', description: 'filter tasks by project id', required: false })
  @ApiImplicitQuery({ name: 'status', description: 'filter tasks by status if project id specified', required: false })
  async findAll(
    @Query('projectId', new QueryIdValidationPipe()) projectId?: string,
    @Query('status') status?: string
  ): Promise<Tasks[]> {
    return ...
  }

pumano avatar Jul 31 '18 14:07 pumano

Nest supports using DTOs with both @Body() and @Query() decorators, and this is exactly what I'm doing. I don't want to specify parameters implicitly when they have already been specified in the DTO. Also, I use class-validator on my query DTO object, so there's no way I can use a separate @Query() decorator for each of my parameters.

This should be changed in my opinion: https://github.com/nestjs/swagger/blob/master/lib/explorers/api-parameters.explorer.ts#L103

autaut03 avatar Jul 31 '18 15:07 autaut03

I cannot reproduce this issue locally. Query DTO should be generated correctly. What do you use, fastify or express?

kamilmysliwiec avatar Aug 16 '18 19:08 kamilmysliwiec

It's express.

autaut03 avatar Aug 16 '18 21:08 autaut03

Please, provide a repository that reproduces this issue. It's working fine for me

kamilmysliwiec avatar Aug 16 '18 21:08 kamilmysliwiec

Not sure what happened, but now it works for me too =/ So many things have been done since, I won't be able to tell if it got magically fixed by my actions or have I just missed something, but this can be closed for now. Thanks.

autaut03 avatar Aug 16 '18 21:08 autaut03

Cool @autaut03 !

kamilmysliwiec avatar Aug 16 '18 21:08 kamilmysliwiec

DTO is not generated for me as well if I use it like this (as query):

@UsePipes(new ValidationPipe(FindProductsDto))
@Get("products")
@AllowAnonymous()
async products(@Query() dto: FindProductsDto): Promise<ProductInfoPaginationDto> {
    return await this.productService.products(dto);
}

No matter if I put tin ApiImplicitQuery on a method or not. If I say @ApiImplicitQuery({name: "dto", type: FindProductsDto}) swagger will not generate anything and UI will be dto: findProductsDto FindProductsDto have multiple @ApiModelProperty.

Any idea?

goors avatar Jul 18 '19 15:07 goors

@goors I have the same problem, have you found a workaround for it?

vadzimmaisiuk avatar Feb 04 '20 07:02 vadzimmaisiuk

@vmajsuk I think they fixed this in the new version. I am not sure. I kind of gave up on nestjs. I did not try the new version since it has a lot of breaking changes. I remember solving this with PUT method. I know that sucks because it does not really rest way. Sorry I can not help.

goors avatar Feb 04 '20 10:02 goors