Skip to content

Commit

Permalink
Merge pull request #1525 from ton11797/fix-api-extension
Browse files Browse the repository at this point in the history
fix(api-extension): support for multiple api-extension
  • Loading branch information
kamilmysliwiec authored Jan 25, 2022
2 parents a525819 + be46b23 commit ab92747
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
7 changes: 6 additions & 1 deletion e2e/api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
},
"get": {
"operationId": "CatsController_findAll",
"x-multiple": {
"test": "test"
},
"x-codeSamples": [
{
"lang": "JavaScript",
Expand Down Expand Up @@ -226,7 +229,9 @@
}
},
"tags": [
"cats"
"cats",
"tag2",
"tag1"
],
"security": [
{
Expand Down
3 changes: 3 additions & 0 deletions e2e/src/cats/cats.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ export class CatsController {
@ApiExtension('x-codeSamples', [
{ lang: 'JavaScript', source: "console.log('Hello World');" }
])
@ApiExtension('x-multiple', { test: "test" })
@ApiTags("tag1")
@ApiTags("tag2")
findAll(@Query() paginationQuery: PaginationQuery) {}

@ApiQuery({ type: PaginationQuery })
Expand Down
5 changes: 5 additions & 0 deletions e2e/validate-schema.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ describe('Validate OpenAPI schema', () => {
expect(api.paths['/api/cats']['get']['x-codeSamples'][0]['lang']).toEqual(
'JavaScript'
);
expect(api.paths['/api/cats']['get']['x-multiple']['test']).toEqual(
'test'
);
expect(api.paths['/api/cats']['get']['tags']).toContain('tag1');
expect(api.paths['/api/cats']['get']['tags']).toContain('tag2');
} catch (err) {
console.log(doc);
expect(err).toBeUndefined();
Expand Down
10 changes: 9 additions & 1 deletion lib/decorators/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ export function createMixedDecorator<T = any>(
descriptor?: TypedPropertyDescriptor<any>
): any => {
if (descriptor) {
Reflect.defineMetadata(metakey, metadata, descriptor.value);
let metadatas: any;
if (Array.isArray(metadata)) {
const previousMetadata = Reflect.getMetadata(metakey, descriptor.value) || [];
metadatas = [...previousMetadata, ...metadata];
} else {
const previousMetadata = Reflect.getMetadata(metakey, descriptor.value) || {};
metadatas = {...previousMetadata, ...metadata};
}
Reflect.defineMetadata(metakey, metadatas, descriptor.value);
return descriptor;
}
Reflect.defineMetadata(metakey, metadata, target);
Expand Down

0 comments on commit ab92747

Please sign in to comment.