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 committed Jul 15, 2020
1 parent b67e402 commit 7d2c8bf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions jsonpointer.js
Expand Up @@ -69,6 +69,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
11 changes: 10 additions & 1 deletion test.js
Expand Up @@ -8,14 +8,17 @@ var obj = {
},
d: {
e: [{ a: 3 }, { b: 4 }, { c: 5 }]
}
},
nullValue: null
}

assert.equal(jsonpointer.get(obj, '/a'), 1)
assert.equal(jsonpointer.get(obj, '/b/c'), 2)
assert.equal(jsonpointer.get(obj, '/d/e/0/a'), 3)
assert.equal(jsonpointer.get(obj, '/d/e/1/b'), 4)
assert.equal(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.equal(jsonpointer.set(obj, '/a', 2), 1)
Expand Down Expand Up @@ -128,6 +131,12 @@ assert.equal(pointer.set(a, 'test'), 'bar')
assert.equal(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 7d2c8bf

Please sign in to comment.