Skip to content

Commit 5c9b2f4

Browse files
committed
change option to enableCircularCheck and disable circular check by default
1 parent 945345f commit 5c9b2f4

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

src/ClassTransformOptions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ export interface ClassTransformOptions {
5858

5959

6060
/**
61-
* If set to true then class transformer will not perform a circular check.
62-
* This option is useful when you know for sure that your types can't have a circular dependency. You usually use this option to boost the transform operation.
61+
* If set to true then class transformer will perform a circular check. (circular check is turned off by default)
62+
* This option is useful when you know for sure that your types might have a circular dependency.
6363
*/
64-
skipCircularCheck?: boolean;
64+
enableCircularCheck?: boolean;
6565
}

src/TransformOperationExecutor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class TransformOperationExecutor {
3636
const newValue = arrayType && this.transformationType === "plainToClass" ? new (arrayType as any)() : [];
3737
(value as any[]).forEach((subValue, index) => {
3838
const subSource = source ? source[index] : undefined;
39-
if (this.options.skipCircularCheck || !this.isCircular(subValue, level)) {
39+
if (!this.options.enableCircularCheck || !this.isCircular(subValue, level)) {
4040
const value = this.transform(subSource, subValue, targetType, undefined, subValue instanceof Map, level + 1);
4141
if (newValue instanceof Set) {
4242
newValue.add(value);
@@ -157,7 +157,7 @@ export class TransformOperationExecutor {
157157
continue;
158158
}
159159

160-
if (this.options.skipCircularCheck || !this.isCircular(subValue, level)) {
160+
if (!this.options.enableCircularCheck || !this.isCircular(subValue, level)) {
161161
let transformKey = this.transformationType === "plainToClass" ? newValueKey : key;
162162
let finalValue = this.transform(subSource, subValue, type, arrayType, isSubValueMap, level + 1);
163163
finalValue = this.applyCustomTransformations(finalValue, targetType, transformKey);

test/functional/circular-reference-problem.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe("circular reference problem", () => {
4040
photo1.users = [user];
4141
photo2.users = [user];
4242

43-
const plainUser = classToPlain(user);
43+
const plainUser = classToPlain(user, { enableCircularCheck: true });
4444
plainUser.should.be.eql({
4545
firstName: "Umed Khudoiberdiev",
4646
photos: [{
@@ -89,14 +89,14 @@ describe("circular reference problem", () => {
8989
photo1.users = [user];
9090
photo2.users = [user];
9191

92-
const classUser = classToClass(user);
92+
const classUser = classToClass(user, { enableCircularCheck: true });
9393
classUser.should.not.be.equal(user);
9494
classUser.should.be.instanceOf(User);
9595
classUser.should.be.eql(user);
9696

9797
});
9898

99-
describe("skipCircularCheck option", () => {
99+
describe("enableCircularCheck option", () => {
100100
class Photo {
101101
id: number;
102102
filename: string;
@@ -124,14 +124,14 @@ describe("circular reference problem", () => {
124124
isCircularSpy.restore();
125125
});
126126

127-
it("skipCircularCheck option is true", () => {
128-
const result = plainToClass<User, Object>(User, user, { skipCircularCheck: true});
127+
it("enableCircularCheck option is undefined (default)", () => {
128+
const result = plainToClass<User, Object>(User, user);
129129
sinon.assert.notCalled(isCircularSpy);
130130
});
131131

132-
it("skipCircularCheck option is undefined", () => {
133-
const result = plainToClass<User, Object>(User, user);
132+
it("enableCircularCheck option is true", () => {
133+
const result = plainToClass<User, Object>(User, user, { enableCircularCheck: true });
134134
sinon.assert.called(isCircularSpy);
135-
});
135+
});
136136
});
137137
});

0 commit comments

Comments
 (0)