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
After upgrading from dotnet 8 to dotnet 9 our applications don't work anymore. This is because we are using HttpClient generation with NSwag and unknown objects are serialized to JsonDocument? properties. This is a breaking change. I have tried a little bit with the new RespectNullableAnnotations and DefaultIgnoreCondition but this has not worked either.
Reproduction Steps
usingSystem.Text.Json;//lang=jsonvarjson=""" { "Properties": null } """;vardto=JsonSerializer.Deserialize<MyTestClass>(json);//Returns true in dotnet 8 and false in dotnet 9Console.WriteLine(dto!.Propertiesisnull);//Returns false in dotnet 8 and true in dotnet 9Console.WriteLine(dto!.PropertiesisJsonDocument{RootElement.ValueKind:JsonValueKind.Null});publicclassMyTestClass(){publicJsonDocument?Properties{get;set;}}
Expected behavior
Properties property in MyTestClass should be null
Actual behavior
Properties property in MyTestClass is JsonDocument with RootElement.ValueKind = JsonValueKind.Null
Regression?
It worked on dotnet 8
Known Workarounds
public class JsonDocumentNullableConverter : JsonConverter<JsonDocument?>
{
public override JsonDocument? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return null;
return JsonDocument.ParseValue(ref reader);
}
public override void Write(Utf8JsonWriter writer, JsonDocument? value, JsonSerializerOptions options)
{
if (value is null)
{
writer.WriteNullValue();
return;
}
value.WriteTo(writer);
}
}
Configuration
dotnet 9.0.100 SDK
Other information
No response
The text was updated successfully, but these errors were encountered:
The change was made intentionally in #97596 to address an inconsistency between JsonDocument.Parse which correcty maps null to JsonValueKind.Null and the built-in converter for JsonDocument.
Thank you for reporting that you encountered this, @aco-mreble. I've referenced the code sample you provided in the breaking change documentation issue: dotnet/docs#43869.
Description
After upgrading from dotnet 8 to dotnet 9 our applications don't work anymore. This is because we are using HttpClient generation with NSwag and unknown objects are serialized to
JsonDocument?
properties. This is a breaking change. I have tried a little bit with the newRespectNullableAnnotations
andDefaultIgnoreCondition
but this has not worked either.Reproduction Steps
Expected behavior
Properties
property in MyTestClass should benull
Actual behavior
Properties
property in MyTestClass isJsonDocument
withRootElement.ValueKind = JsonValueKind.Null
Regression?
It worked on dotnet 8
Known Workarounds
Configuration
dotnet 9.0.100 SDK
Other information
No response
The text was updated successfully, but these errors were encountered: