Skip to content

Commit

Permalink
test: using IntersectionType (#566)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiho-kr authored Jun 4, 2024
1 parent cbf4c19 commit bed53a5
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 7 deletions.
6 changes: 6 additions & 0 deletions spec/exclude-swagger/additional-base-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ApiProperty } from '@nestjs/swagger';

export class AdditionalBaseInfo {
@ApiProperty()
color: string;
}
8 changes: 5 additions & 3 deletions spec/exclude-swagger/exclude-swagger-resonse-name.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Controller, Module, type INestApplication } from '@nestjs/common';
import { OmitType, PickType } from '@nestjs/swagger';
import { IntersectionType, OmitType, PickType } from '@nestjs/swagger';
import { Test } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';

import { AdditionalBaseInfo } from './additional-base-info';
import { Crud } from '../../src/lib/crud.decorator';
import { CrudController } from '../../src/lib/interface';
import { BaseEntity } from '../base/base.entity';
Expand All @@ -16,14 +17,15 @@ import type { TestingModule } from '@nestjs/testing';

class OmitTypeDto extends OmitType(BaseEntity, ['description']) {}
class PickTypeDto extends PickType(BaseEntity, ['name']) {}
class IntersectionTypeDto extends IntersectionType(BaseEntity, AdditionalBaseInfo) {}

@Crud({
entity: BaseEntity,
routes: {
recover: { swagger: { hide: true } },
readOne: { swagger: { response: OmitTypeDto } },
create: { swagger: { body: PickTypeDto } },
update: { swagger: { response: OmitTypeDto } },
update: { swagger: { response: IntersectionTypeDto } },
},
})
@Controller('exclude-swagger')
Expand Down Expand Up @@ -98,7 +100,7 @@ describe('exclude swagger > defined name', () => {
'200': {
content: {
'application/json': {
schema: { $ref: '#/components/schemas/OmitTypeDto' },
schema: { $ref: '#/components/schemas/IntersectionTypeDto' },
},
},
description: 'Updated ok',
Expand Down
4 changes: 3 additions & 1 deletion spec/exclude-swagger/exclude-swagger.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Controller } from '@nestjs/common';
import { PickType } from '@nestjs/swagger';
import { IntersectionType, PickType } from '@nestjs/swagger';

import { AdditionalBaseInfo } from './additional-base-info';
import { CustomResponseDto } from './custom-response.dto';
import { Crud } from '../../src/lib/crud.decorator';
import { CrudController } from '../../src/lib/interface';
Expand All @@ -12,6 +13,7 @@ import { BaseService } from '../base/base.service';
routes: {
recover: { swagger: { hide: true } },
readOne: { swagger: { response: PickType(BaseEntity, ['name']) } },
readMany: { swagger: { response: IntersectionType(BaseEntity, AdditionalBaseInfo) } },
create: { swagger: { body: PickType(BaseEntity, ['name']) } },
update: { swagger: { response: CustomResponseDto } },
},
Expand Down
50 changes: 50 additions & 0 deletions spec/exclude-swagger/exclude-swagger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,54 @@ describe('exclude swagger by route', () => {
},
});
});

it('Should be changed swagger readMany response interface', () => {
const readMany = 'get /exclude-swagger';
expect(routeSet[readMany].responses).toEqual({
'200': {
content: {
'application/json': {
schema: {
allOf: [
{
properties: {
data: {
items: {
$ref: '#/components/schemas/IntersectionBaseEntityAdditionalBaseInfo',
},
type: 'array',
},
metadata: {
properties: {
limit: {
example: 20,
type: 'number',
},
nextCursor: {
example: 'cursorToken',
type: 'string',
},
total: {
example: 100,
type: 'number',
},
},
type: 'object',
},
},
},
],
},
},
},
description: 'Fetch many entities from Base table',
},
'422': {
description: 'Invalid query',
},
});
expect(routeSet[readMany].root?.method).toEqual('get');
expect(routeSet[readMany].root?.summary).toEqual("read many from 'Base' Table");
expect(routeSet[readMany].root?.description).toEqual("Fetch multiple entities in 'Base' Table");
});
});
4 changes: 1 addition & 3 deletions src/lib/crud.route.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,7 @@ export class CrudRouteFactory {
const bodyType = (() => {
const customBody = this.crudOptions.routes?.[method]?.swagger?.body;
if (customBody) {
return ['PickTypeClass', 'OmitTypeClass'].includes(customBody.name)
? this.generalTypeGuard(customBody, method, 'body')
: customBody;
return this.generalTypeGuard(customBody, method, 'body');
}
if (method === Method.SEARCH) {
return RequestSearchDto;
Expand Down

0 comments on commit bed53a5

Please sign in to comment.