JsonPatch is a simple library which adds JSON Patch support to .NET 6 - ASP.NET Core Minimal Web API (http://tools.ietf.org/html/rfc6902).
You can get it on NuGet here: https://www.nuget.org/packages/JsonPatch/
endpoints.MapMethods("/some-resource/{id}", new[] { "PATCH" }, async (Guid id, JsonPatchDocument<SomeDto> model) =>
{
//Remember to do some validation and all that fun stuff
var objectToUpdate = repository.GetById(id);
repository.Save(patchData.ApplyTo(objectToUpdate));
});
Yep - that's it. You can now use PATCH on your flash new Minimal API (https://docs.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-6.0)
The main thing is to make sure the content type is "application/json-patch+json" otherwise Web API won't invoke the JsonPatch media formatter. Here's an example.
PATCH /my/data HTTP/1.1
Host: example.org
Content-Type: application/json-patch+json
[
{ "op": "add", "path": "/a/b/c", "value": "foo" }
]
The path specified in the patch request can be resolved to a different property on the model using a resolver.
E.g. the ExactCasePropertyPathResolver will match a property based on an exact match
JsonPatch example:
var jsonPatchSettings = new JsonPatchSettings
{
PathResolver = new ExactCasePropertyPathResolver(new JsonValueConverter())
};
JsonPatchCore example:
JsonPatchSettings.Options = new JsonPatchOptions
{
PathResolver = new ExactCasePropertyPathResolver(new JsonValueConverter()),
RequireJsonPatchContentType = false,
};
Now the request
PATCH /my/data HTTP/1.1
Host: example.org
Content-Type: application/json-patch+json
[
{ "op": "add", "path": "/sampleProperty", "value": "foo" }
]
Will be no longer be valid for the class
{
[JsonProperty("sampleProperty")]
public string SampeProperty {get;set;}
}
Available resolvers are
You can add your own by extending BaseResolver and defining how to get the property from the type.
Because we are applying updates to C# objects rather than JSON objects we differ from the spec slightly, here are some points of difference.
The JSON patch document uses JSON pointers to refer to properties, we don't accept paths in the following formats:
We only support a subset of available operations at this moment, add, remove, and replace.
Since C# is a typed language we have a few restrictions, hence the operations work a little differently than specified in the specification.
Here's our flavour: