flitbit / json-ptr

A complete implementation of JSON Pointer (RFC 6901) for nodejs and modern browsers.
MIT License
91 stars 28 forks source link

Creation of non-existent elements in array doesn't work #11

Closed chrisknight32 closed 6 years ago

chrisknight32 commented 7 years ago

This is related to what #10 addresses. That issue fixes when arrays don't exist to begin with but there is also the problem that when you set a value using force = true, and the path leads through an array element that doesn't exist yet, you get the final value inserted into the array rather than objects being created as expected.

ptr = require('json-ptr')

a = {
    Foo: []
}

p = ptr.create('/Foo/0/Bar/Baz');

// Succeeds but with incorrect results.
p.set(a, 5, true);
// a is now { Foo: [5] }
// a should be
// {
//   Foo: [
//     {
//       Bar: {
//         Baz: 5
//       }
//     }
//   ]
// }
console.log(a);

p = ptr.create('/Foo/Bar/Baz');

b = {
    Foo: {}
}

p.set(b, 5, true);
// Works as you'd expect.
// b is now
// {
//   Foo: {
//     Bar: {
//       Baz: 5
//     }
//   }
// }
console.log(b);
flitbit commented 6 years ago

Fixed in release 1.1.0. See issue-11.js and the test