JsonPatch: Use registered json converters that has HandleNull = true
- 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
Consider the json patch `JsonPatchDocument` where:
```C#
record Model
{
public OneOf IntValue { get; set; }
}
```
`OneOf<,>` is a struct.
I have a json converter registered that handles this, which is registered with `HandleNull = true` so that it sets the default value of `None` for `JsonTokenType.Null`. This converter is however not invoked for patch operations. It currently simply returns a `ConversionResult` based on if the value `IsNullableType` (https://github.com/dotnet/aspnetcore/blob/main/src/Features/JsonPatch/src/Internal/ConversionResultProvider.cs#L25) and (https://github.com/dotnet/aspnetcore/blob/main/src/Features/JsonPatch.SystemTextJson/src/Internal/ConversionResultProvider.cs#L15) which it is clearly not, so the conversion fails
### Expected Behavior
I expect that a registered json converter with `HandleNull` is invoked when a patch operation has value null for non-reference types.
### Steps To Reproduce
```C#
record Model
{
public OneOf IntValue { get; set; }
}
class OneOfJsonConverter : JsonConverter>
{
public override bool HandleNull { get; } = true;
public override OneOf Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options
)
{
return reader.TokenType switch
{
JsonTokenType.Null => new None(),
_ => reader.GetInt32(),
};
}
public override void Write(
Utf8JsonWriter writer,
OneOf value,
JsonSerializerOptions options
)
{
throw new NotImplementedException();
}
}
```
### Exceptions (if any)
_No response_
### .NET Version
10
### Anything else?
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.