KevinDockx / JsonPatch

JSON Patch (JsonPatchDocument) RFC 6902 implementation for .NET
MIT License
174 stars 28 forks source link

Add on array recognized as path on property #88

Closed dpescatore closed 5 years ago

dpescatore commented 6 years ago

Hello! I'm trying to add an element to an array with a patch like this:

[{
"op": "add",
"path": "/links/8",
"value": {
    "linkName": "test",
    "linkValue": "http://test.com",
    "id": 0,
    "valid": true
}
}] 

Unfortunately I'm getting this error: TargetLocationAtPathSegmentNotFound

It seems that the lib is checking not for the array position but for a path called links/4 and obviously it is not able to find it. I've also tried with links/- but there is the same problem.

My model on server is written using Entity Framework Code First approach.

So I have an object like this:

[Table("XXX.Websites")]
public partial class Website : Entity
{
    [StringLength(255)]
    public string Name { get; set; }        
    public virtual ICollection<Link> Links { get; set; }
}

Any suggestions on this?

chrisac309 commented 5 years ago

If you're trying to add to an array at location 8, the issue could arise where you are trying to add at position 8 to an array that doesn't have that index, hence the TargetLocationAtPathSegmentNotFound -- it's trying to add to the array at an index that doesn't exist I'd recommend removing the "/8" from the path, making it so that you are adding the new "link" to the array, but not by position.