dokichan
dokichan

Reputation: 591

Swagger + Nest.js doesn't remove empty DTO and model

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.

Upvotes: 5

Views: 4066

Answers (1)

shenku
shenku

Reputation: 12430

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.

Upvotes: 7

Related Questions