p0tta
p0tta

Reputation: 1651

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

Upvotes: 26

Views: 35138

Answers (6)

Pramod Patil
Pramod Patil

Reputation: 2763

Create a file with *.dto.ts and add the below code in nest-cli.json inside compilerOptions. You do not need to add @ApiProperty on each field.

{
  "compilerOptions": {
    "plugins": [
      {
        "name": "@nestjs/swagger",
        "options": {
          "introspectComments": true
        }
      }
    ]
  }
}

Upvotes: 3

Sohail Shrestha
Sohail Shrestha

Reputation: 768

I would recommend using dto for body.

Refer to documentation.

Example of a DTO is shown below.

DTO:

import { ApiProperty } from '@nestjs/swagger';

export class CreateCatDto {
  @ApiProperty()
  name: string;

  @ApiProperty()
  age: number;

  @ApiProperty()
  breed: string;
}

Function

@Post()
async create(@Body() createCatDto: CreateCatDto) {
  //Do Stuff.
}

@ApiProperty adds properties to swagger request.

It should show something like this: enter image description here

@Post()
@ApiBody({ type: CreateCatDto })
async create(@Body() createCatDto: CreateCatDto) {
  //Do Stuff.
}

The above code is going to give output similar to below where your schema is also going to be documented:

enter image description here

Hope this helps.

Upvotes: 12

Kir SR
Kir SR

Reputation: 69

try it like this:

@ApiBody({description: "body:any someMethod"})
@Post('lookup')
async someMethod(@Body() body:any){
console.log("BEGIN -- CustomerController.someMethod");
}

Upvotes: 3

Add @ApiProperty()

export class User{

 @ApiProperty()
  name:string
 
}

Upvotes: 21

Marco C.
Marco C.

Reputation: 1412

My endpoint accepts unknown key/value data and I was having the same problem (I tried any, unknown, Record<string, any>, object, {}). Finally @Body() data: Map<string, any> worked for me.

Upvotes: 4

Jay McDoniel
Jay McDoniel

Reputation: 70570

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.

Upvotes: 9

Related Questions