@@ -320,6 +320,53 @@ var _ = Describe("JSONPatch", func() {
320320 testPatchWithExpected (F {}, F {B : & B {Str : "don't remove me" }}, F {B : & B {Str : "don't remove me" }}, jsonpatch .WithPredicate (predicate ))
321321 })
322322 })
323+ Context ("CreateJsonPatch_with_prefix" , func () {
324+ It ("empty prefix" , func () {
325+ testPatchWithExpected (F {B : & B {Bool : true , Str : "str" }}, F {}, F {B : & B {Bool : true , Str : "str" }}, jsonpatch .WithPrefix ([]string {"" }))
326+ })
327+ It ("pointer prefix" , func () {
328+ modified := F {A : & A {B : & B {Bool : true , Str : "str" }}}
329+ current := F {A : & A {}}
330+ expected := F {A : & A {B : & B {Bool : true , Str : "str" }}}
331+
332+ currentJSON , err := json .Marshal (current )
333+ Ω (err ).ShouldNot (HaveOccurred ())
334+ _ , err = json .Marshal (modified )
335+ Ω (err ).ShouldNot (HaveOccurred ())
336+ expectedJSON , err := json .Marshal (expected )
337+ Ω (err ).ShouldNot (HaveOccurred ())
338+
339+ bytes , _ , err := jsonpatch .CreateJSONPatch (modified .A .B , current .A .B , jsonpatch .WithPrefix (jsonpatch .ParseJSONPointer ("/a/ptr" )))
340+ Ω (err ).ShouldNot (HaveOccurred ())
341+ Ω (bytes .String ()).ShouldNot (Equal ("" ))
342+ jsonPatch , err := jsonpatch2 .DecodePatch (bytes )
343+ Ω (err ).ShouldNot (HaveOccurred ())
344+ patchedJSON , err := jsonPatch .Apply (currentJSON )
345+ Ω (err ).ShouldNot (HaveOccurred ())
346+ Ω (patchedJSON ).Should (MatchJSON (expectedJSON ))
347+ })
348+ It ("string prefix" , func () {
349+ modified := F {B : & B {Bool : true , Str : "str" }}
350+ current := F {}
351+ expected := F {B : & B {Bool : true , Str : "str" }}
352+
353+ currentJSON , err := json .Marshal (current )
354+ Ω (err ).ShouldNot (HaveOccurred ())
355+ _ , err = json .Marshal (modified )
356+ Ω (err ).ShouldNot (HaveOccurred ())
357+ expectedJSON , err := json .Marshal (expected )
358+ Ω (err ).ShouldNot (HaveOccurred ())
359+
360+ bytes , _ , err := jsonpatch .CreateJSONPatch (modified .B , current .B , jsonpatch .WithPrefix ([]string {"b" }))
361+ Ω (err ).ShouldNot (HaveOccurred ())
362+ Ω (bytes .String ()).ShouldNot (Equal ("" ))
363+ jsonPatch , err := jsonpatch2 .DecodePatch (bytes )
364+ Ω (err ).ShouldNot (HaveOccurred ())
365+ patchedJSON , err := jsonPatch .Apply (currentJSON )
366+ Ω (err ).ShouldNot (HaveOccurred ())
367+ Ω (patchedJSON ).Should (MatchJSON (expectedJSON ))
368+ })
369+ })
323370 Context ("CreateJsonPatch_errors" , func () {
324371 It ("not matching types" , func () {
325372 _ , _ , err := jsonpatch .CreateJSONPatch (A {}, B {})
0 commit comments