# Types and Parameters

# Body, Query and Params

The SwaggerModule searches for all @Body() and @Query() decorators in route handlers to generate the API document. It also creates corresponding model definitions by taking advantage of reflection. Consider the following code:

@Post()
async create(@Body() createTodoDto: CreateTodoDto) {
  this.todoService.create(createTodoDto);
}

Based on the Todo, the following model definition Swagger UI will be created:

image
image

In order to make the class properties visible to the SwaggerModule, we have to annotate them with the @ApiProperty() decorator :

export class CreateTodoDto {
  @ApiProperty()
  name!: string;

  @ApiProperty()
  priority!: number;

  @ApiProperty()
  colorLabel!: string;
}

Let's open the browser and verify the generated Todo model:

image
image

# Return type

Due to SWC (Deno's typescript compiler) lacking design:return metadata, you must use the @ReturnedType decorator to say what your endpoint will return :

@ReturnedType(Todo)
@Get(':id')
async getById(@Param('id') id: string): Todo {
  return this.todoService.getById(id);
}

If your route returns an array, pass true as the second argument of ReturnedType :

@ReturnedType(Todo, true)
@Get()
async getTodos(): Todo[] {
  return this.todoService.getAll();
}

# Enums

To identify an enum, we must manually set the enum property on the @ApiProperty with an array of values.

@ApiProperty({ enum: ['Admin', 'Moderator', 'User']})
role: UserRole;