Description
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 Operation
s 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
: