diff --git a/v5/patch.go b/v5/patch.go index 83102e5..e42fa88 100644 --- a/v5/patch.go +++ b/v5/patch.go @@ -878,7 +878,7 @@ func ensurePathExists(pd *container, path string, options *ApplyOptions) error { } newNode := newLazyNode(newRawMessage(rawJSONArray)) - doc.add(part, newNode, options) + doc.add(decodePatchKey(part), newNode, options) doc, _ = newNode.intoAry() // Pad the new array with null values up to the required index. @@ -888,7 +888,7 @@ func ensurePathExists(pd *container, path string, options *ApplyOptions) error { } else { newNode := newLazyNode(newRawMessage(rawJSONObject)) - doc.add(part, newNode, options) + doc.add(decodePatchKey(part), newNode, options) doc, err = newNode.intoDoc(options) if err != nil { return err diff --git a/v5/patch_test.go b/v5/patch_test.go index 1f807f5..958a965 100644 --- a/v5/patch_test.go +++ b/v5/patch_test.go @@ -1245,3 +1245,26 @@ func init() { "foo": &msg, } } + +func TestEnsurePathEscapedSlash(t *testing.T) { + doc := `{ "foo": "bar" }` + patch := `[ { "op": "add", "path": "/with~1slash/nested", "value": "baz" } ]` + expected := `{"foo":"bar","with/slash":{"nested":"baz"}}` + + p, err := DecodePatch([]byte(patch)) + if err != nil { + t.Fatal(err) + } + + options := NewApplyOptions() + options.EnsurePathExistsOnAdd = true + + out, err := p.ApplyWithOptions([]byte(doc), options) + if err != nil { + t.Fatalf("Failed to apply patch: %v", err) + } + + if !compareJSON(expected, string(out)) { + t.Fatalf("Expected:\n%s\nGot:\n%s", expected, string(out)) + } +}