You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/web-api/jsonpatch.md
+23-23Lines changed: 23 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ uid: web-api/jsonpatch
14
14
15
15
This article explains how to handle JSON Patch requests in an ASP.NET Core web API.
16
16
17
-
JSON Patch support in ASP.NET Core web API is based on `System.Text.Json` serialization, and requires the [`Microsoft.AspNetCore.JsonPatch.SystemTextJson`](https://www.nuget.org/packages/Microsoft.AspNetCore.JsonPatch.SystemTextJson) NuGet package.
17
+
JSON Patch support in ASP.NET Core web API is based on <xref:System.Text.Json> serialization, and requires the [`Microsoft.AspNetCore.JsonPatch.SystemTextJson`](https://www.nuget.org/packages/Microsoft.AspNetCore.JsonPatch.SystemTextJson) NuGet package.
18
18
19
19
**[JSON Patch](https://jsonpatch.com/)**:
20
20
@@ -24,12 +24,12 @@ JSON Patch support in ASP.NET Core web API is based on `System.Text.Json` serial
24
24
25
25
In web apps, JSON Patch is commonly used in a PATCH operation to perform partial updates of a resource. Rather than sending the entire resource for an update, clients can send a JSON Patch document containing only the changes. Patching reduces payload size and improves efficiency.
26
26
27
-
JSON Patch support in ASP.NET Core web API is based on `System.Text.Json` serialization, starting with .NET 10. This release introduces a new implementation of `JsonPatch` based on `System.Text.Json` serialization. This feature:
27
+
JSON Patch support in ASP.NET Core web API is based on <xref:System.Text.Json> serialization, starting with .NET 10. This release introduces a new implementation of <xref:Microsoft.AspNetCore.JsonPatch> based on <xref:System.Text.Json> serialization. This feature:
28
28
29
-
* Aligns with modern .NET practices by leveraging the `System.Text.Json` library, which is optimized for .NET.
29
+
* Aligns with modern .NET practices by leveraging the <xref:System.Text.Json> library, which is optimized for .NET.
30
30
* Provides improved performance and reduced memory usage compared to the legacy `Newtonsoft.Json`-based implementation. For more information on the legacy `Newtonsoft.Json`-based implementation, see the [.NET 9 version of this article](xref:web-api/jsonpatch?view=aspnetcore-9.0&preserve-view=true).
31
31
32
-
The following benchmarks compare the performance of the new `System.Text.Json` implementation with the legacy `Newtonsoft.Json` implementation:
32
+
The following benchmarks compare the performance of the new <xref:System.Text.Json> implementation with the legacy `Newtonsoft.Json` implementation:
33
33
34
34
| Scenario | Implementation | Mean | Allocated Memory |
@@ -46,22 +46,22 @@ These benchmarks highlight significant performance gains and reduced memory usag
46
46
> [!IMPORTANT]
47
47
> The JSON Patch standard has ***inherent security risks***. Since these risks are inherent to the JSON Patch standard, the new implementation ***doesn't attempt to mitigate inherent security risks***. It's the responsibility of the developer to ensure that the JSON Patch document is safe to apply to the target object. For more information, see the [Mitigating Security Risks](#mitigating-security-risks) section.
48
48
49
-
## Enable JSON Patch support with `System.Text.Json`
49
+
## Enable JSON Patch support with <xref:System.Text.Json>
50
50
51
-
To enable JSON Patch support with `System.Text.Json`, install the [`Microsoft.AspNetCore.JsonPatch.SystemTextJson`](https://www.nuget.org/packages/Microsoft.AspNetCore.JsonPatch.SystemTextJson) NuGet package.
51
+
To enable JSON Patch support with <xref:System.Text.Json>, install the [`Microsoft.AspNetCore.JsonPatch.SystemTextJson`](https://www.nuget.org/packages/Microsoft.AspNetCore.JsonPatch.SystemTextJson) NuGet package.
This package provides a `JsonPatchDocument<T>` class to represent a JSON Patch document for objects of type `T` and custom logic for serializing and deserializing JSON Patch documents using `System.Text.Json`. The key method of the `JsonPatchDocument<T>` class is `ApplyTo`, which applies the patch operations to a target object of type `T`.
57
+
This package provides a <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument%601> class to represent a JSON Patch document for objects of type `T` and custom logic for serializing and deserializing JSON Patch documents using <xref:System.Text.Json>. The key method of the <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument%601> class is <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument.ApplyTo(System.Object)>, which applies the patch operations to a target object of type `T`.
58
58
59
59
## Action method code
60
60
61
61
In an API controller, an action method for JSON Patch:
62
62
63
-
* Is annotated with the `HttpPatch` attribute.
64
-
* Accepts a <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument%601>, typically with [`[FromBody]`](xref:Microsoft.AspNetCore.Mvc.FromBodyAttribute).
63
+
* Is annotated with the <xref:Microsoft.AspNetCore.Mvc.HttpPatchAttribute> attribute.
64
+
* Accepts a <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument%601>, typically with [<xref:Microsoft.AspNetCore.Mvc.FromBodyAttribute>](xref:Microsoft.AspNetCore.Mvc.FromBodyAttribute).
65
65
* Calls <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument.ApplyTo(System.Object)> on the patch document to apply the changes.
66
66
67
67
Here's an example:
@@ -85,7 +85,7 @@ The sample action method:
85
85
86
86
### Model state
87
87
88
-
The preceding action method example calls an overload of `ApplyTo` that takes model state as one of its parameters. With this option, you can get error messages in responses. The following example shows the body of a 400 Bad Request response for a `test` operation:
88
+
The preceding action method example calls an overload of <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument.ApplyTo(System.Object)> that takes model state as one of its parameters. With this option, you can get error messages in responses. The following example shows the body of a 400 Bad Request response for a `test` operation:
89
89
90
90
```json
91
91
{
@@ -241,9 +241,9 @@ Example:
241
241
242
242
## Apply a JSON Patch document to an object
243
243
244
-
The following examples demonstrate how to use the `ApplyTo` method to apply a JSON Patch document to an object.
244
+
The following examples demonstrate how to use the <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument.ApplyTo(System.Object)> method to apply a JSON Patch document to an object.
245
245
246
-
### Example: Apply a `JsonPatchDocument` to an object
246
+
### Example: Apply a <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument%601> to an object
247
247
248
248
The following example demonstrates:
249
249
@@ -313,15 +313,15 @@ The previous example results in the following output of the updated object:
313
313
}
314
314
```
315
315
316
-
The `ApplyTo` method generally follows the conventions and options of `System.Text.Json` for processing the `JsonPatchDocument`, including the behavior controlled by the following options:
316
+
The <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument.ApplyTo(System.Object)> method generally follows the conventions and options of <xref:System.Text.Json> for processing the <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument%601>, including the behavior controlled by the following options:
317
317
318
-
*`NumberHandling`: Whether numeric properties are read from strings.
319
-
*`PropertyNameCaseInsensitive`: Whether property names are case-sensitive.
318
+
*<xref:System.Text.Json.Serialization.JsonNumberHandling>: Whether numeric properties are read from strings.
319
+
*<xref:System.Text.Json.JsonSerializerOptions.PropertyNameCaseInsensitive>: Whether property names are case-sensitive.
320
320
321
-
Key differences between `System.Text.Json` and the new `JsonPatchDocument<T>` implementation:
321
+
Key differences between <xref:System.Text.Json> and the new <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument%601> implementation:
322
322
323
-
* The runtime type of the target object, not the declared type, determines which properties `ApplyTo` patches.
324
-
*`System.Text.Json` deserialization relies on the declared type to identify eligible properties.
323
+
* The runtime type of the target object, not the declared type, determines which properties <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument.ApplyTo(System.Object)> patches.
324
+
*<xref:System.Text.Json> deserialization relies on the declared type to identify eligible properties.
325
325
326
326
### Example: Apply a JsonPatchDocument with error handling
327
327
@@ -332,7 +332,7 @@ JSON `Patch` supports the `test` operation, which checks if a specified value eq
332
332
The following example demonstrates how to handle these errors gracefully.
333
333
334
334
> [!Important]
335
-
> The object passed to the `ApplyTo` method is modified in place. The caller is responsible for discarding changes if any operation fails.
335
+
> The object passed to the <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument.ApplyTo(System.Object)> method is modified in place. The caller is responsible for discarding changes if any operation fails.
336
336
337
337
```csharp
338
338
// Original object
@@ -412,7 +412,7 @@ Consumers of these packages can integrate JSON Patch functionality into their ap
412
412
***Scenario**: A malicious client submits a `copy` operation that duplicates large object graphs multiple times, leading to excessive memory consumption.
413
413
***Impact**: Potential Out-Of-Memory (OOM) conditions, causing service disruptions.
414
414
***Mitigation**:
415
-
* Validate incoming JSON Patch documents for size and structure before calling `ApplyTo`.
415
+
* Validate incoming JSON Patch documents for size and structure before calling <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument.ApplyTo(System.Object)>.
416
416
* The validation must be app specific, but an example validation can look similar to the following:
417
417
418
418
```csharp
@@ -433,9 +433,9 @@ public void Validate(JsonPatchDocument<T> patch)
433
433
***Scenario**: Patch operations can manipulate fields with implicit invariants, (for example, internal flags, IDs, or computed fields), violating business constraints.
434
434
***Impact**: Data integrity issues and unintended app behavior.
435
435
***Mitigation**:
436
-
* Use POCO objects with explicitly defined properties that are safe to modify.
437
-
* Avoid exposing sensitive or security-critical properties in the target object.
438
-
* If no POCO object is used, validate the patched object after applying operations to ensure business rules and invariants aren't violated.
436
+
* Use POCOs (Plain Old CLR Objects) with explicitly defined properties that are safe to modify.
437
+
* Avoid exposing sensitive or security-critical properties in the target object.
438
+
* If a POCO object isn't used, validate the patched object after applying operations to ensure business rules and invariants aren't violated.
0 commit comments