Search code examples
node.jsswaggernestjsopenapinestjs-swagger

Nestjs + Swagger: How to add a custom option to @ApiProperty


Is it possible to add a custom option to @ApiProperty decorator?

import { ApiProperty } from '@nestjs/swagger';

class Animal {

  @ApiProperty({
    type: String,
    description: 'animal name',
    'x-description': 'some information' // how to add a cutom option 'x-description' ?
  })
  name: string;
}

Solution

  • Solution from https://github.com/nestjs/swagger/issues/195#issuecomment-526215840

    import { ApiProperty } from '@nestjs/swagger';
    
    type SwaggerDecoratorParams = Parameters<typeof ApiProperty>;
    type SwaggerDecoratorMetadata = SwaggerDecoratorParams[0];
    type Options = SwaggerDecoratorMetadata & DictionarySwaggerParameters;
    type DictionarySwaggerParameters = { 'x-property'?: string };
    
    export const ApiPropertyX = (params: Options) => {
      return ApiProperty(params);
    };