From 6e5ef5026e6ee7e6971d39333141e7dff8f1a88c Mon Sep 17 00:00:00 2001 From: Slawomir Chylek Date: Mon, 19 Feb 2018 09:27:19 +0100 Subject: [PATCH] Fix for testing objects/arrays against null. --- LICENSE | 1 + patch.go | 8 ++++++++ patch_test.go | 12 ++++++++++++ 3 files changed, 21 insertions(+) diff --git a/LICENSE b/LICENSE index 0eb9b72..382d7de 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,5 @@ Copyright (c) 2014, Evan Phoenix + 2018, Google LLC All rights reserved. Redistribution and use in source and binary forms, with or without diff --git a/patch.go b/patch.go index de70dbc..42cf867 100644 --- a/patch.go +++ b/patch.go @@ -104,6 +104,10 @@ func (n *lazyNode) compact() []byte { } func (n *lazyNode) tryDoc() bool { + if n.raw == nil { + return false + } + err := json.Unmarshal(*n.raw, &n.doc) if err != nil { @@ -115,6 +119,10 @@ func (n *lazyNode) tryDoc() bool { } func (n *lazyNode) tryAry() bool { + if n.raw == nil { + return false + } + err := json.Unmarshal(*n.raw, &n.ary) if err != nil { diff --git a/patch_test.go b/patch_test.go index 40ff5af..9b441c7 100644 --- a/patch_test.go +++ b/patch_test.go @@ -314,6 +314,18 @@ var TestCases = []TestCase{ true, "", }, + { + `{ "foo": {} }`, + `[ { "op": "test", "path": "/foo", "value": null } ]`, + false, + "/foo", + }, + { + `{ "foo": [] }`, + `[ { "op": "test", "path": "/foo", "value": null } ]`, + false, + "/foo", + }, { `{ "baz/foo": "qux" }`, `[ { "op": "test", "path": "/baz~1foo", "value": "qux"} ]`,