NewtonsoftJsonPatchInputFormatter not working for non generic JsonPatchDocument with System.Text.Json
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Describe the bug
When using System.Text.Json, patches to [dynamic objects](https://learn.microsoft.com/en-us/aspnet/core/web-api/jsonpatch?view=aspnetcore-8.0#dynamic-objects) using the non-generic type JsonPatchDocument are not processed correctly.
The CanRead method of the NewtonsoftJsonPatchInputFormatter class does not allow non-generic types.
With this behavior, patch requests are not handled correctly and the InputFormatter falls back to the default of System.Text.Json, which cannot deserialize this request into a suitable object.
```
public override bool CanRead(InputFormatterContext context)
{
ArgumentNullException.ThrowIfNull(context);
var modelType = context.ModelType;
if (!typeof(IJsonPatchDocument).IsAssignableFrom(modelType) ||
!modelType.IsGenericType)
{
return false;
}
return base.CanRead(context);
}
```
### Expected Behavior
Perhaps the type should be treated explicitly
```
public override bool CanRead(InputFormatterContext context)
{
ArgumentNullException.ThrowIfNull(context);
var modelType = context.ModelType;
if (!typeof(IJsonPatchDocument).IsAssignableFrom(modelType) ||
!(modelType.IsGenericType && modelType.Equals(typeof(JsonPatchDocument))))
{
return false;
}
return base.CanRead(context);
}
```
### Steps To Reproduce
1. [Setup a project like described](https://learn.microsoft.com/en-us/aspnet/core/web-api/jsonpatch?view=aspnetcore-8.0#add-support-for-json-patch-when-using-systemtextjson)
2. [Create Endpoint for dynamic objects](https://learn.microsoft.com/en-us/aspnet/core/web-api/jsonpatch?view=aspnetcore-8.0#dynamic-objects)
3. Send Request to Endpoint
### Exceptions (if any)
_No response_
### .NET Version
.NET 8.0
### Anything else?
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.