evanphx / json-patch

A Go library to apply RFC6902 patches and create and apply RFC7386 patches
BSD 3-Clause "New" or "Revised" License
1.03k stars 181 forks source link

move fails with "missing value" if from is "" #192

Closed lucastheisen closed 6 months ago

lucastheisen commented 6 months ago

According to the JSON Pointer RFC 6901:

The following JSON strings evaluate to the accompanying values:

""           // the whole document

But using that as a from results in a missing value error.

Adding this test will demonstrate the bug:

func TestMoveFromRoot(t *testing.T) {
    expected := `{"baz":{"foo":"bar"}}`
    res, err := applyPatch(`{"foo":"bar"}`, `[{"op":"move","from":"","path":"/baz"}]`)
    if err != nil {
        t.Errorf("unexpected error: %+v", err)
    } else if res != expected {
        t.Errorf("expected:\n%s\ngot:\n%s", expected, res)
    }
}
lucastheisen commented 6 months ago

Well, i just saw this in the RFC for JSON Patch:

The "from" location MUST NOT be a proper prefix of the "path" location; i.e., a location cannot be moved into one of its children.

So my use case is invalid (as is the test per-se)... But the lack of support for "" JSON Pointer may still be something you want to address. You can close this issue out if you would like, or modify it to deal with the JSON Pointer support.

evanphx commented 6 months ago

@lucastheisen Gotcha gotcha. Trying some other json patch implementations, they largely fail to handle when from is "", so I think we're in a bit of uncharted waters here.

evanphx commented 6 months ago

Could you have a look at #193 and give me your thoughts? Thanks!

lucastheisen commented 6 months ago

@evanphx , that merge looks great.