From 5809572f1957a76e6c6137ca2b9fd5640b96eb0d Mon Sep 17 00:00:00 2001 From: Gireesh Punathil Date: Tue, 17 Nov 2020 15:02:07 +0530 Subject: [PATCH] lib: remove visited objects from stack Adding visited objects in the stack was helpful in detecting circular references. However, the objects which were processed were never removed. This causes unexpected issues in stringification. Fixes: https://github.com/ibmruntimes/yieldable-json/issues/24 PR-URL: https://github.com/ibmruntimes/yieldable-json/pull/30 Reviewed-By: Harshitha K P Reviewed-By: Ravali Yatham --- package.json | 2 +- test/test-stringify-same-object-twice.js | 23 +++++++++++++++++++++++ yieldable-stringify.js | 2 ++ 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/test-stringify-same-object-twice.js diff --git a/package.json b/package.json index 7730783..7323cd6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "yieldable-json", - "version": "2.0.0", + "version": "2.0.1", "main": "index.js", "description": "An asynchronous yieldable version of JSON.stringify and JSON.parse", "author": "Gireesh Punathil ", diff --git a/test/test-stringify-same-object-twice.js b/test/test-stringify-same-object-twice.js new file mode 100644 index 0000000..2832100 --- /dev/null +++ b/test/test-stringify-same-object-twice.js @@ -0,0 +1,23 @@ +'use strict'; + +const yj = require('../index'); +const tap = require('tap'); + +const obj = { + name: 'Jacqueline Poole', + gender: 'female', + age: 40, +}; + +const master = {arr: [ { a: obj }, { b: obj} ] }; + +// Make sure presence of obj twice in the master +// object does not cause revisit issues while +// stringifying it - such as circular dependency + +yj.stringifyAsync(obj, (err, str) => { + if (!err) { + tap.ok(true, 'Repeated object presence cause no issues'); + } else + tap.fail(err); +}); diff --git a/yieldable-stringify.js b/yieldable-stringify.js index 0965854..b3814f6 100755 --- a/yieldable-stringify.js +++ b/yieldable-stringify.js @@ -224,7 +224,9 @@ function * stringifyYield(field, container, replacer, space, intensity) { ? ': ' : ':') + val); } + objStack = objStack.filter((v, i, a) => { return v !== value[key] }); } + objStack = objStack.filter((v, i, a) => { return v !== value }); } return getResult(true); default: