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(logger): no longer detects duplicate object as circular #1679

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
44 changes: 44 additions & 0 deletions spec/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,50 @@ describe("logger", () => {
});
});

it("should not detect duplicate object as circular", () => {
const obj: any = { a: "foo" };
const entry: logger.LogEntry = {
severity: "ERROR",
message: "testing circular",
a: obj,
b: obj,
};
logger.write(entry);
expectStderr({
severity: "ERROR",
message: "testing circular",
a: { a: "foo" },
b: { a: "foo" },
});
});

it("should not detect duplicate object in array as circular", () => {
const obj: any = { a: "foo" };
const arr: any = [
{ a: obj, b: obj },
{ a: obj, b: obj },
];
const entry: logger.LogEntry = {
severity: "ERROR",
message: "testing circular",
a: arr,
b: arr,
};
logger.write(entry);
expectStderr({
severity: "ERROR",
message: "testing circular",
a: [
{ a: { a: "foo" }, b: { a: "foo" } },
{ a: { a: "foo" }, b: { a: "foo" } },
],
b: [
{ a: { a: "foo" }, b: { a: "foo" } },
{ a: { a: "foo" }, b: { a: "foo" } },
],
});
});

it("should not break on objects that override toJSON", () => {
const obj: any = { a: new Date("August 26, 1994 12:24:00Z") };

Expand Down
28 changes: 16 additions & 12 deletions src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,32 @@ function removeCircular(obj: any, refs: any[] = []): any {
if (typeof obj !== "object" || !obj) {
return obj;
}
// If the object defines its own toJSON, prefer that.
if (obj.toJSON) {
// If the object defines its own toJSON method, use it.
if (obj.toJSON && typeof obj.toJSON === "function") {
return obj.toJSON();
}
// Only check for circularity among ancestors in the recursion stack.
if (refs.includes(obj)) {
return "[Circular]";
} else {
refs.push(obj);
}
// Add the current object to the recursion stack.
refs.push(obj);

// If the object is an array, run circular check on each element.
let returnObj: any;
if (Array.isArray(obj)) {
returnObj = new Array(obj.length);
} else {
returnObj = {};
returnObj = obj.map((item) => removeCircular(item, refs));
}
for (const k in obj) {
if (refs.includes(obj[k])) {
returnObj[k] = "[Circular]";
} else {
returnObj[k] = removeCircular(obj[k], refs);
// If the object is a Map, run circular check on each key and value.
else {
returnObj = {};
for (const key of Object.keys(obj)) {
returnObj[key] = removeCircular(obj[key], refs);
}
}

// Remove the current object from the stack once its properties are processed.
refs.pop();
return returnObj;
}

Expand Down
Loading