Skip to content

Commit

Permalink
Merge pull request #812 from atsjo/json-patch
Browse files Browse the repository at this point in the history
fix(json-patch): create deep copy of "replace" operation value
  • Loading branch information
streamich authored Feb 5, 2025
2 parents 70014f2 + e84449b commit a93f230
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/json-patch/op/OpReplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {AbstractOp} from './AbstractOp';
import type {OperationReplace} from '../types';
import {find, isObjectReference, isArrayReference, type Path, formatJsonPointer} from '@jsonjoy.com/json-pointer';
import {OPCODE} from '../constants';
import {clone as deepClone} from '@jsonjoy.com/util/lib/json-clone/clone';
import type {IMessagePackEncoder} from '@jsonjoy.com/json-pack/lib/msgpack';

/**
Expand All @@ -28,9 +29,10 @@ export class OpReplace extends AbstractOp<'replace'> {
public apply(doc: unknown) {
const ref = find(doc, this.path);
if (ref.val === undefined) throw new Error('NOT_FOUND');
if (isObjectReference(ref)) ref.obj[ref.key] = this.value;
else if (isArrayReference(ref)) ref.obj[ref.key] = this.value;
else doc = this.value;
const value = deepClone(this.value);
if (isObjectReference(ref)) ref.obj[ref.key] = value;
else if (isArrayReference(ref)) ref.obj[ref.key] = value;
else doc = value;
return {doc, old: ref.val};
}

Expand Down

0 comments on commit a93f230

Please sign in to comment.