Repository navigation
Open
Description
Three related path and history bugs in src/engine.ts break round-trip change replay, atomic rollback on failed imports, and diff path escaping.
// repro.mjs
import { Engine } from '@maxjay/patchwork';
// 1. add() creating nodes or appending with [-] records dotted paths
const e1 = new Engine({ items: ['a'] });
e1.add("$['my key']['x']", 1);
e1.add('$.items[-]', 'b');
e1.add("$['a.b']", 2);
console.log('1a:', JSON.stringify(e1.exportChanges()));
try {
new Engine({ items: ['a'] }).importChanges(e1.exportChanges());
} catch (err) {
console.log('1b:', err.message);
}
const eDot = new Engine({});
eDot.add("$['a.b']", 2);
const eDotReplay = new Engine({});
eDotReplay.importChanges(eDot.exportChanges());
console.log('1c:', JSON.stringify(eDot.draft), 'vs', JSON.stringify(eDotReplay.draft));
// 2. importChanges() does not roll back on error and reports index 0
const e2 = new Engine({ a: 1, b: [1, 2] });
try {
e2.importChanges([
{ op: 'replace', path: '$.a', value: 2 },
{ op: 'add', path: '$.b[0]', value: 9 },
{ op: 'move', from: '$.nope', to: '$.c' },
]);
} catch (err) {
console.log('2a:', err.message);
}
console.log('2b:', JSON.stringify(e2.draft));
// 3. diff() does not escape ' or \ in object keys
const e3 = new Engine({ "it's": 1, 'back\\slash': 2 });
e3.replace(`$["it's"]`, 5);
e3.replace(`$["back\\\\slash"]`, 6);
console.log('3a diff:', e3.diff().map(d => d.path).join(', '));
console.log('3b get: ', e3.get('$.*').map(d => d.path).join(', '));
try {
e3.get(e3.diff()[0].path);
} catch (err) {
console.log('3c:', err.message);
}Run with:
npm init -y && npm pkg set type=module && npm i @maxjay/patchwork@0.18.0
node repro.mjsActual output:
1a: [{"op":"add","path":"$.my key.x","value":1},{"op":"add","path":"$.items.1","value":"b"},{"op":"add","path":"$.a.b","value":2}]
1b: Failed to import changes at operation index 0: Expected ".", "..", "[", or [\t-\n\r ] but "k" found.
1c: {"a.b":2} vs {"a":{"b":2}}
2a: Failed to import changes at operation index 0: Move source must resolve to exactly one path, got 0
2b: {"a":2,"b":[9,1,2]}
3a diff: $['it's'], $['back\slash']
3b get: $['it\'s'], $['back\\slash']
3c: Expected ",", "]", or [\t-\n\r ] but "s" found.
Expected behavior:
upsertAt(src/engine.ts:873) buildsop.pathas'$.' + segments.join('.'), which produces invalid JSONPath on array indices ($.items.1) or spaces ($.my key.x) and turns keys with dots ($['a.b']) into nested paths ({"a":{"b":2}});exportChanges()should emit a valid JSONPath (such asjsonPathor a normalized path) soimportChanges()reproducesdraft.importChanges(src/engine.ts:559-589) never incrementsprogress, so the rollback loop never runs and errors always report index0; when op 2 fails, ops 0 and 1 should be undone (e2.draftrestored to{"a":1,"b":[1,2]}) and the error should reportoperation index 2.diffNode(src/engine.ts:747, andextractKeyMapat line 97) formats keys as`${path}['${key}']`without escaping'as\'or\as\\per RFC 9535 Section 2.7, unlikeget();diff()should emit$['it\'s']and$['back\\slash']so diff paths can be passed toget(),replace(), andrestore().
Versions tested: @maxjay/patchwork@0.18.0 (npm latest) and main (ade3726), Node.js v26.13.0.
Credit: https://ucsbplse.github.io/
Metadata
Metadata
Assignees
Labels
No labels
Activity