dotnet / aspnetcore Public
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for Json Merge Patch - RFC7396 #2436
Comments
From @vhe1 on Tuesday, February 21, 2017 3:35:44 AM Seconded. |
From @gilmishal on Sunday, June 4, 2017 7:40:06 AM Third. |
From @Mardoxx on Thursday, August 24, 2017 8:08:28 AM This would be great. Seems a lot more appropriate for what I'd imagine the "average" user requires of JSON Patch. |
From @Mardoxx on Sunday, August 27, 2017 6:02:01 PM What does the Has an API for this already been decided? Or is it in discussion -- if so is this public? I have been playing around a bit and I've got a working PoC which allows for uhhh.. this: // Normal DTO/model
public class PersonDetailsUpdate
{
public string GivenName { get; set; }
public string Surname { get; set; }
public int Age { get; set; }
public int? GroupId { get; set; }
} [HttpPatch("Users/{id:int}")]
public IActionResult Patch([FromRoute]int id, JsonMergeDocument<PersonDetailsUpdate> mergeDocument)
{
JsonMergeDocumentMember<int?> groupIdMember = mergeDocument.Member(x => x.GroupId);
if (groupIdMember.IsSet)
{
int? newGroupId = groupIdMember.Value;
if (newGroupId.HasValue) {
// Throw if group is not valid
_groupsService.SetUserGroup(id, newGroupId);
}
_groupsService.UnSetUserGroup(id);
}
using (UsersDbContext usersContext = new UsersDbContext())
{
// Check if user exists
UserEntity user = new UserEntity
{
Id = id
};
EntityEntry entity = dbContext.Users.Attach(dbUser);
foreach(IEnumerable<JsonMergeDocumentMember> member in mergeDocument.Members.Where(x => x.IsSet))
{
PropertyEntry entityProp = entity.Member(member.Name) as PropertyEntry;
if (entityProp != null)
{
if (entityProp.MetaData.ClrType != member.Type)
{
// perhaps try convert type and set
}
entityProp.CurrentValue = member.Value;
}
}
usersContext.SaveChanges();
}
return NoContent();
} Assume the following data is persisted:
With group information being stored in a completely isolated service. That is, a {
"UserId": 10,
"FirstName": "Mardoxx",
"Surname": "",
"Age": 99,
"GroupId" : 100
} Sending {
"Age": 20,
"GroupId" : null
} Will update the user's age to 20, and remove the user from its group, i.e. GroupId to null. A {
"UserId": 10,
"FirstName": "Mardoxx",
"Surname": "",
"Age": 20,
"GroupId" : null
}
This is the sort of functionality I would like from an official impl of Json Merge Patch. Validation is a necessary addition to this, so is an An official solution to something like this would be really desirable! |
From @peabnuts123 on Wednesday, August 30, 2017 8:25:07 PM Just pitching in from a front-end-dev perspective. Granted that JSON Patch is a nice standard but it's pretty widely accepted to do operations in a JSON-Merge kind of way. To a JS developer, it would be pretty weird if their backend developer told them they couldn't support this kind of operation, it would be generally seen as basically an incapable/not very useful technology. |
From @Mardoxx on Friday, September 1, 2017 7:57:37 AM Ok so I've cleaned it up a bit, API is a little different now, Shall I write a few tests and do a PR? Or has work on this already begun? I want this to be |
From @mhosman on Friday, September 1, 2017 8:20:52 AM +1 For this. Please add support for Json Merge Patch! |
From @rynowak on Friday, September 1, 2017 10:46:45 AM @Mardoxx - we'll discuss and get back to you next week. We're still planning the set of features we're going to do for 2.1, and this is under consideration. I'm putting a reminder on my calendar right now. If you have an example of this somewhere in a repo that we can look at that would help. |
From @tinchoel10 on Friday, September 1, 2017 11:57:48 AM +1 Please add json merge patch, this is more client-friendly, easier to understand and to manage in a lot of situations. Is a very important feature. |
From @Mardoxx on Friday, September 1, 2017 2:42:08 PM @rynowak Amazing thanks. Done here - https://github.com/Mardoxx/RJBM.JsonMergePatch Cleaned it up a lot and simplified it... a lot. Hacked together a handful of tests that should give an idea of behaviour. Not happy about a few things, see here, but happy enough about general API for it (not necessarily the names of things though!). Gives a basic idea of what I want. Can add end to end example if you like? |
From @mhosman on Monday, September 4, 2017 10:27:36 AM Hey @Mardoxx, this is a working (non-official) example? I know there are several things to work in it (Mardoxx/RJBM.JsonMergePatch#1), but it works? |
From @rynowak on Tuesday, September 5, 2017 10:20:09 AM @Mardoxx - have you done any work with your code to support cases like the following: Original
Patch
After
Notice in this case that From the .NET point-of-view this seems like the hard part. Here's the pseudo-code from the spec (emphasis mine)
Some of the other challenges that we've run into implementing patch are around casing and the behavior of dictionary types and dynamic types. JSON-Patch and JSON-Merge (and JSON) in general are case-sensitive. We banished most of these issues recently by using |
From @Mardoxx on Tuesday, September 5, 2017 10:43:31 AM Totally overlooked that, will have a look later - and also adding properties that don't exist on the original document (i.e. source is a dynamic object)... Haven't looked at (didn't occur to me) Json.Net api! Good idea. Ok, this is very difficult. Does not really fit in with the way I wrote it. Back to the drawing board! |
From @rynowak on Friday, September 15, 2017 11:55:25 AM Hi Folks, sorry for the delay. We don't have an official 2.1 announcement post out yet, but I'm sad to say JSON Merge didn't make the cut. We understand the feedback that this is valuable and we'd like for a good .NET solution to exist, but we're not going to invest in building it right now. I'd suggest teaming up and fleshing out the implementation that @Mardoxx has started working on. We'd be happy to provide advice and help promote the library if it takes off. |
From @Mardoxx on Friday, September 15, 2017 12:38:11 PM @rynowak Understandable, it's something that comparatively few will benefit from. Not at all certain about the current implementation I came up with (besides it only conforming to like 1/3rd of the spec!) -- Not had time to think about it the past few days. Some pointers from someone more experienced than I, or anyone really, would be very welcomed! @mhosman, seeing as it's only 4 files or so, feel free to just copy them to your own project. I would not feel okay publishing this anywhere too public (e.g. NuGet) until it is in an actual working state! |
From @mhosman on Monday, September 18, 2017 10:52:52 AM I just found this.... https://github.com/Morcatko/Morcatko.AspNetCore.JsonMergePatch |
From @adnan-kamili on Tuesday, September 19, 2017 3:50:38 AM Since I logged this issue, I have been using the following to perform a patch: public void Patch<TEntity, TViewModel>(TEntity entity, TViewModel updatedModel) where TEntity : class, IEntity
{
// copy the value of properties from view model into entity
PropertyInfo[] entityProperties = entity.GetType().GetProperties();
foreach (PropertyInfo entityPropertyInfo in entityProperties)
{
PropertyInfo updatedModelPropertyInfo = updatedModel.GetType().GetProperty(entityPropertyInfo.Name);
if (updatedModelPropertyInfo != null)
{
var value = updatedModelPropertyInfo.GetValue(updatedModel, null);
if (value != null)
{
entityPropertyInfo.SetValue(entity, value, null);
}
}
}
} |
From @peabnuts123 on Sunday, September 24, 2017 1:09:49 PM Seems like most people in this thread are missing the subtle difference between omitting a field from the request and sending a I found this issue because I was trying to solve this problem myself and looking for others talking about the subject. In JavaScript-land this nuance can be modelled with |
From @Mardoxx on Sunday, September 24, 2017 3:57:57 PM No, both my and that other example account for null vs undefined. On 24 Sep 2017 21:09, "Jeff" notifications@github.com wrote:
|
From @peabnuts123 on Sunday, September 24, 2017 7:54:49 PM Alright, that's my mistake. I hadn't trawled the code, and was merely acting on language cues. |
Is this coming soon as part of asp.net? |
@SatishTata I don't know when this is gonna happend (officially) but meanwhile you can use this third party library: https://github.com/Morcatko/Morcatko.AspNetCore.JsonMergePatch |
Is anything regarding this being worked on for .Net Core? |
Closing as this is not something we plan to do. Looks like the community has come up with some libraries to support this already. |
From @adnan-kamili on Wednesday, January 4, 2017 10:22:13 PM
Hi,
Are their any plans to support the Json Merge Patch
https://tools.ietf.org/html/rfc7396
Copied from original issue: aspnet/JsonPatch#52
The text was updated successfully, but these errors were encountered: