Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
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
12 changes: 7 additions & 5 deletions commonjs/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,13 @@ function applyOperation(document, operation, validateOperation, mutateDocument,
}
while (true) {
key = keys[t];
if (banPrototypeModifications && key == '__proto__') {
throw new TypeError('JSON-Patch: modifying `__proto__` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README');
if (key && key.indexOf('~') != -1) {
key = helpers_js_1.unescapePathComponent(key);
}
if (banPrototypeModifications &&
(key == '__proto__' ||
(key == 'prototype' && t > 0 && keys[t - 1] == 'constructor'))) {
throw new TypeError('JSON-Patch: modifying `__proto__` or `constructor/prototype` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README');
}
if (validateOperation) {
if (existingPathFragment === undefined) {
Expand Down Expand Up @@ -226,9 +231,6 @@ function applyOperation(document, operation, validateOperation, mutateDocument,
}
}
else {
if (key && key.indexOf('~') != -1) {
key = helpers_js_1.unescapePathComponent(key);
}
if (t >= len) {
var returnValue = objOps[operation.op].call(operation, obj, key, document); // Apply patch
if (returnValue.test === false) {
Expand Down
2 changes: 1 addition & 1 deletion commonjs/duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ function _generate(mirror, obj, patches, path, invertible) {
var oldVal = mirror[key];
if (helpers_js_1.hasOwnProperty(obj, key) && !(obj[key] === undefined && oldVal !== undefined && Array.isArray(obj) === false)) {
var newVal = obj[key];
if (typeof oldVal == "object" && oldVal != null && typeof newVal == "object" && newVal != null) {
if (typeof oldVal == "object" && oldVal != null && typeof newVal == "object" && newVal != null && Array.isArray(oldVal) === Array.isArray(newVal)) {
_generate(oldVal, newVal, patches, path + "/" + helpers_js_1.escapePathComponent(key), invertible);
}
else {
Expand Down
4 changes: 2 additions & 2 deletions module/core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export declare function applyOperation<T>(document: T, operation: Operation, val
* @param banPrototypeModifications Whether to ban modifications to `__proto__`, defaults to `true`.
* @return An array of `{newDocument, result}` after the patch
*/
export declare function applyPatch<T>(document: T, patch: Operation[], validateOperation?: boolean | Validator<T>, mutateDocument?: boolean, banPrototypeModifications?: boolean): PatchResult<T>;
export declare function applyPatch<T>(document: T, patch: ReadonlyArray<Operation>, validateOperation?: boolean | Validator<T>, mutateDocument?: boolean, banPrototypeModifications?: boolean): PatchResult<T>;
/**
* Apply a single JSON Patch Operation on a JSON document.
* Returns the updated document.
Expand All @@ -107,5 +107,5 @@ export declare function validator(operation: Operation, index: number, document?
* @param document
* @returns {JsonPatchError|undefined}
*/
export declare function validate<T>(sequence: Operation[], document?: T, externalValidator?: Validator<T>): PatchError;
export declare function validate<T>(sequence: ReadonlyArray<Operation>, document?: T, externalValidator?: Validator<T>): PatchError;
export declare function _areEquals(a: any, b: any): boolean;
12 changes: 7 additions & 5 deletions module/core.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,13 @@ export function applyOperation(document, operation, validateOperation, mutateDoc
}
while (true) {
key = keys[t];
if (banPrototypeModifications && key == '__proto__') {
throw new TypeError('JSON-Patch: modifying `__proto__` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README');
if (key && key.indexOf('~') != -1) {
key = unescapePathComponent(key);
}
if (banPrototypeModifications &&
(key == '__proto__' ||
(key == 'prototype' && t > 0 && keys[t - 1] == 'constructor'))) {
throw new TypeError('JSON-Patch: modifying `__proto__` or `constructor/prototype` prop is banned for security reasons, if this was on purpose, please set `banPrototypeModifications` flag false and pass it to this function. More info in fast-json-patch README');
}
if (validateOperation) {
if (existingPathFragment === undefined) {
Expand Down Expand Up @@ -224,9 +229,6 @@ export function applyOperation(document, operation, validateOperation, mutateDoc
}
}
else {
if (key && key.indexOf('~') != -1) {
key = unescapePathComponent(key);
}
if (t >= len) {
var returnValue = objOps[operation.op].call(operation, obj, key, document); // Apply patch
if (returnValue.test === false) {
Expand Down
58 changes: 30 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* https://github.com/Starcounter-Jack/JSON-Patch
* (c) 2013-2020 Joachim Wester
* (c) 2013-2021 Joachim Wester
* MIT license
*/
declare var require: any;
Expand Down Expand Up @@ -247,6 +247,10 @@ export function applyOperation<T>(document: T, operation: Operation, validateOpe
}
while (true) {
key = keys[t];
if (key && key.indexOf('~') != -1) {
key = unescapePathComponent(key);
}

if(banPrototypeModifications &&
(key == '__proto__' ||
(key == 'prototype' && t>0 && keys[t-1] == 'constructor'))
Expand Down Expand Up @@ -292,9 +296,6 @@ export function applyOperation<T>(document: T, operation: Operation, validateOpe
}
}
else {
if (key && key.indexOf('~') != -1) {
key = unescapePathComponent(key);
}
if (t >= len) {
const returnValue = objOps[operation.op].call(operation, obj, key, document); // Apply patch
if (returnValue.test === false) {
Expand Down
5 changes: 5 additions & 0 deletions test/spec/json-patch-tests/tests.json.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ export default [
"patch": [{"op": "move", "from": "/foo", "path": "/bar"}],
"expected": {"baz": [{"qux": "hello"}], "bar": 1} },

{ "comment": "Move handles escaped paths",
"doc": {"foo/": {"bar/": 1, "baz": 1}},
"patch": [{"op": "move", "from": "/foo~1/bar~1", "path": "/bar"}],
"expected": {"bar": 1, "foo/": {"baz": 1}} },

{ "doc": {"baz": [{"qux": "hello"}], "bar": 1},
"patch": [{"op": "move", "from": "/baz/0/qux", "path": "/baz/1"}],
"expected": {"baz": [{}, "hello"], "bar": 1} },
Expand Down
Loading