Immutable Properties With JSON Patch in AspNet Core

Maxwell Weru
The Startup
Published in
4 min readNov 17, 2020

--

JSON Patch

There is now a NuGet package for this: Tingle.AspNetCore.JsonPatch.NewtonsoftJson

There are times you want ot make updates to a resource on the server without replacing the whole of it. JSON Patch works a great deal in this, lighter and very powerful. To learn more on JSON Patch check out these links below:

  1. Official site at http://jsonpatch.com
  2. Introductory Article by Kevin Sookocheff
  3. Explainer by Waqat Mansor

ASP.NET Core supports JSON Patch natively using the HttpPatchAttribute usually decorate on a controller action with [HttpPatch], model binding support for JsonPatchDocument and JsonPatchDocument<T>, and model validation support when applying changes via extension method with the signature document.ApplyTo(document, ModelState). More on this can be found in the official documentation.

On this post I will not focus on how JSON Patch works, why it is better or how it is supported in ASP.NET Core (for that, see above links). Instead, I will focus on how you can ensure that certain properties are not edited in a patch operation or ensure that only certain properties can be edited.

TLDR; you can jump straight to the code.

Breakdown for parts?

--

--