Skip to content
This repository was archived by the owner on Nov 15, 2018. It is now read-only.

Incorrect move operation behavior #61

Closed
TFleury opened this issue Feb 7, 2017 · 6 comments
Closed

Incorrect move operation behavior #61

TFleury opened this issue Feb 7, 2017 · 6 comments
Assignees

Comments

@TFleury
Copy link
Contributor

TFleury commented Feb 7, 2017

When performing a move operation, target object is not moved, but it is removed from source and a copy is added to the destination.
There is no problem with value types, but there is one with reference types : reference was lost.

To handle move operation, ObjectAdapter gets the moved property, removes it from the source, then adds it to the destination. That's OK.

The issue is in ListAdapter, PocoAdapter and ExpandoAdapter (don't know why, but not in DictionaryAdapter), in TryConvertValue method where the value is copied regardless of its type.

If object's type is compatible with target property's type, the reference should be kept.

@kichalla
Copy link
Member

kichalla commented Feb 7, 2017

@TFleury Could you share a repro where you are seeing this issue?

@TFleury
Copy link
Contributor Author

TFleury commented Feb 7, 2017

I'd want to patch a Template object, which have a property of type IList<Theme>. Themes in the list are ordered.

Here is a simplified version of my classes :

public class Template {
    private TrackedList<Theme> _theme = new TrackedList<Theme>();
    public string Label { get; set; }
    public IList<Theme> Themes {
        retrun _themes;
    }
}

public class Theme {
    private TrackedList<Theme> _theme = new TrackedList<Theme>();
    public string Label { get; set; }
    public IList<Theme> Themes {
        retrun _themes;
    }
}

Here is a sample patch :

[
{op:"move",from:"/themes/3",path:"/themes/1"},
{op:"move",from:"/themes/4/themes/1",path:"/themes/2"}
]

TrackedList is a custom form of List to track items order, and wanted to use JsonPatch's "move" operation to reorder items in the list.

The issue is that moved item is copied, removed, and then the copy is inserted at the destination position : the reference is lost.
As a Theme can have child Themes, moved one and its children are removed, and re-created.

It's backed in a database with EntityFramework, and I have to keep reference of objects to know which entry is new, removed or updated.

TFleury added a commit to TFleury/JsonPatch that referenced this issue Feb 7, 2017
@TFleury
Copy link
Contributor Author

TFleury commented Feb 8, 2017

@kichalla can you confirm this is an issue and not the expected behavior ?
I pushed a PR to fix it, but just realized it will break 'copy' operation : object must be deep copied.

@TFleury
Copy link
Contributor Author

TFleury commented Feb 8, 2017

I fixed the copy issue in my PR, and added matching unit tests.

@Eilon
Copy link
Member

Eilon commented Feb 23, 2017

@kichalla can you confirm you agree with the issue?

@kichalla
Copy link
Member

@Eilon and I discussed about this and agree that this is an issue. Changing the label to bug.

@kichalla kichalla added bug and removed investigate labels Feb 27, 2017
@Eilon Eilon added this to the 2.0.0 milestone Feb 27, 2017
TFleury added a commit to TFleury/JsonPatch that referenced this issue Mar 4, 2017
TFleury added a commit to TFleury/JsonPatch that referenced this issue Mar 4, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants