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
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`.
56
56
57
+
## Action method code
58
+
59
+
In an API controller, an action method for JSON Patch:
60
+
61
+
* Is annotated with the `HttpPatch` attribute.
62
+
* Accepts a <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument%601>, typically with [`[FromBody]`](xref:Microsoft.AspNetCore.Mvc.FromBodyAttribute).
63
+
* Calls <xref:Microsoft.AspNetCore.JsonPatch.JsonPatchDocument.ApplyTo(System.Object)> on the patch document to apply the changes.
> In a real app, the data would typically be retrieved from a store such as a database. After applying the patch, the updated data would be saved back to the database.
83
+
84
+
### Model state
85
+
86
+
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:
87
+
88
+
```json
89
+
{
90
+
"Customer": [
91
+
"The current value 'John' at path 'customerName' != test value 'Nancy'."
92
+
]
93
+
}
94
+
```
95
+
57
96
## JSON Patch Operations
58
97
59
98
The following sections describe the supported JSON Patch operations `add`, `remove`, `replace`, `move`, `copy`, and `test` that modify a JSON document and provide examples of their usage.
@@ -202,7 +241,7 @@ Example:
202
241
203
242
The following examples demonstrate how to use the `ApplyTo` method to apply a JSON Patch document to an object.
204
243
205
-
## Example: Apply a `JsonPatchDocument` to an object
244
+
###Example: Apply a `JsonPatchDocument` to an object
206
245
207
246
The following example demonstrates:
208
247
@@ -282,7 +321,7 @@ Key differences between `System.Text.Json` and the new `JsonPatchDocument<T>` im
282
321
* The runtime type of the target object, not the declared type, determines which properties `ApplyTo` patches.
283
322
*`System.Text.Json` deserialization relies on the declared type to identify eligible properties.
284
323
285
-
## Example: Apply a JsonPatchDocument with error handling
324
+
###Example: Apply a JsonPatchDocument with error handling
286
325
287
326
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.
288
327
@@ -404,6 +443,17 @@ public void Validate(JsonPatchDocument<T> patch)
404
443
* Protect endpoints accepting JSON Patch requests with proper authentication and authorization mechanisms.
405
444
* Restrict access to trusted clients or users with appropriate permissions.
406
445
446
+
## Get the code
447
+
448
+
[View or download sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/web-api/jsonpatch/samples). ([How to download](xref:index#how-to-download-a-sample)).
449
+
450
+
To test the sample, run the app and send HTTP requests with the following settings:
0 commit comments