Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions v5/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions v5/patch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}