Doesn't work with @Query() dto: Dto
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
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 ...
}
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
I cannot reproduce this issue locally. Query DTO should be generated correctly. What do you use, fastify or express?
It's express.
Please, provide a repository that reproduces this issue. It's working fine for me
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.
Cool @autaut03 !
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 I have the same problem, have you found a workaround for it?
@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.