Morcatko.AspNetCore.JsonMergePatch.Document 3.0.1

JSON Merge Patch support for ASP.NET Core 3.X (Base Package)

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

JSON Merge Patch support for ASP.NET Core

Nuget - Morcatko.AspNetCore.JsonMergePatch (ASP.NET Core 2.x)
Nuget - Morcatko.AspNetCore.JsonMergePatch.NewtonsoftJson (ASP.NET Core 3.x)
Nuget - Morcatko.AspNetCore.JsonMergePatch.SystemText (ASP.NET Core 3.x)
Nuget - Morcatko.AspNetCore.JsonMergePatch.Document (ASP.NET Core 3.x - base package)

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 2.1-testApp or 3.0 testApp for sample

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

public void ConfigureServices(IServiceCollection services)
{
    ...
    services
        .AddMvc()                         // or .AddMvcCore()
        //.AddJsonMergePatch();           // 2.x
        //.AddNewtonsoftJsonMergePatch(); // 3.x (Newtonsoft.Josn)
        //.AddSystemTextJsonMergePatch(); // 3.x (System.Text)
    ...
}
  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

  • 2.x - https://github.com/Morcatko/Morcatko.AspNetCore.JsonMergePatch/blob/master/src/2.1-testApp/JsonMergePatchDocumentOperationFilter.cs
  • 3.x - https://github.com/Morcatko/Morcatko.AspNetCore.JsonMergePatch/blob/master/src/3.0-testApp/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 tests in ...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

Nuget - Morcatko.AspNetCore.JsonMergePatch (ASP.NET Core 2.x)
Nuget - Morcatko.AspNetCore.JsonMergePatch.NewtonsoftJson (ASP.NET Core 3.x)
Nuget - Morcatko.AspNetCore.JsonMergePatch.SystemText (ASP.NET Core 3.x)
Nuget - Morcatko.AspNetCore.JsonMergePatch.Document (ASP.NET Core 3.x - base package)

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 2.1-testApp or 3.0 testApp for sample

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

public void ConfigureServices(IServiceCollection services)
{
    ...
    services
        .AddMvc()                         // or .AddMvcCore()
        //.AddJsonMergePatch();           // 2.x
        //.AddNewtonsoftJsonMergePatch(); // 3.x (Newtonsoft.Josn)
        //.AddSystemTextJsonMergePatch(); // 3.x (System.Text)
    ...
}
  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

  • 2.x - https://github.com/Morcatko/Morcatko.AspNetCore.JsonMergePatch/blob/master/src/2.1-testApp/JsonMergePatchDocumentOperationFilter.cs
  • 3.x - https://github.com/Morcatko/Morcatko.AspNetCore.JsonMergePatch/blob/master/src/3.0-testApp/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 tests in ...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
3.0.1 0 3/24/2020
3.0.0 653 2/18/2020
3.0.0-Release0.0 535 11/27/2019
3.0.0-preview9.0 264 9/6/2019