Skip to content

Commit

Permalink
Fix null values throwing exception when traversing over while getting
Browse files Browse the repository at this point in the history
  • Loading branch information
reckter authored and marcbachmann committed Jul 13, 2022
1 parent a5706e8 commit bad4983
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions jsonpointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ function get (obj, pointer) {
obj = obj[untilde(pointer[p++])]
if (len === p) return obj
if (typeof obj !== 'object') return undefined
if (obj === null) return null
}
}

Expand Down
16 changes: 10 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ var obj = {
},
d: {
e: [{ a: 3 }, { b: 4 }, { c: 5 }]
}
},
nullValue: null
}

assert.strictEqual(jsonpointer.get(obj, '/a'), 1)
assert.strictEqual(jsonpointer.get(obj, '/b/c'), 2)
assert.strictEqual(jsonpointer.get(obj, '/d/e/0/a'), 3)
assert.strictEqual(jsonpointer.get(obj, '/d/e/1/b'), 4)
assert.strictEqual(jsonpointer.get(obj, '/d/e/2/c'), 5)
assert.equal(jsonpointer.get(obj, '/nullValue'), null)
assert.equal(jsonpointer.get(obj, '/nullValue/e'), null)

// set returns old value
assert.strictEqual(jsonpointer.set(obj, '/a', 2), 1)
Expand Down Expand Up @@ -128,6 +126,12 @@ assert.strictEqual(pointer.set(a, 'test'), 'bar')
assert.strictEqual(pointer.get(a), 'test')
assert.deepEqual(a, { foo: 'test' })


// compile read null value
var compileWithNullValue = { foo: 'bar' }
var pointerNullValue = jsonpointer.compile('/foo2/baz')
assert.equal(pointer.get(pointerNullValue), null)

var b = {}
jsonpointer.set({}, '/constructor/prototype/boo', 'polluted')
assert(!b.boo, 'should not boo')
Expand Down

0 comments on commit bad4983

Please sign in to comment.