Skip to content

Commit bad4983

Browse files
recktermarcbachmann
authored andcommitted
Fix null values throwing exception when traversing over while getting
1 parent a5706e8 commit bad4983

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

jsonpointer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ function get (obj, pointer) {
7373
obj = obj[untilde(pointer[p++])]
7474
if (len === p) return obj
7575
if (typeof obj !== 'object') return undefined
76+
if (obj === null) return null
7677
}
7778
}
7879

test.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ var obj = {
88
},
99
d: {
1010
e: [{ a: 3 }, { b: 4 }, { c: 5 }]
11-
}
11+
},
12+
nullValue: null
1213
}
1314

14-
assert.strictEqual(jsonpointer.get(obj, '/a'), 1)
15-
assert.strictEqual(jsonpointer.get(obj, '/b/c'), 2)
16-
assert.strictEqual(jsonpointer.get(obj, '/d/e/0/a'), 3)
17-
assert.strictEqual(jsonpointer.get(obj, '/d/e/1/b'), 4)
18-
assert.strictEqual(jsonpointer.get(obj, '/d/e/2/c'), 5)
15+
assert.equal(jsonpointer.get(obj, '/nullValue'), null)
16+
assert.equal(jsonpointer.get(obj, '/nullValue/e'), null)
1917

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

129+
130+
// compile read null value
131+
var compileWithNullValue = { foo: 'bar' }
132+
var pointerNullValue = jsonpointer.compile('/foo2/baz')
133+
assert.equal(pointer.get(pointerNullValue), null)
134+
131135
var b = {}
132136
jsonpointer.set({}, '/constructor/prototype/boo', 'polluted')
133137
assert(!b.boo, 'should not boo')

0 commit comments

Comments
 (0)