Morcatko.AspNetCore.JsonMergePatch 2.0.0

JSON Merge Patch support for ASP.NET Core 2.X

Install-Package Morcatko.AspNetCore.JsonMergePatch -Version 2.0.0
dotnet add package Morcatko.AspNetCore.JsonMergePatch --version 2.0.0
<PackageReference Include="Morcatko.AspNetCore.JsonMergePatch" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Morcatko.AspNetCore.JsonMergePatch --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

JSON Merge Patch support for ASP.NET Core 2.x

Nuget

JSON Merge Patch

  • RFC 7396
  • performs partial resource update similar to JSON Patch
  • Supports Swagger
  • netstandard 2.0
C# object:
var backendModel = new Model()
{
    Name = "James Bond"
    Age = "45"
    Weapon = "Gun"
}

JSON Merge Patch:
{
    "Weapon": "Knife"
}

resulting C# object:
{
    Name = "James Bond"
    Age = "45"
    Weapon = "Knife"
}

How to

See testApp2.0 for sample

  1. Install Morcatko.AspNetCore.JsonMergePatch nuget
  2. Add to your startup class
using Morcatko.AspNetCore.JsonMergePatch;

public void ConfigureServices(IServiceCollection services)
{
    ...
    services
        .AddMvc()              // or .AddMvcCore()
        .AddJsonMergePatch();
    ...
}
  1. Use in your controller
using Morcatko.AspNetCore.JsonMergePatch;

[HttpPatch]
[Consumes(JsonMergePatchDocument.ContentType)]
public void Patch([FromBody] JsonMergePatchDocument<Model> patch)
{
    ...
    patch.ApplyTo(backendModel);
    ...
}

You can apply a patch to a different Type (be carefull, all C# static typing is ignored) - see #16 for more details.

BackendModel backendModel;
JsonMergePatch<DtoModel> patch;
patch.ApplyTo(backendModel)
  1. Swagger config (optional)

copy & paste this class into your app - https://github.com/Morcatko/Morcatko.AspNetCore.JsonMergePatch/blob/master/test/testApp2.0/JsonMergePatchDocumentOperationFilter.cs

services.AddSwaggerGen(c =>
    {
        c.OperationFilter<JsonMergePatchDocumentOperationFilter>();
    });

Options

    services
        .AddMvc()
        .AddJsonMergePatch(o => ....)
  • bool EnableDelete - Deletes items when target object is Dictionary and patched value is null

How to - unit testing

See Morcatko.AspNetCore.JsonMergePatch.Tests.Builder.Json.Simple class for more examples

Morcatko.AspNetCore.JsonMergePatch.Tests.Builder.Json
public void UnitTest()
{
    var model = new Model();
    var patch1 = PatchBuidler.Build<Model>("{ integer: 1}");
    ...
    or
    ...
    var original = new Model();
    var patched = new Model() { Integer = 1};
    var patch2 = PatchBuilder.Build(original, patched);
}

Known issues/Not working

  • ModelState.IsValid is false when a required property is missing
  • Enums with [EnumMember(Value = "....")] attribute

JSON Merge Patch support for ASP.NET Core 2.x

Nuget

JSON Merge Patch

  • RFC 7396
  • performs partial resource update similar to JSON Patch
  • Supports Swagger
  • netstandard 2.0
C# object:
var backendModel = new Model()
{
    Name = "James Bond"
    Age = "45"
    Weapon = "Gun"
}

JSON Merge Patch:
{
    "Weapon": "Knife"
}

resulting C# object:
{
    Name = "James Bond"
    Age = "45"
    Weapon = "Knife"
}

How to

See testApp2.0 for sample

  1. Install Morcatko.AspNetCore.JsonMergePatch nuget
  2. Add to your startup class
using Morcatko.AspNetCore.JsonMergePatch;

public void ConfigureServices(IServiceCollection services)
{
    ...
    services
        .AddMvc()              // or .AddMvcCore()
        .AddJsonMergePatch();
    ...
}
  1. Use in your controller
using Morcatko.AspNetCore.JsonMergePatch;

[HttpPatch]
[Consumes(JsonMergePatchDocument.ContentType)]
public void Patch([FromBody] JsonMergePatchDocument<Model> patch)
{
    ...
    patch.ApplyTo(backendModel);
    ...
}

You can apply a patch to a different Type (be carefull, all C# static typing is ignored) - see #16 for more details.

BackendModel backendModel;
JsonMergePatch<DtoModel> patch;
patch.ApplyTo(backendModel)
  1. Swagger config (optional)

copy & paste this class into your app - https://github.com/Morcatko/Morcatko.AspNetCore.JsonMergePatch/blob/master/test/testApp2.0/JsonMergePatchDocumentOperationFilter.cs

services.AddSwaggerGen(c =>
    {
        c.OperationFilter<JsonMergePatchDocumentOperationFilter>();
    });

Options

    services
        .AddMvc()
        .AddJsonMergePatch(o => ....)
  • bool EnableDelete - Deletes items when target object is Dictionary and patched value is null

How to - unit testing

See Morcatko.AspNetCore.JsonMergePatch.Tests.Builder.Json.Simple class for more examples

Morcatko.AspNetCore.JsonMergePatch.Tests.Builder.Json
public void UnitTest()
{
    var model = new Model();
    var patch1 = PatchBuidler.Build<Model>("{ integer: 1}");
    ...
    or
    ...
    var original = new Model();
    var patched = new Model() { Integer = 1};
    var patch2 = PatchBuilder.Build(original, patched);
}

Known issues/Not working

  • ModelState.IsValid is false when a required property is missing
  • Enums with [EnumMember(Value = "....")] attribute

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.0.0 3,087 6/20/2019
2.0.0-beta10 549 5/27/2019
2.0.0-beta09 933 4/11/2019
2.0.0-beta08 223 4/2/2019
2.0.0-beta07 6,266 12/13/2018
2.0.0-beta06 608 12/7/2018
2.0.0-beta05 967 11/20/2018
2.0.0-beta04 4,361 9/7/2018
2.0.0-beta03 4,107 11/6/2017
2.0.0-beta02 222 11/3/2017
2.0.0-beta01 192 10/24/2017
2.0.0-alpha02 985 9/14/2017
1.0.0-alpha01 330 6/23/2017