5

I was creating documentation for Nest.js API using Swagger. The problem is what I removed documentation from this DTO or model, in swagger UI docs I can see it as empty object.

For example:

import { IsNotEmpty } from 'class-validator';

export class PostDto {
  @IsNotEmpty()
  readonly title: string;

  @IsNotEmpty()
  readonly content: string;

  @IsNotEmpty()
  readonly description: string;
}

enter image description here

Also I was trying to change name of this entity, using incognito mode, reinstall node_modules, but it didn't work. If I change name of this entity, it also changes there. What's wrong?

What I want to do, is by removing this documentation decorators, not to see those empty objects.

2
  • What exactly are you trying to achieve? Are you trying to exclude a controller or an action from Swagger? docs.nestjs.com/openapi/decorators
    – skink
    Commented Aug 16, 2022 at 13:35
  • I removed documentation from some DTOs and models, and I don't want to see it in UI docs as empty object.
    – dokichan
    Commented Aug 16, 2022 at 13:36

1 Answer 1

7

There is a CLI plugin provided with the @nestjs/swagger which will automatically add the annotations using reflection.

This means you NO LONGER need to individually annotate each property using @ApiProperty() etc.

The plugin is opt-in and not active by default.

In your nest-cli.json add:

{
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "plugins": ["@nestjs/swagger"]
  }
}

You can also change the default options:

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

See https://docs.nestjs.com/openapi/cli-plugin

Troubleshooting: if you are running in dev mode, may need to stop/start.

1
  • 2
    According to the docs we need to remove the dist folder manually in case of dev mode. Just restarting didn't help. However, all other information was helpful.
    – asifsaho
    Commented Apr 14, 2023 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.