diff --git a/e2e/api-spec.json b/e2e/api-spec.json index 6ac9b3738..94527952e 100644 --- a/e2e/api-spec.json +++ b/e2e/api-spec.json @@ -1677,6 +1677,12 @@ ] } }, + "x-schema-extension-multiple": { + "test": "test" + }, + "x-schema-extension": { + "test": "test" + }, "required": [ "name", "age", diff --git a/e2e/src/cats/classes/cat.class.ts b/e2e/src/cats/classes/cat.class.ts index a0a147309..d0b5f2064 100644 --- a/e2e/src/cats/classes/cat.class.ts +++ b/e2e/src/cats/classes/cat.class.ts @@ -1,6 +1,8 @@ -import { ApiProperty } from '../../../../lib'; +import { ApiExtension, ApiProperty } from '../../../../lib'; import { LettersEnum } from '../dto/pagination-query.dto'; +@ApiExtension('x-schema-extension', { test: 'test' }) +@ApiExtension('x-schema-extension-multiple', { test: 'test' }) export class Cat { @ApiProperty({ example: 'Kitty', description: 'The name of the Cat' }) name: string; diff --git a/e2e/validate-schema.e2e-spec.ts b/e2e/validate-schema.e2e-spec.ts index a6a22cf3c..0e397c3ba 100644 --- a/e2e/validate-schema.e2e-spec.ts +++ b/e2e/validate-schema.e2e-spec.ts @@ -111,13 +111,21 @@ describe('Validate OpenAPI schema', () => { writeFileSync(join(__dirname, 'api-spec.json'), doc); try { - const api = await SwaggerParser.validate(document as any); + const api = (await SwaggerParser.validate( + document as any + )) as OpenAPIV3.Document; console.log( 'API name: %s, Version: %s', api.info.title, api.info.version ); expect(api.info.title).toEqual('Cats example'); + expect( + api.components.schemas['Cat']['x-schema-extension']['test'] + ).toEqual('test'); + expect( + api.components.schemas['Cat']['x-schema-extension-multiple']['test'] + ).toEqual('test'); expect( api.paths['/api/cats']['post']['callbacks']['myEvent'][ '{$request.body#/callbackUrl}' @@ -204,6 +212,6 @@ describe('Validate OpenAPI schema', () => { 'image/jpeg': { schema: { type: 'string', format: 'binary' } } } } - }) + }); }); }); diff --git a/lib/decorators/helpers.ts b/lib/decorators/helpers.ts index 2d51b9376..f2a36f28f 100644 --- a/lib/decorators/helpers.ts +++ b/lib/decorators/helpers.ts @@ -114,7 +114,16 @@ export function createMixedDecorator( Reflect.defineMetadata(metakey, metadatas, descriptor.value); return descriptor; } - Reflect.defineMetadata(metakey, metadata, target); + + let metadatas: any; + if (Array.isArray(metadata)) { + const previousMetadata = Reflect.getMetadata(metakey, target) || []; + metadatas = [...previousMetadata, ...metadata]; + } else { + const previousMetadata = Reflect.getMetadata(metakey, target) || {}; + metadatas = Object.assign(Object.assign({}, previousMetadata), metadata); + } + Reflect.defineMetadata(metakey, metadatas, target); return target; }; }