Skip to content

Commit

Permalink
- Fix: Avoid cache corruption when the returned structure is modified.
Browse files Browse the repository at this point in the history
- Fix: Avoid cache corruption when the returned structure is modified.
    Fixes JSONPath-Plus#102. (@tejodorus)

Co-authored-by: Brett Zamir <[email protected]>
  • Loading branch information
tejodorus and brettz9 authored Apr 9, 2021
1 parent 61c654f commit 2860830
Show file tree
Hide file tree
Showing 11 changed files with 25 additions and 10 deletions.
2 changes: 1 addition & 1 deletion badges/tests-badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion dist/index-browser-esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 982,7 @@ JSONPath.toPathArray = function (expr) {
return !match || !match[1] ? exp : subx[match[1]];
});
cache[expr] = exprList;
return cache[expr];
return cache[expr].concat();
};

/**
Expand Down
2 changes: 1 addition & 1 deletion dist/index-browser-esm.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index-browser-esm.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index-browser-umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 988,7 @@
return !match || !match[1] ? exp : subx[match[1]];
});
cache[expr] = exprList;
return cache[expr];
return cache[expr].concat();
};

/**
Expand Down
2 changes: 1 addition & 1 deletion dist/index-browser-umd.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index-browser-umd.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index-node-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 729,7 @@ JSONPath.toPathArray = function (expr) {
return !match || !match[1] ? exp : subx[match[1]];
});
cache[expr] = exprList;
return cache[expr];
return cache[expr].concat();
};

JSONPath.prototype.vm = vm__default['default'];
Expand Down
2 changes: 1 addition & 1 deletion dist/index-node-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 721,7 @@ JSONPath.toPathArray = function (expr) {
return !match || !match[1] ? exp : subx[match[1]];
});
cache[expr] = exprList;
return cache[expr];
return cache[expr].concat();
};

JSONPath.prototype.vm = vm;
Expand Down
2 changes: 1 addition & 1 deletion src/jsonpath.js
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 699,7 @@ JSONPath.toPathArray = function (expr) {
return !match || !match[1] ? exp : subx[match[1]];
});
cache[expr] = exprList;
return cache[expr];
return cache[expr].concat();
};

export {JSONPath};
15 changes: 15 additions & 0 deletions test/test.toPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 46,19 @@ describe('JSONPath - toPath*', function () {

assert.strictEqual(path, originalPath);
});

it('toPathArray (cache issue)', () => {
// We test here a bug where toPathArray did not return a clone of the cached
// array. As a result, the evaluate call corrupted the cached value instead
// of its local copy.

// Make the path unique by including the test name 'cacheissue' in the path
// because we do not want it to be in the cache already.
const expected = ['$', 'store', 'bicycle', 'cacheissue'];
const path = "$.store['bicycle'].cacheissue";
const json = {};
jsonpath({json, path, wrap: false});
const result = jsonpath.toPathArray(path);
assert.deepEqual(result, expected);
});
});

0 comments on commit 2860830

Please sign in to comment.