简体   繁体   中英

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;
}

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

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);
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM