myquay / JsonPatch

Adds JSON Patch support to ASP.NET Web API (http://tools.ietf.org/html/rfc6902)
MIT License
99 stars 30 forks source link

Issue replacing an List<T> property #26

Closed fscopel closed 8 years ago

fscopel commented 8 years ago

Let me explain the issue with an example: Client data being sent to server:

[
  { "op": "replace", "path": "/Name", "value": "Foo"},   
  { "op": "replace", "path": "/SiteMenuItemExclusion", "value": [{"SiteId":"157b2110-b253-462c-86ca-366c43f19c4c"}]},
]

Server side code:

public HttpResponseMessage Patch(Guid menuItemId, JsonPatchDocument<MenuItem> patchData)
        {
                //Validation and other things  
                patchData.ApplyUpdatesTo(menuItemEntity);
                dbContext.SaveChanges();
               //more irrelevant code for this example here
         }

The above will in fact replace the value of the Name property of the entity to "Foo" but instead of replacing the value of the SiteMenuItemExclusion property it will add that SiteId to it.

I also tried:

[
  { "op": "replace", "path": "/Name", "value": "Item with site exclusion 3"},   
  { "op": "remove", "path": "/SiteMenuItemExclusion"},
  { "op": "replace", "path": "/SiteMenuItemExclusion", "value": [{"SiteId":"157b2110-b253-462c-86ca-366c43f19c4c"}]},
]

But that does not seem to work.

fscopel commented 8 years ago

This is actually not an issue. The problem is with lazy loading. I just needed to make sure I eagerly loaded the data, for it to work properly.