Skip to content

Commit

Permalink
fix: support for multiple api decorators on schema class
Browse files Browse the repository at this point in the history
Support multiple @APIextension decorators on the schema class.
  • Loading branch information
arabold committed Nov 13, 2024
1 parent 7acedba commit 10297ec
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
6 changes: 6 additions & 0 deletions e2e/api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -1677,6 +1677,12 @@
]
}
},
"x-schema-extension-multiple": {
"test": "test"
},
"x-schema-extension": {
"test": "test"
},
"required": [
"name",
"age",
Expand Down
4 changes: 3 additions & 1 deletion e2e/src/cats/classes/cat.class.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
12 changes: 10 additions & 2 deletions e2e/validate-schema.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand Down Expand Up @@ -204,6 +212,6 @@ describe('Validate OpenAPI schema', () => {
'image/jpeg': { schema: { type: 'string', format: 'binary' } }
}
}
})
});
});
});
11 changes: 10 additions & 1 deletion lib/decorators/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,16 @@ export function createMixedDecorator<T = any>(
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;
};
}
Expand Down

0 comments on commit 10297ec

Please sign in to comment.