From bad49837fe07c0cecf67fc06cb1e255af55ce2b8 Mon Sep 17 00:00:00 2001 From: Hannes Guedelhoefer Date: Wed, 15 Jul 2020 17:49:11 +0200 Subject: [PATCH] Fix null values throwing exception when traversing over while getting --- jsonpointer.js | 1 + test.js | 16 ++++++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/jsonpointer.js b/jsonpointer.js index 3f6f353..1006c04 100644 --- a/jsonpointer.js +++ b/jsonpointer.js @@ -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 } } diff --git a/test.js b/test.js index 1897728..712ab90 100644 --- a/test.js +++ b/test.js @@ -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) @@ -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')