Skip to content

Commit 5edc97d

Browse files
authored
Merge pull request #3 from alromh87/enhancePR
Update to latest master
2 parents c397b6f + f76022e commit 5edc97d

File tree

9 files changed

+67
-49
lines changed

9 files changed

+67
-49
lines changed

commonjs/core.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,13 @@ function applyOperation(document, operation, validateOperation, mutateDocument,
185185
}
186186
while (true) {
187187
key = keys[t];
188-
if (banPrototypeModifications && key == '__proto__') {
189-
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');
188+
if (key && key.indexOf('~') != -1) {
189+
key = helpers_js_1.unescapePathComponent(key);
190+
}
191+
if (banPrototypeModifications &&
192+
(key == '__proto__' ||
193+
(key == 'prototype' && t > 0 && keys[t - 1] == 'constructor'))) {
194+
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');
190195
}
191196
if (validateOperation) {
192197
if (existingPathFragment === undefined) {
@@ -226,9 +231,6 @@ function applyOperation(document, operation, validateOperation, mutateDocument,
226231
}
227232
}
228233
else {
229-
if (key && key.indexOf('~') != -1) {
230-
key = helpers_js_1.unescapePathComponent(key);
231-
}
232234
if (t >= len) {
233235
var returnValue = objOps[operation.op].call(operation, obj, key, document); // Apply patch
234236
if (returnValue.test === false) {

commonjs/duplex.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ function _generate(mirror, obj, patches, path, invertible) {
131131
var oldVal = mirror[key];
132132
if (helpers_js_1.hasOwnProperty(obj, key) && !(obj[key] === undefined && oldVal !== undefined && Array.isArray(obj) === false)) {
133133
var newVal = obj[key];
134-
if (typeof oldVal == "object" && oldVal != null && typeof newVal == "object" && newVal != null) {
134+
if (typeof oldVal == "object" && oldVal != null && typeof newVal == "object" && newVal != null && Array.isArray(oldVal) === Array.isArray(newVal)) {
135135
_generate(oldVal, newVal, patches, path + "/" + helpers_js_1.escapePathComponent(key), invertible);
136136
}
137137
else {

module/core.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export declare function applyOperation<T>(document: T, operation: Operation, val
8181
* @param banPrototypeModifications Whether to ban modifications to `__proto__`, defaults to `true`.
8282
* @return An array of `{newDocument, result}` after the patch
8383
*/
84-
export declare function applyPatch<T>(document: T, patch: Operation[], validateOperation?: boolean | Validator<T>, mutateDocument?: boolean, banPrototypeModifications?: boolean): PatchResult<T>;
84+
export declare function applyPatch<T>(document: T, patch: ReadonlyArray<Operation>, validateOperation?: boolean | Validator<T>, mutateDocument?: boolean, banPrototypeModifications?: boolean): PatchResult<T>;
8585
/**
8686
* Apply a single JSON Patch Operation on a JSON document.
8787
* Returns the updated document.
@@ -107,5 +107,5 @@ export declare function validator(operation: Operation, index: number, document?
107107
* @param document
108108
* @returns {JsonPatchError|undefined}
109109
*/
110-
export declare function validate<T>(sequence: Operation[], document?: T, externalValidator?: Validator<T>): PatchError;
110+
export declare function validate<T>(sequence: ReadonlyArray<Operation>, document?: T, externalValidator?: Validator<T>): PatchError;
111111
export declare function _areEquals(a: any, b: any): boolean;

module/core.mjs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,13 @@ export function applyOperation(document, operation, validateOperation, mutateDoc
183183
}
184184
while (true) {
185185
key = keys[t];
186-
if (banPrototypeModifications && key == '__proto__') {
187-
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');
186+
if (key && key.indexOf('~') != -1) {
187+
key = unescapePathComponent(key);
188+
}
189+
if (banPrototypeModifications &&
190+
(key == '__proto__' ||
191+
(key == 'prototype' && t > 0 && keys[t - 1] == 'constructor'))) {
192+
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');
188193
}
189194
if (validateOperation) {
190195
if (existingPathFragment === undefined) {
@@ -224,9 +229,6 @@ export function applyOperation(document, operation, validateOperation, mutateDoc
224229
}
225230
}
226231
else {
227-
if (key && key.indexOf('~') != -1) {
228-
key = unescapePathComponent(key);
229-
}
230232
if (t >= len) {
231233
var returnValue = objOps[operation.op].call(operation, obj, key, document); // Apply patch
232234
if (returnValue.test === false) {

package-lock.json

Lines changed: 30 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* https://github.com/Starcounter-Jack/JSON-Patch
3-
* (c) 2013-2020 Joachim Wester
3+
* (c) 2013-2021 Joachim Wester
44
* MIT license
55
*/
66
declare var require: any;
@@ -247,6 +247,10 @@ export function applyOperation<T>(document: T, operation: Operation, validateOpe
247247
}
248248
while (true) {
249249
key = keys[t];
250+
if (key && key.indexOf('~') != -1) {
251+
key = unescapePathComponent(key);
252+
}
253+
250254
if(banPrototypeModifications &&
251255
(key == '__proto__' ||
252256
(key == 'prototype' && t>0 && keys[t-1] == 'constructor'))
@@ -292,9 +296,6 @@ export function applyOperation<T>(document: T, operation: Operation, validateOpe
292296
}
293297
}
294298
else {
295-
if (key && key.indexOf('~') != -1) {
296-
key = unescapePathComponent(key);
297-
}
298299
if (t >= len) {
299300
const returnValue = objOps[operation.op].call(operation, obj, key, document); // Apply patch
300301
if (returnValue.test === false) {

test/spec/json-patch-tests/tests.json.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,11 @@ export default [
268268
"patch": [{"op": "move", "from": "/foo", "path": "/bar"}],
269269
"expected": {"baz": [{"qux": "hello"}], "bar": 1} },
270270

271+
{ "comment": "Move handles escaped paths",
272+
"doc": {"foo/": {"bar/": 1, "baz": 1}},
273+
"patch": [{"op": "move", "from": "/foo~1/bar~1", "path": "/bar"}],
274+
"expected": {"bar": 1, "foo/": {"baz": 1}} },
275+
271276
{ "doc": {"baz": [{"qux": "hello"}], "bar": 1},
272277
"patch": [{"op": "move", "from": "/baz/0/qux", "path": "/baz/1"}],
273278
"expected": {"baz": [{}, "hello"], "bar": 1} },

0 commit comments

Comments
 (0)