Skip to content

Commit

Permalink
feat: changed request body schema name when using omitted/picked type (
Browse files Browse the repository at this point in the history
  • Loading branch information
JadenKim-dev authored May 25, 2024
1 parent 87b7311 commit a103a02
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 64 deletions.
6 changes: 3 additions & 3 deletions spec/exclude-swagger/exclude-swagger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ describe('exclude swagger by route', () => {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/PickTypeClass',
$ref: '#/components/schemas/CreateBaseBodyDto',
anyOf: [
{
$ref: '#/components/schemas/PickTypeClass',
$ref: '#/components/schemas/CreateBaseBodyDto',
},
{
items: {
$ref: '#/components/schemas/PickTypeClass',
$ref: '#/components/schemas/CreateBaseBodyDto',
},
type: 'array',
},
Expand Down
70 changes: 27 additions & 43 deletions src/lib/crud.route.factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,36 @@ describe('CrudRouteFactory', () => {
class TestEntity extends BaseEntity {}

it('should check tableName in TypeORM', () => {
expect(() => new CrudRouteFactory({ prototype: {} }, { entity: {} as typeof BaseEntity })).toThrow(Error);
expect(() => new CrudRouteFactory({ prototype: {} }, { entity: {} as typeof BaseEntity })).toThrow(
new Error('Cannot find Table from TypeORM'),
);
});

it('should be checked paginationType of readMany route', () => {
expect(() =>
new CrudRouteFactory(
{ prototype: {} },
{
entity: TestEntity,
routes: { readMany: { paginationType: 'wrong' as unknown as PaginationType } },
},
).init(),
).toThrow(TypeError);
describe('should be checked paginationType', () => {
test.each(['readMany', 'search'])('route(%s)', (route) => {
expect(() =>
new CrudRouteFactory(
{ prototype: {} },
{
entity: TestEntity,
routes: { [route]: { paginationType: 'wrong' as unknown as PaginationType } },
},
).init(),
).toThrow(new TypeError('invalid PaginationType wrong'));
});
});

it('should be checked paginationType of search route', () => {
expect(() =>
new CrudRouteFactory(
{ prototype: {} },
{
entity: TestEntity,
routes: { search: { paginationType: 'wrong' as unknown as PaginationType } },
},
).init(),
).toThrow(TypeError);
});

it('should be checked paginationKeys included in entity columns', () => {
expect(() =>
new CrudRouteFactory(
{ prototype: {} },
{
entity: TestEntity,
routes: { search: { paginationKeys: ['wrong'] } },
},
).init(),
).toThrow(UnprocessableEntityException);

expect(() =>
new CrudRouteFactory(
{ prototype: {} },
{
entity: TestEntity,
routes: { readMany: { paginationKeys: ['wrong'] } },
},
).init(),
).toThrow(UnprocessableEntityException);
describe('should be checked paginationKeys included in entity columns', () => {
test.each(['readMany', 'search'])('route(%s)', (route) => {
expect(() =>
new CrudRouteFactory(
{ prototype: {} },
{
entity: TestEntity,
routes: { [route]: { paginationKeys: ['wrong'] } },
},
).init(),
).toThrow(new UnprocessableEntityException('pagination key wrong is unknown'));
});
});
});
24 changes: 7 additions & 17 deletions src/lib/crud.route.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,8 @@ export class CrudRouteFactory {
return namingStrategy(typeof discriminatorValue === 'string' ? discriminatorValue : discriminatorValue?.name);
}

return namingStrategy(table?.name);
return namingStrategy(table.name);
})();
if (!tableName) {
throw new Error('Cannot find Entity name from TypeORM');
}
this.entity.tableName = tableName;

const inheritanceTree = MetadataUtils.getInheritanceTree(entity as Function);
Expand Down Expand Up @@ -194,9 +191,6 @@ export class CrudRouteFactory {
}

const methodName = this.writeMethodOnController(crudMethod);
if (!methodName) {
throw new Error(`Required Method Name of ${crudMethod}`);
}

const targetMethod = this.targetPrototype[methodName];
const { path, params } = CRUD_POLICY[crudMethod].uriParameter(this.crudOptions, this.entity.primaryKeys);
Expand Down Expand Up @@ -286,8 +280,9 @@ export class CrudRouteFactory {
Reflect.defineMetadata(DECORATORS.API_OPERATION, CRUD_POLICY[method].swagger.operationMetadata(this.tableName), target);
this.defineParameterSwagger(method, params, target, paginationType);

if (this.crudOptions.routes?.[method]?.swagger?.response) {
const responseDto = this.generalTypeGuard(this.crudOptions.routes?.[method]?.swagger?.response!, method, 'response');
const routeConfig = this.crudOptions.routes?.[method];
if (routeConfig?.swagger?.response) {
const responseDto = this.generalTypeGuard(routeConfig.swagger.response, method, 'response');
const extraModels: Array<{ name: string }> = Reflect.getMetadata(DECORATORS.API_EXTRA_MODELS, target) ?? [];
if (!extraModels.some((model) => model.name === responseDto.name)) {
Reflect.defineMetadata(DECORATORS.API_EXTRA_MODELS, [...extraModels, responseDto], target);
Expand Down Expand Up @@ -357,18 +352,13 @@ export class CrudRouteFactory {
}
if (CRUD_POLICY[method].useBody) {
const bodyType = (() => {
const customBody = this.crudOptions.routes?.[method]?.swagger?.body;
if (customBody != null) {
return customBody;
const routeConfig = this.crudOptions.routes?.[method];
if (routeConfig?.swagger?.body) {
return this.generalTypeGuard(routeConfig.swagger.body, method, 'body');
}
if (method === Method.SEARCH) {
return RequestSearchDto;
}
const routeConfig = this.crudOptions.routes?.[method];
if (routeConfig?.swagger && 'body' in routeConfig.swagger) {
return this.generalTypeGuard(routeConfig.swagger['body']!, method, 'body');
}

return CreateRequestDto(this.crudOptions.entity, method);
})();

Expand Down
2 changes: 1 addition & 1 deletion src/lib/request/read-many.request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class CrudReadManyRequest<T> {

setSort(sort: Sort): this {
this._sort = sort;
this._findOptions.order = this._paginationKeys.reduce((order, paginationKey) => ({ ...order, [paginationKey]: sort }), {});
this._findOptions.order = this.paginationKeys.reduce((order, paginationKey) => ({ ...order, [paginationKey]: sort }), {});
return this;
}

Expand Down

0 comments on commit a103a02

Please sign in to comment.