Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(crud-response-interceptor): classToPlain call two times #526

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class CrudResponseInterceptor extends CrudBaseInterceptor

return data instanceof dto
? classToPlain(data)
: classToPlain(classToPlainFromExist(data, new dto()));
: classToPlainFromExist(classToPlain(data), new dto());
}

protected serialize(context: ExecutionContext, data: any): any {
Expand Down
1 change: 1 addition & 0 deletions packages/crud/test/__fixture__/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './test.model';
export * from './test-serialize.model';
export * from './test-serialize-2.model';
export * from './test-serialize.transform';
20 changes: 20 additions & 0 deletions packages/crud/test/__fixture__/models/test-serialize.transform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Exclude, Transform } from 'class-transformer';

import { TestSerializeModel } from './test-serialize.model';

export class TestSerializeTransformModel extends TestSerializeModel {
id: number;

@Transform((value, obj) => obj.id + ':' + obj.name)
name: string;

email: string;

@Exclude()
isActive: boolean;

constructor(partial: Partial<TestSerializeTransformModel>) {
super(partial);
Object.assign(this, partial);
}
}
65 changes: 65 additions & 0 deletions packages/crud/test/crud.transform.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { Controller, INestApplication, Inject } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';

import { Crud } from '../src/decorators';
import { HttpExceptionFilter } from './__fixture__/exception.filter';
import { TestSerializeModel, TestSerializeTransformModel } from './__fixture__/models';

import { TestSerializeService } from './__fixture__/services';

describe('#crud', () => {
describe('#transform', () => {
let app: INestApplication;
let server: any;

const SERVICE = 'TestSerializeService';

@Crud({
model: {
type: TestSerializeTransformModel,
},
query: {
alwaysPaginate: true,
},
})
@Controller('test')
class TestController {
constructor(@Inject(SERVICE) public service: TestSerializeService) {}
}

beforeAll(async () => {
const fixture = await Test.createTestingModule({
controllers: [TestController],
providers: [
{
provide: SERVICE,
useFactory: () => new TestSerializeService(TestSerializeModel),
},
],
}).compile();

app = fixture.createNestApplication();

await app.init();
server = app.getHttpServer();
});

afterAll(async () => {
await app.close();
});

describe('#transform', () => {
it('transform only one times', (done) => {
return request(server)
.get('/test')
.expect(200)
.end((_, res) => {
expect(res.body.data[0].name).toBe('1:name');
done();
});
});
});
});
});