Skip to content

Commit 9d266f4

Browse files
author
Theo van der Donk
committed
Fixed issue 102
1 parent 1d0ad9e commit 9d266f4

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/jsonpath.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ JSONPath.toPathArray = function (expr) {
790790
return !match || !match[1] ? exp : subx[match[1]];
791791
});
792792
cache[expr] = exprList;
793-
return cache[expr];
793+
return cache[expr].concat();
794794
};
795795

796796
export {JSONPath};

test/test.toPath.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,19 @@ describe('JSONPath - toPath*', function () {
4646

4747
assert.strictEqual(path, originalPath);
4848
});
49+
50+
it('toPathArray (cache issue)', () => {
51+
// We test here a bug where toPathArray did not return a clone of the cached
52+
// array. As a result, the evaluate call corrupted the cached value instead
53+
// of its local copy.
54+
55+
// Make the path unique by including the test name 'cacheissue' in the path
56+
// because we do not want it to be in the cache already.
57+
const expected = ['$', 'store', 'bicycle', 'cacheissue'];
58+
const path = "$.store['bicycle'].cacheissue";
59+
const json = {};
60+
jsonpath({json, path, wrap: false});
61+
const result = jsonpath.toPathArray(path);
62+
assert.deepEqual(result, expected);
63+
});
4964
});

0 commit comments

Comments
 (0)