question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using the dto class and @Body decorator does not work as expected.

See original GitHub issue

The code:

DTOs:

import {
  ApiModelProperty,
} from '@nestjs/swagger';

import { UserDto } from './user.dto';

export class CreateUserDto extends UserDto {

  @ApiModelProperty()
  readonly password: string;

  @ApiModelProperty()
  readonly confirmPassword: string;
}
import {
  ApiModelProperty,
  ApiModelPropertyOptional,
} from '@nestjs/swagger';


export class UserDto {

  @ApiModelPropertyOptional()
  readonly firstName?: string;

  @ApiModelPropertyOptional()
  readonly lastName?: string;

  @ApiModelProperty()
  readonly email: string;
}

Controller:


@ApiUseTags('users')
@Controller('users')
export class UsersController {

  constructor(private readonly usersService: UsersService) {}

  /**
   * Creates a new User (register)
   *
   * @param createUserDto
   * @return Promise<void>
   */
  @Post()
  @ApiOperation({ title: 'Create a new User' })
  @ApiResponse({
    status: HttpStatus.CREATED,
    description: 'The record has been successfully created.',
  })
  @ApiResponse({
    status: HttpStatus.BAD_REQUEST,
    description: 'confirm password doesn\'t match the password, or the payload is invalid',
  })
  @ApiResponse({
    status: HttpStatus.CONFLICT,
    description: 'Email already in use',
  })
  @ApiResponse({ status: HttpStatus.FORBIDDEN, description: 'Forbidden.' })
  @ApiResponse({ status: HttpStatus.UNAUTHORIZED, description: 'Unauthorized.' })
  async createUser(@Body() createUserDto: CreateUserDto) {
    await this.usersService
      .create(createUserDto)
      .catch(this._handleError);
  }

  / * <...> */

}

The result:

screenshot 2018-03-22 03 05 24


However, using the following is fixing the Swagger UI:

  @ApiImplicitBody({
    name: 'role data',
    type: CreateUserDto,
  })

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
alexandr2110procommented, Mar 22, 2018

The same is for the @Query decorator

1reaction
kamilmysliwieccommented, May 28, 2018
But now we have another issue: @ApiBearerAuth doesn't do a thing! It worked previously but suddenly it stopped.

@alexandr2110pro track this issue here https://github.com/nestjs/swagger/issues/83

The rest should be fixed in 2.0.1 which I just published.

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - DTO not working with custom decorator in NestJS
Show activity on this post. i'm trying to use DTO near a custom decorator inside a controller in NestJS to validate the body....
Read more >
Validation | NestJS - A progressive Node.js framework
Now we can add a few validation rules in our CreateUserDto . We do this using decorators provided by the class-validator package, described...
Read more >
How To Use DTO For Validation in NestJS (2022)
Today I'll talk about Data Transfer Objects (DTO) in NestJS and how to use them in order to validate your incoming requests. What...
Read more >
Understanding controllers and routes in NestJS
A controller is a class defined with methods for handling one or more requests. The controller provides the handler function for a route....
Read more >
NestJS — The Hero We Didn't Know We Needed - SingleStone
Class validator — A decorator validator that uses validator.js under the hood. Class Transformer — This allows you to transform a plain object ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found