Vladimir  Golub
Vladimir Golub

Reputation: 613

How to add description to @ApiTags for swagger in NestJS?

I want to add desription for block of api.

I have tried:

@ApiOperation({
  description: 'Operation description'
})

It doesn't work.

Upvotes: 4

Views: 11051

Answers (3)

user23828085
user23828085

Reputation: 1

// users.controller.ts
@ApiTag({
  name: 'users',
  description: 'Get all users',
})
@ApiBearerAuth()
@Controller('users')
export class UsersController {}


// ApiTag.decorator.ts
import { swaggerConfig } from '@/common/swagger';
import { TagObject } from '@nestjs/swagger/dist/interfaces/open-api-spec.interface';
import { ApiTags } from '@nestjs/swagger';
import { applyDecorators } from '@nestjs/common';

export const ApiTag = (tagObject: TagObject) => {
  swaggerConfig.tags.unshift(tagObject);
  return applyDecorators(ApiTags(tagObject.name));
};

//  common/swagger.ts
import { DocumentBuilder } from '@nestjs/swagger';

const swaggerConfig = new DocumentBuilder()
  .setTitle('title')
  .setDescription('api docs')
  .setVersion('1.0')
  .addBearerAuth()
  .build();

export { swaggerConfig };

//main.ts
  const options: SwaggerDocumentOptions = {
    operationIdFactory: (controllerKey: string, methodKey: string) => methodKey,
  };
  const document = SwaggerModule.createDocument(app, swaggerConfig, options);
  SwaggerModule.setup('docs', app, document);

Upvotes: 0

Gopard
Gopard

Reputation: 1060

To improve previous answer: You can add tag description on initial step

new DocumentBuilder()
.setTitle('API with NestJS')
...
.addTag('SomeTag1', 'Tag description 1')
.addTag('SomeTag2', 'Tag description 2')

And then use tag on class level (controller)

@ApiTags('SomeTag1')

Upvotes: 11

Tim
Tim

Reputation: 502

Adding description on method level

@ApiOperation({ summary: 'Operation description' })

Check here: https://docs.nestjs.com/openapi/migration-guide#breaking-changes

Adding tag on class level (controller)

@ApiTags('MyTag')

This will create a collapsible block with all methods underneath it.

Check here: https://docs.nestjs.com/openapi/decorators

Upvotes: 12

Related Questions