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/release-notes/aspnetcore-10/includes/jsonPatch.md
+58-56Lines changed: 58 additions & 56 deletions
Original file line number
Diff line number
Diff line change
@@ -1,87 +1,89 @@
1
-
### New JsonPatch Implementation with System.Text.Json
1
+
### New JSON Patch implementation with `System.Text.Json`
2
2
3
3
**[JSON Patch](https://jsonpatch.com/)**:
4
4
5
5
* Is a standard format for describing changes to apply to a JSON document.
6
-
* Is defined in [RFC 6902] and is widely used in RESTful APIs to perform partial updates to JSON resources.
6
+
* Is defined in RFC 6902 and is widely used in RESTful APIs to perform partial updates to JSON resources.
7
7
* Represents a sequence of operations (for example, Add, Remove, Replace, Move, Copy, Test) that can be applied to modify a JSON document.
8
8
9
9
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.
10
10
11
11
[RFC 6902]: https://tools.ietf.org/html/rfc6902
12
12
13
-
This release introduces a new implementation of `JsonPatch` based on `System.Text.Json` serialization. This feature:
13
+
This release introduces a new implementation of <xref:Microsoft.AspNetCore.JsonPatch> based on <xref:System.Text.Json?displayProperty=fullName> serialization. This feature:
14
14
15
15
* Aligns with modern .NET practices by leveraging the `System.Text.Json` library, which is optimized for .NET.
16
16
* Provides improved performance and reduced memory usage compared to the legacy `Newtonsoft.Json`-based implementation.
17
17
18
-
The following benchmarks compare the performance of the new `System.Text.Json` implementation with the legacy `Newtonsoft.Json` implementation:
18
+
The following benchmarks compare the performance of the new `System.Text.Json` implementation with the legacy `Newtonsoft.Json` implementation.
19
19
20
-
| Scenario | Implementation| Mean | Allocated Memory |
These benchmarks highlight significant performance gains and reduced memory usage with the new implementation.
28
28
29
29
Notes:
30
-
* The new implementation isn't a drop-in replacement for the legacy implementation. In particular:
31
-
* The new implementation doesn't support dynamic types, for example, [`ExpandoObject`](/dotnet/api/system.dynamic.expandoobject).
30
+
* The new implementation isn't a drop-in replacement for the legacy implementation. In particular, the new implementation doesn't support dynamic types, for example, <xref:System.Dynamic.ExpandoObject>.
32
31
* 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.
33
32
34
33
#### Usage
35
34
36
-
To enable JSON Patch support with `System.Text.Json`, install the [`Microsoft.AspNetCore.JsonPatch.SystemTextJson`](https://www.nuget.org/packages/Microsoft.AspNetCore.JsonPatch/) NuGet package.
35
+
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.
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`.
41
+
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`.
43
42
44
43
The following examples demonstrate how to use the `ApplyTo` method to apply a JSON Patch document to an object.
45
44
46
-
#### Example: Applying a JsonPatchDocument
45
+
#### Example: Applying a `JsonPatchDocument`
47
46
48
47
The following example demonstrates:
49
48
50
49
1. The `add`, `replace`, and `remove` operations.
51
50
2. Operations on nested properties.
52
51
3. Adding a new item to an array.
53
-
4. Using a JSON String Enum Converter in a JSON patch document.
52
+
4. Using a JSON String Enum Converter in a JSON Patch document.
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:
114
116
115
-
*`NumberHandling`: Whether numeric properties can be read from strings.
117
+
*`NumberHandling`: Whether numeric properties are read from strings.
116
118
*`PropertyNameCaseInsensitive`: Whether property names are case-sensitive.
117
119
118
120
Key differences between `System.Text.Json` and the new `JsonPatchDocument<T>` implementation:
119
121
120
-
* The runtime type of the target object, not the declared type, determines which properties `ApplyTo` patches.
122
+
* The runtime type of the target object, not the declared type, determines which properties `ApplyTo` patches.
121
123
*`System.Text.Json` deserialization relies on the declared type to identify eligible properties.
122
124
123
-
#### Example: Applying a JsonPatchDocument with error handling
125
+
#### Example: Applying a `JsonPatchDocument` with error handling
124
126
125
127
There are various errors that can occur when applying a JSON Patch document. For example, the target object may not have the specified property, or the value specified might be incompatible with the property type.
126
128
127
-
JSON `Patch` also supports the `test` operation. The `test` operation checks if a specified value is equal to the target property, and if not, returns an error.
129
+
JSON Patch also supports the `test` operation. The `test` operation checks if a specified value is equal to the target property, and if not, returns an error.
128
130
129
131
The following example demonstrates how to handle these errors gracefully.
130
132
131
-
> [!Important]
133
+
> [!IMPORTANT]
132
134
> The object passed to the `ApplyTo` method is modified in place. It is the caller's responsiblity to discard these changes if any operation fails.
When using the `Microsoft.AspNetCore.JsonPatch.SystemTextJson` package, it's critical to understand and mitigate potential security risks. The following sections outline the identified security risks associated with JSON Patch and provide recommended mitigations to ensure secure usage of the package.
192
194
193
195
> [!IMPORTANT]
194
-
> ***This is not an exhaustive list of threats.***app developers must conduct their own threat model reviews to determine an app-specific comprehensive list and come up with appropriate mitigations as needed. For example, apps which expose collections to patch operations should consider the potential for algorithmic complexity attacks if those operations insert or remove elements at the beginning of the collection.
196
+
> ***This is not an exhaustive list of threats.***App developers must conduct their own threat model reviews to determine an app-specific comprehensive list and come up with appropriate mitigations as needed. For example, apps which expose collections to patch operations should consider the potential for algorithmic complexity attacks if those operations insert or remove elements at the beginning of the collection.
195
197
196
198
By running comprehensive threat models for their own apps and addressing identified threats while following the recommended mitigations below, consumers of these packages can integrate JSON Patch functionality into their apps while minimizing security risks.
197
199
@@ -207,15 +209,15 @@ Consumers of these packages can integrate JSON Patch functionality into their ap
207
209
***Impact**: Potential Out-Of-Memory (OOM) conditions, causing service disruptions.
208
210
***Mitigation**:
209
211
* Validate incoming JSON Patch documents for size and structure before calling `ApplyTo`.
210
-
* The validation needs to be app specific, but an example validation can look similar to the following:
212
+
* The validation must be app specific, but an example validation can look similar to the following:
211
213
212
214
```csharp
213
215
publicvoidValidate(JsonPatchDocument<T>patch)
214
216
{
215
217
// This is just an example. It's up to the developer to make sure that
216
-
// this case is handled properly, based on the app needs.
218
+
// this case is handled properly, based on the app's requirements.
217
219
if (patch.Operations.Where(op=>op.OperationType==OperationType.Copy).Count()
218
-
>MaxCopyOperationsCount)
220
+
>MaxCopyOperationsCount)
219
221
{
220
222
thrownewInvalidOperationException();
221
223
}
@@ -224,7 +226,7 @@ public void Validate(JsonPatchDocument<T> patch)
224
226
225
227
##### Business Logic Subversion
226
228
227
-
***Scenario**: Patch operations can manipulate fields with implicit invariants, (e.g., internal flags, IDs, or computed fields), violating business constraints.
229
+
***Scenario**: Patch operations can manipulate fields with implicit invariants, (for example, internal flags, IDs, or computed fields), violating business constraints.
228
230
***Impact**: Data integrity issues and unintended app behavior.
229
231
***Mitigation**:
230
232
* Use POCO objects with explicitly defined properties that are safe to modify.
0 commit comments