swaggerswagger-uinestjsnestjs-swaggernestjs-config

Request body not showing in Nest.js + Swagger


My controller code is something like this.

@Controller('customer')
export class CustomerController{

    constructor(private readonly customerService: CustomerService){}

    @Post('lookup')
    async someMethod(@Body() body:any){

        console.log("BEGIN -- CustomerController.someMethod");

I am expecting to see in Swagger a place where I can input some text as a request body but instead I see this

enter image description here


Solution

  • So it sounds like there's a few things going on here. The Swagger UI is a helper tool for sending in requests, but to do that it needs to know the shape of the request body. any is not good enough. If you're looking for a tool that allows you to send in anything, curl or postman are you best bets (at least for free).

    Nest has a Swagger plugin that will read through your Typescript code and decorate your types and method accordingly, but you have to opt in to enable it. Otherwise, you need to use the decorators from the @nestjs/swagger package to tell Swagger what types are expected in and out of the methods.

    So long as the type that corresponds to @Body() has swagger decorators or you enable the swagger plugin and have a valid class, the swagger UI should show up as expected, but with the above and using type any it won't do you any good.