The JsonPatchDocument<T> as an argument to a PATCH method on server is not recognized on swagger/redoc page and shown as {}.
The way to overcome this issue it to use List<Operation<T>> as argument instead and initialize JsonPatchDocument<T> from it in patch method:
public async Task<ActionResult<User>> PatchUserAsync(int id, List<Operation<User>> operations)
{
var patch = new JsonPatchDocument<User>(operations, new());
// some request logics
patch.ApplyTo(update);
// more request logic
}
Sending collection of operations as application/json-patch+json works fine too. The problem is: initializing list of Operations isn't very convenient. Instead of:
var patch = new JsonPatchDocument<User>();
patch.Replace((u) => u.Name, "Tom");
patch.Replace((u) => u.Age, 40);
We have to write such code:
var operations = new List<Operation<User>>
{
new Operation<User>("replace", "/name", null, "Tom"),
new Operation<User>("replace", "/age", null, 40)
};
The problems of this code are obvious: we have to rely on string values when creating the operations (while it could have been safer to use OperationType enum) and we have to rely on string when resolving path.
Probably a static methods for Operation<T> class could be implemented, so the usage would look similar to this:
var operations = new List<Operation<User>>
{
Operation<User>.Replace((u) => u.Name, "Tom"),
Operation<User>.Replace((u) => u.Age, 40)
};
These static methods would also simplify related calls in JsonPatchDocumentOfT:
|
Operations.Add(new Operation<TModel>( |
The
JsonPatchDocument<T>as an argument to aPATCHmethod on server is not recognized on swagger/redoc page and shown as{}.The way to overcome this issue it to use
List<Operation<T>>as argument instead and initializeJsonPatchDocument<T>from it in patch method:Sending collection of operations as
application/json-patch+jsonworks fine too. The problem is: initializing list ofOperations isn't very convenient. Instead of:We have to write such code:
The problems of this code are obvious: we have to rely on
stringvalues when creating the operations (while it could have been safer to useOperationTypeenum) and we have to rely onstringwhen resolvingpath.Probably a static methods for
Operation<T>class could be implemented, so the usage would look similar to this:These static methods would also simplify related calls in
JsonPatchDocumentOfT:SystemTextJsonPatch/SystemTextJsonPatch/JsonPatchDocumentOfT.cs
Line 189 in c7ffbaf