2

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;
}
KDI
  • 81
  • 1
  • 7

2 Answers2

1

I'm not sure i fully understand, but If you are talking about openapi's extensions. Take a look at this: https://github.com/nestjs/swagger/issues/195

CY-OD
  • 336
  • 2
  • 8
  • I don't catch how to use Extension decorator to add a custom attribute. I've created a custom decorator than extends ApiProperty like https://github.com/nestjs/swagger/issues/195#issuecomment-526215840 and it works well. – KDI May 22 '22 at 16:32
  • not sure i fully understand what you mean. – CY-OD May 23 '22 at 13:21
0

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);
};
KDI
  • 81
  • 1
  • 7