aspnet / JsonPatch

[Archived] JSON PATCH library. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
103 stars 48 forks source link

Move forward in a nested list fails #64

Closed TFleury closed 7 years ago

TFleury commented 7 years ago

Assume you have a nestable class like this one :

public class Nestable
    {
        public string Label { get; set; }

        public List<Nestable> Children { get; set; }

        public Nestable()
        {
            this.Children = new List<Nestable>();
        }
    }

Your have this object :

{
    Label: "object 1"
    Children: [
        { Label: "object 1.1", Children: [] },
        { Label: "object 1.2", Children: [] }
    ]
}

And you want to move 1.1 into 1.2. The operation should be :

[{
    op: "move",
    from: "/Children/0",
    path: "/Children/1/Children/-"
}]

And you expect to obtain :

{
    Label: "object 1"
    Children: [
        { Label: "object 1.2", Children: [
            { Label: "object 1.1", Children: [] }
        ] }
    ]
}

Correct ?

But it fails with : The index value provided by path segment '1' is out of bounds of the array size.

It's because move gets value, removes source and add at destination. but in this special case, destination has moved...

TFleury commented 7 years ago

It seems my interpretation was wrong.

RFC says :

This operation is functionally identical to a "remove" operation on the "from" location, followed immediately by an "add" operation at the target location with the value that was just removed.

The correct patch should be

[{
    op: "move",
    from: "/Children/0",
    path: "/Children/0/Children/-"
}]