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;
}
2
  • doc SchemaObject. Commented May 22, 2022 at 8:48
  • @EmptyBrain I can't see anything about a custom option there. Commented May 22, 2022 at 16:24

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

I don't catch how to use Extension decorator to add a custom attribute. I've created a custom decorator than extends ApiProperty like github.com/nestjs/swagger/issues/195#issuecomment-526215840 and it works well.
not sure i fully understand what you mean.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.